C++通讯录管理系统
爱思考的小伙 人气:01. 效果图
2. 需要的知识
C++基础知识(包括变量,运算符,顺序,分支,循环)
数组
函数
指针
结构体
3. 源码
#include <iostream> using namespace std; #define MAX 1000 //最大联系人个数 struct Person { string name; string sex; int age; string tell; string addr; }; struct Books { struct Person per[MAX]; int N;//通讯录人数 }; //函数声明 void initBooks(Books *books); void showMenu(); void addPerson(Books *books); void showPerson(Books *books); int findPerson(Books *books,string name); void findPerson(Books *books); void deletePerson(Books *books); void modifyPerson(Books *books); void clearPerson(Books *books); //初始化通讯录,向其中加入联系人 void initBooks(Books *books) { books->per[books->N].name = "张三"; books->per[books->N].sex = "男"; books->per[books->N].age = 22; books->per[books->N].tell = "123456789"; books->per[books->N].addr = "成都"; books->N++; books->per[books->N].name = "李四"; books->per[books->N].sex = "男"; books->per[books->N].age = 23; books->per[books->N].tell = "123456789"; books->per[books->N].addr = "深圳"; books->N++; books->per[books->N].name = "王五"; books->per[books->N].sex = "男"; books->per[books->N].age = 25; books->per[books->N].tell = "123456789"; books->per[books->N].addr = "北京"; books->N++; } //菜单 void showMenu() { system("color a"); 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(Books *books) { if(books->N == MAX) { cout<<"通讯录已满,无法添加\n"; return; } else { string name; string sex; int age; string tell; string addr; cout<<"请输入姓名,性别,年龄,电话以及地址:"; cin>>name>>sex>>age>>tell>>addr; books->per[books->N].name = name; books->per[books->N].sex = sex; books->per[books->N].age = age; books->per[books->N].tell = tell; books->per[books->N].addr = addr; books->N++; cout<<"添加成功\n"; } } //输出 void showPerson(Books *books) { if(books->N == 0) { cout<<"当前记录为空\n"; } else { cout<<"姓名\t性别\t年龄\t电话\t\t地址\n"; for(int i=0; i<books->N; i++) { cout<<books->per[i].name<<"\t" <<books->per[i].sex<<"\t" <<books->per[i].age<<"\t" <<books->per[i].tell<<"\t" <<books->per[i].addr<<"\n"; } } } //查询并返回是否存在 int findPerson(Books *books,string name) { for(int i=0; i<books->N; i++) { if(books->per[i].name== name) { return i; } } return -1; } //查询并输出 void findPerson(Books *books) { cout<<"请输入你要查找的联系人的姓名:"; string name; cin>>name; int ret = findPerson(books,name); if(ret!=-1) { cout<<books->per[ret].name<<"\t" <<books->per[ret].sex<<"\t" <<books->per[ret].age<<"\t" <<books->per[ret].tell<<"\t" <<books->per[ret].addr<<"\n"; } else { cout<<"查无此人\n"; } } //删除 void deletePerson(Books *books) { cout<<"请输入你要删除的联系人的姓名:"; string name; cin>>name; int ret = findPerson(books, name); if(ret!=-1) { for(int i=ret; i<books->N; i++) { if(books->per[i].name== name) { books->per[i] = books->per[i+1]; } } books->N--; } else { cout<<"查无此人\n"; } } //修改 void modifyPerson(Books *books) { cout<<"请输入你要修改的联系人的姓名:"; string name; cin>>name; int ret = findPerson(books, name); if(ret!=-1) { string name; string sex; int age; string tell; string addr; cout<<"请输入姓名,性别,年龄,电话以及地址:"; cin>>name>>sex>>age>>tell>>addr; books->per[ret].name = name; books->per[ret].sex = sex; books->per[ret].age = age; books->per[ret].tell = tell; books->per[ret].addr = addr; cout<<"修改成功\n"; } else { cout<<"查无此人,是否选择新增(Y/N):\n"; char flag; cin>>flag; if(flag=='Y') { addPerson(books); } } } //清空 void clearPerson(Books *books) { books->N = 0; cout<<"通讯录已清空\n"; } int main(int argc, char** argv) { showMenu(); Books books; books.N = 0; initBooks(&books); showPerson(&books); while(true) { showMenu(); cout<<"请输入功能前的序号:\n"; int select=0; cin>>select; switch(select) { case 1: addPerson(&books); break; case 2: showPerson(&books); break; case 3: deletePerson(&books); break; case 4: findPerson(&books); break; case 5: modifyPerson(&books); break; case 6: clearPerson(&books); break; case 0: cout<<"欢迎下次使用\n"; exit(0); default: break; } } return 0; }
加载全部内容