#include<iostream>
using namespace std;
#include<string>
#define max 1000
struct person{
string name;
string age;
string tel;
string address;
};
struct addrbook{
int size ;
struct person abs[max];
};
void printmenu(){
cout << "************************" << endl;
cout << "***** 1.添加联系人 *****" << endl;
cout << "***** 2.显示联系人 *****" << endl;
cout << "***** 3.删除联系人 *****" << endl;
cout << "***** 4.查找联系人 *****" << endl;
cout << "***** 5.修改联系人 *****" << endl;
cout << "***** 6.清空通讯录 *****" << endl;
cout << "***** 0.退出通讯录 *****" << endl;
cout << "************************" << endl;
}
void addperson(struct addrbook * a){
cout << "请输入姓名:" << endl;
cin >> a->abs[a->size].name;
cout << "请输入年龄: " << endl;
cin >> a->abs[a->size].age;
cout << "请输入电话: " << endl;
cin >> a->abs[a->size].tel;
cout << "请输入住址: " << endl;
cin >> a->abs[a->size].address;
a->size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
void displayper(struct addrbook * a){
if(a->size == 0){
cout << "********通讯录为空********" << endl;
system("pause");
system("cls");
return;
}
cout << "*********通讯录*********" << endl;
for(int i = 0;i < a->size;i++){
cout << "姓名:"<< a->abs[i].name << endl;
cout << "\t年龄:"<< a->abs[i].age << endl;
cout << "\t电话:"<< a->abs[i].tel << endl;
cout << "\t住址:"<< a->abs[i].address << endl;
cout << "************************" << endl;
}
system("pause");
system("cls");
}
int checkbook(struct addrbook * a){
string name;
cin >> name;
for(int i = 0;i < a->size;i++){
if(a->abs[i].name == name){
return i;
}
}
return -1;
}
void deleteperson(struct addrbook * a){
cout << "请输入要删除的联系人的姓名: " << endl;
int ret = checkbook(a);
if(ret == -1){
cout << "查无此人!" << endl;
system("pause");
system("cls");
return;
}
for(int i = ret;i < a->size-1;i++){
a->abs[i] = a->abs[i+1];
}
a->size--;
cout << "删除成功!" << endl;
system("pause");
system("cls");
return;
}
void findperson(struct addrbook * a){
cout << "请输入要查找的联系人的姓名: " << endl;
int ret = checkbook(a);
if(ret == -1){
cout << "查无此人" << endl;
system("pause");
system("cls");
return;
}
cout << "查找成功! 信息如下:" << endl;
cout << "姓名 :" << a->abs[ret].name << endl;
cout << "\t年龄:"<< a->abs[ret].age << endl;
cout << "\t电话:"<< a->abs[ret].tel << endl;
cout << "\t住址:"<< a->abs[ret].address << endl;
system("pause");
system("cls");
return;
}
void modifybook(struct addrbook * a){
cout << "请输入要修改的联系人的姓名: " << endl;
int ret = checkbook(a);
if(ret == -1){
cout << "查无此人" << endl;
system("pause");
system("cls");
return;
}
cout << "请输入姓名:" << endl;
cin >> a->abs[ret].name;
cout << "请输入年龄: " << endl;
cin >> a->abs[ret].age;
cout << "请输入电话: " << endl;
cin >> a->abs[ret].tel;
cout << "请输入住址: " << endl;
cin >> a->abs[ret].address;
cout << "修改成功!" << endl;
system("pause");
system("cls");
return;
}
int main(){
int opr;
struct addrbook txl;
txl.size = 0;
while(1){
printmenu();
scanf("%d",&opr);
switch(opr){
case 1:
addperson(&txl);
break;
case 2:
displayper(&txl);
break;
case 3:
deleteperson(&txl);
break;
case 4:
findperson(&txl);
break;
case 5:
modifybook(&txl);
break;
case 6:
if(txl.size == 0){
cout << "您的通讯录已为空,无法进行清空操作" << endl;
system("pause");
system("cls");
break;
}
cout << "您确定要清空通讯录中的所有信息吗?(无法找回)" << endl;
cout << "1.确定" << endl <<"2.取消" << endl;
int opr;
cin >> opr;
if(opr == 2){
break;
}
txl.size = 0;
cout << "已清空!" << endl;
system("pause");
system("cls");
break;
case 0:
cout << "欢迎下次使用!" << endl;
return 0;
default:
cout << "输入有误,请重试" << endl;
system("pause");
system("cls");
break;
}
}
}
通讯录 by C++
最新推荐文章于 2024-10-30 21:53:27 发布