发布于2021-07-24 20:49 阅读(1351) 评论(0) 点赞(28) 收藏(1)
以下代码为本人所作,因水平所限及时间仓促,疏忽之处在所难免。
- #include <iostream>
- #include <string>
- #include <windows.h>
- using namespace std;
- #define max 100
- struct person
- {
- string name;
- string sex;
- string address;
- int phonenumber;
- int age;
- };
- struct phonebook
- {
- person personarr[max];
- int personnum;
-
- };
- void showmenu()
- {
- cout << "请输入您要选择的功能" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
- cout << "1-添加联系人" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
- cout << "2-显示所有联系人" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
- cout << "3-删除联系人" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
- cout << "4-查找联系人" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
- cout << "5-修改联系人" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
- cout << "6-清空通讯录" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
- cout << "7-退出通讯录" << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED|FOREGROUND_BLUE);//设置红色
- }
- void addperson(phonebook* p)
- {
- if (p->personnum == max)
- {
- cout << "联系人已满" << endl;
- return;
- }
- else
- {
- string name;
- cout << "请输入您要添加的联系人的名字" << endl;
-
- cin >> name;
- p->personarr[p->personnum].name = name;
- cout << "请输入您要添加的联系人的地址" << endl;
- string address;
- cin >> address;
- p->personarr[p->personnum].address = address;
- cout << "请输入您要添加的联系人的年龄" << endl;
- int age;
- cin >> age;
- p->personarr[p->personnum].age = age;
- cout << "请输入您要添加的联系人的电话号码" << endl;
- int phonenumber;
- cin >> phonenumber;
- p->personarr[p->personnum].phonenumber = phonenumber;
- cout << "请输入您要添加的联系人的性别" << endl;
- string sex;
- cin >> sex;
- p->personarr[p->personnum].sex = sex;
- p->personnum++;
- }
- cout << "输入完成" << endl;
- system("pause");
- system("cls");
- }
- void showphonebook(phonebook* p)
- {
- if (p->personnum == 0)
- {
- cout << "联系人为空" << endl;
- system("pause");
- system("cls");
- }
- else
- {
- for (int i = 0; i < p->personnum; i++)
- {
- cout << "姓名:" << p->personarr[i].name << "\t";
- cout << "年龄:" << p->personarr[i].age << "\t";
- cout << "住址:" << p->personarr[i].address << "\t";
- cout << "电话:" << p->personarr[i].phonenumber << "\t";
- cout << "性别:" << p->personarr[i].sex << "\t";
- cout << endl;
- }
- system("pause");
- system("cls");
- }
- }
- int isexit(phonebook*p)
- {
- if (p->personnum == 0)
- {
- cout << "通讯录为空" << endl;
- system("pause");
- system("cls");
- return -1;
-
- }
- else
- {
- string name;
- cout << "请输入要删除的名字" << endl;
- cin >> name;
- for (int i = 0; i < p->personnum; i++)
- {
- if (name == p->personarr[i].name)
- {
- return i;
- }
- }
- }
- }
- void deleteinperson(phonebook* p,int ssd)
- {
- if (ssd >= 0)
- {
-
- for(int i=ssd;i<p->personnum;i++)
- {
- p->personarr[i] = p->personarr[i + 1];
- }
- cout << "删除成功" << endl;
- system("pause");
- system("cls");
- }
-
- }
- void searchinperson(phonebook*p)
- {
- string name;
- cout << "请输入要查找的联系人名字:" << endl;
- cin >> name;
-
- for (int i = 0; i <= p->personnum; i++)
- {
- if (p->personarr[i].name == name)
- {
- cout << "以下为联系人信息" << endl;
- cout << p->personarr[i].address << '\t'; cout << p->personarr[i].age << '\t'; cout << p->personarr[i].name << '\t'; cout << p->personarr[i].phonenumber << '\t'; cout << p->personarr[i].address << '\t'; cout << p->personarr[i].sex << endl;
- }
-
- }
- cout << "若未显示联系人信息,则是您未将该联系人添加到通讯录" << endl;
- system("pause");
- system("cls");
- }
- void exchangeinperson(phonebook*p)
- {
- string name;
- cout << "请输入要修改的联系人的名字" << endl;
- cin >> name;
- for(int i=0;i<=p->personnum;i++)
- {
- if (name == p->personarr[i].name)
- {
- int num;
- cout << "请输入你要修改的手机号" << endl;
- cin >> num;
- p->personarr->phonenumber = num;
- cout << "修改完成" << endl;
- }
-
- }
- system("pause");
- system("cls");
- }
- void blankalldata(phonebook* p)
- {
- p->personnum = 0;
- cout << "清空完成" << endl;
- system("pause");
- system("cls");
- }
- int main()
- {
- phonebook inperson;
- inperson.personnum = 0;
- int a = 0;
- while (true)
- {
- showmenu();
- cin >> a;
- switch (a)
- {
- case 1:addperson(&inperson);
- break;
- case 2:
- showphonebook(&inperson);
- break;
- case 3:
- deleteinperson(&inperson, isexit(&inperson));
- break;
- case 4:
- searchinperson(&inperson);
- break;
- case 5:
- exchangeinperson(&inperson);
- break;
- case 6:
- blankalldata(&inperson);
- break;
- case 7:cout << "祝您生活愉快,欢迎下次使用。" << endl;
- system("pause");
- return 0;
- break;
- default:
- break;
- }
- }
- }
作者:小可爱不要爱
链接:http://www.pythonpdf.com/blog/article/284/1d9e2461538ee5ee7c73/
来源:编程知识网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!