新手C++通讯录实现并且用文件储存

制作存在Za和Ao两个类的头文件

#include <iostream>
#include <string>
#include <windows.h>
#include<fstream>
//包含iostream库,用于输入输出操作。包含string库,用于处理字符串。包含windows.h库,用于使用Windows系统相关功能。包含fstream库,用于文件输入输出操作。
using namespace std;
//使用std命名空间,避免重复写std::。
class Za{
//定义了一个名为Za的类,用于存储个人信息。
private:
string Name;
string Pc;
string Email;
string Address;
string Phone;
//私有成员变量,用于存储姓名、邮编、邮箱、地址和电话号码。
Za *next;
//指向下一个Za对象的指针
public:
Za(){Name="no name";Pc="no pc";Email="no email";Address="no address";Phone="no phone";next=NULL;}
void setname(){cout<<"输入姓名"<<endl;cin>>Name;}
string getname(){return Name;}
void setpc(){cout<<"输入邮编"<<endl;cin>>Pc;}
string getpc(){return Pc;}
void setemail(){cout<<"输入email"<<endl;cin>>Email;} 
string getemail(){return Email;}
void setaddress(){cout<<"输入地址"<<endl;cin>>Address;}
string getaddress(){return Address;}
void setphone(){cout<<"输入电话号码"<<endl;cin>>Phone;}
string getphone(){return Phone;}
//公有成员函数,用于设置和获取个人信息。
void setnext(Za *stu){next=stu;}
Za* getnext(){return next;} 
~Za(){};
};
class Ao{
//定义了一个名为Ao的类。
public:
Ao(){
std::ifstream _if("data.txt");
for(std::string buf; std::getline(_if, buf); )
std::cout << buf << std::endl;}
};
//构造函数,打开名为"data.txt"的文件,并逐行读取文件内容并输出到控制台。

然后制作main函数

#include"Za Ao.h"
//包含了名为"Za Ao.h"的头文件。
const int nu=100;
定义了一个常量nu为100。
int main(){
//定义了主函数main。
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);
//设置控制台文本属性,使输出文字具有红色和绿色(显示为黄色)。
Za *head=new Za();Za *st[nu];int i;
//创建一个Za对象head,并将其指针赋值给指针变量st。定义整型变量i。
ofstream outfile("data.txt",ios::out);
//创建一个名为"data.txt"的输出文件流对象outfile。
int n;
//定义整型变量n。
cout<<"☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"★                                          请输入指令 0,1,2,3,4                                                     ☆"<<endl;
cout<<"☆                                          0、退出通讯录系统                                                        ★"<<endl;
cout<<"★                                              1、 录入                                                             ☆"<<endl;
cout<<"★                                              2、 删除                                                             ☆"<<endl;
cout<<"☆                                              3、 查找                                                             ★"<<endl;
cout<<"★                                              4、 修改                                                             ☆"<<endl;
cout<<"☆                                              5、 通讯录                                                           ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★"<<endl;
cout<<"请输入:"<<endl;
cin>>n;
cout<<endl;
system("cls");
while(!(0<=n&&n<6)){
cin>>n;
system("cls");
}
while(n){
switch (n){
case 1:{
Za* p;
p=new Za();
p->setname();
p->setemail();
p->setpc();
p->setphone();
p->setaddress();
p->setnext(head->getnext());
head->setnext(p);
outfile<<"名字;"<<p->getname()<<"    Email:"<<p->getemail()<<"   邮箱:"<<p->getpc()<<"   电话:"<<p->getphone()<<"   地址:"<<p->getaddress()<<endl;
system("cls");
break;
}
case 2:{
string na;
cout<<"输入要删除的学生名字"<<endl;
cin>>na;
Za* p;Za* q;
p=head;
if(head->getnext()==NULL) cout<<"通讯录为空"<<endl;
while(p->getpc()!=na){
q=p;
p=p->getnext();
}
if(p->getnext()==NULL)
cout<<"该学生不存在"<<endl;
else{
q->setnext(p->getnext());
p->~Za();
cout<<"名字为"<<na<<"的学生已经删除"<<endl;
system("cls");
}
break;
}
case 3:{
string na;
cout<<"请输入姓名"<<endl;
cin>>na;
Za * p=head->getnext();
while(p){
if(p->getname()==na) break;
p=p->getnext();
}
if(p==NULL) cout<<"该学生不存在"<<endl;
else{
cout<<"学生信息:"<<endl;
cout<<"姓名:"<<p->getname()<<endl;
cout<<"邮编:"<<p->getpc()<<endl;
cout<<"email:"<<p->getemail()<<endl;
cout<<"地址:"<<p->getaddress()<<endl;
cout<<"电话号码:"<<p->getphone()<<endl;
system("pause");
}
break;
}
case 4:{
string na;
cout<<"请输入姓名"<<endl;
cin>>na;
Za * p=head->getnext();
while(p){
if(p->getname()==na) break;
p=p->getnext();
}
if(p==NULL) cout<<"该学生不存在"<<endl;
else{ 
cout<<"修改姓名"<<endl;
p->setname();
cout<<"修改邮编"<<endl;
p->setpc();
cout<<"修改email"<<endl;
p->setemail();
cout<<"修改地址"<<endl;
p->setaddress();
cout<<"修改电话号码"<<endl;
p->setphone();
cout<<"修改完毕"<<endl;
system("cls");
}
break;
}
case 5:{
cout<<"通讯录"<<endl;
Ao();
system("pause");
break;
}
}
system("cls");
cout<<"☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"★                                          请输入指令 0,1,2,3,4                                                     ☆"<<endl;
cout<<"☆                                          0、退出通讯录系统                                                        ★"<<endl;
cout<<"★                                              1、 录入                                                             ☆"<<endl;
cout<<"★                                              2、 删除                                                             ☆"<<endl;
cout<<"☆                                              3、 查找                                                             ★"<<endl;
cout<<"★                                              4、 修改                                                             ☆"<<endl;
cout<<"☆                                              5、 通讯录                                                           ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆                                                                                                                   ★"<<endl;
cout<<"★                                                                                                                   ☆"<<endl;
cout<<"☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★ ☆ ★"<<endl;
cout<<"请输入:"<<endl;
cin>>n;
cout<<endl;
system("cls");
} 
outfile.close();
}

猜你喜欢

转载自blog.csdn.net/sschara01/article/details/126267411