各位大佬,求一段代码,课程设计用c++实现通讯录的设计。
自己写了很多,但是运行出来总有bug,跪求一段源代码借鉴,万分感谢。(要求:1.实现通讯录的添加,删除。2.按不同的项进行查找。3.对已存在的通讯录按不同的项排序。4.将通讯录写入文件。5.从文件读入通讯录。数据包含:姓名,地址,电话,邮编,email。)
论坛里有很多类似的,搜索一下。
要不你的代码贴出来大家研究下
#include<iostream> #include<string> #define max 1000 using namespace std; //设计联系人的结构体 struct person{ string m_Name; string m_Sex; int m_Age; string m_Phone; string m_Addr; }; //设计通讯录的结构体 struct Addressbooks { //通讯录中保存的联系人数组 person personarry[max]; //通讯录中当前记录联系人个数 int m_Size; }; void addperson(Addressbooks*abs) { if(abs->m_Size==max) { cout<<"通讯录已满,无法添加!"<<end|; } else{ //添加具体的联系人 //添加姓名 string name; cout<<"请输入姓名:"<<end; cin>>name; abs->personarry[abs->m_Size].m_Name=name; cout<<"您添加的用户姓名为:" <<abs->personarry[abs->m_Size].m_Name<<end|; cout<<"添加成功!"<<end|; while(true){ //添加年龄 int age; cout<<"请输入年龄:"<<end|; cin>>age; if(age>=0&&age<=110){ abs->personarry[abs->m_Size].m_Age=age; cout<<"您添加的用户年龄为:"<<abs->personarry[abs->m_Size].m_Age<<end|; cout<<"添加成功!"<<end|; break; } else{ cout<<"您输入的年龄超出范围,请重新输入!"<<end|; } } while(true){ //添加性别 int sex; cout<<"请输入性别(1:男-2:女)" <<end|; cin>>sex; if(sex==1){ abs->personarry[abs->m_Size].m_Sex="男"; break; } else if(sex==0){ abs->personarry[abs->m_Size].m_Sex="女"; cout<<"您添加的用户性别为:"<<abs->personarry[abs->m_Size].m_Sex<<end|; cout<<"添加成功"<<end|; break; } else{ cout<<"您输入的信息有误,请重新输入"<<end|; } } //添加电话; cout<<"请输入电话号码:"<<end|; string number; cin >> number; abs->personarry[abs->m_Size].m_Phone=number; cout<<"您添加的用户号码为:"<<abs->personarry[abs->m_Size].m_Phone<<end|; string addr; cout<<"请输入用户地址:"<<end|; cin>>addr; abs->personarry[abs->m_Size].m_Addr=addr; cout<<"您添加的用户地址为:"<<abs->personarry[abs->m_Size].m_Addr<<end|; cout<<abs->m_Size+1<<"号联系人添加成功!"<<end|; abs->m_Size++; system("pause"); system("cls"); } } void showperson(Addressbooks*abs){ //判断通讯录中人数是否为0,如果为0提示记录为空 //如果不为0,显示记录的联系人信息 if(abs->m_Size==0) { cout<<"当前记录为空"<<end|; } else{ for(int i=0;i<abs->m_Size;i++) { cout<<"姓名:"<<abs->personarry[i].m_Name<<"\t"; cout<<"性别:"<<abs->personarry[i].m_Sex<<"\t"; cout<<"年龄:"<<abs->personarry[i].m_Age<<"\t"; cout<<"电话:"<<abs->personarry[i].m_Phone<<"\t"; cout<<"住址:"<<abs->personarry[i].m_Addr<<end|; } } system("pause"); system("cls"); } //检查联系人是否存在,如果存在,返回联系人的数组具体位置,不存在返回-1 int isexist(Addressbooks*abs,string name){ for(int i=0;i<abs->m_Size;i++) { if(abs->personarry[i].m_Name==name){ return i;//找到了,返回这个人在数组中的下标编号 } else{ return -1; } } } void deleteperson(Addressbooks*abs){ cout<<"请输入你需要删除的联系人:"<<end|; string name; cin>>name; int ret=isexist(abs,name); if(ret!=-1){ //查找到进行删除的联系人 for(int i=ret;i<abs->m_Size;i++){ //数据前移 abs->personarry[i]=abs->personarry[i+1]; } abs->m_Size--; cout<<"删除成功"<<end|; } else{ cout<<"查无此人"<<end|; } system("pause"); system("cls"); } //4.查找指定的联系人 void findperson(Assressbooks*abs){ cout<<"请输入您要查找的联系人:"<<end|; string name; cin>>name; int ret=isexist(abs,name); if(ret!=-1) { cout<<"姓名:"<<abs->personarry[ret].m_Name<<"\t" <<"性别:"<<abs->personarry[ret].m_Sex<<"\t" <<"年龄:"<<abs->personarry[ret].m_Age<<"\t" <<"电话:"<<abs->personarry[ret].m_Phone<<"\t" <<"住址:"<<abs->personarry[ret].m_Addr<<"\t"<<end; } else{ } system("pause"); system("cls"); } void modifyperson(Addressbooks*abs) { cout<<"请输入您要修改的联系人姓名:"<<end|; string name; cin>>name; int ret=isexist(abs,name); if(ret!=-1){ string name; cout<<"请输入修改后的姓名:"<<end|; cin>>name; abs->personarry[ret].m_Name=name; while(true){ int sex; cout<<"请输入修改后的性别:"<<end; cin>>sex; if(seex==1) { abs->personarry[ret].m_Sex="男"; break; } else if(sex==0){ abs->personarry[ret].m_Sex="女"; break; } else{ cout<<"您输入的信息有误,请重新输入:"<<end|; } } cout<<"请输入修改后的电话号码:"<<end|; int number; cin>>number; abs->personarry[ret].m_Phone=number; cout<<"请输入修改后的联系人地址:"<<end|; string adress; cin>>adress; abs->personarry[ret].m_Addr=adress; cout<<"修改成功"<<end|; } else{ cout<<"查无此人"<<end|; } system("pause"); system("cls"); } void clearperson(Addressbooks*abs){ char goal; cout<<"是否需要清除所有联系人"<<end|; cin>>goal; while(true){ if(goal=='y'){ abs->m_Size=0; cout<<"删除成功,已清除所有联系人!" <<end|; break; } else if(goal=='n'){ cout<<"取消删除1"<<end|; break; } else{ cout<<"您的输入有误,请重新输入"<<end|; } } system("pause"); system("cls"); } //菜单界面 void showmenu() { cout<<"***************************"<<end|; cout<<"***1.添加联系人************"<<end|; cout<<"***2.显示所有联系人********"<<end|; cout<<"***3.删除联系人************"<<end|; cout<<"***4.查找联系人************"<<end|; cout<<"***5.修改联系人************"<<end|; cout<<"***6.清空联系人************"<<end|; cout<<"***0.退出通讯录************"<<end|; cout<<"***************************"<<end|; } int main() { //创建通讯录结构体变量 Addressbooks abs; abs.m _Size=0; int select=0; while(true){ showmenu(); cin>>select; switch(select) { case 1://添加联系人 addperson(&abs);//利用地址传递可以修饰实参 break; case 2; showperson(&abs); break; case 3; deleteperson(&abs); break; case 5; modifyperson(&abs); break; case 6; clearperson(&abs); break; case 0; cout<<"是否退出系统:"<<end|; char goal; cin>>goal; if(goal=='y'){ cout<<"欢迎下次使用"<<end|; system("pause"); return 0; break; } default: cout<<"您输入有误,请重新输入!"<<end|; break; } } showmenu(); system("pause"); return 0; }
抽空修改了下,多个/注释是修改的地方 #include<iostream> #include<string> #define max 1000 using namespace std; //设计联系人的结构体 struct person { string m_Name; string m_Sex; int m_Age; string m_Phone; string m_Addr; }; //设计通讯录的结构体 struct Addressbooks { //通讯录中保存的联系人数组 person personarry[max]; //通讯录中当前记录联系人个数 int m_Size; }; void addperson(Addressbooks *abs) { if(abs->m_Size == max) { cout << "通讯录已满,无法添加!" << endl; } else { //添加具体的联系人 //添加姓名 string name; cout << "请输入姓名:" << endl; cin >> abs->personarry[abs->m_Size].m_Name; //cin >> name; //abs->personarry[abs->m_Size].m_Name = name; cout << "您添加的用户姓名为:" << abs->personarry[abs->m_Size].m_Name << endl; cout << "添加成功!" << endl; while(true) { //添加年龄 int age; cout << "请输入年龄:" << endl; cin >> age; if(age >= 0 && age <= 110) { abs->personarry[abs->m_Size].m_Age = age; cout << "您添加的用户年龄为:" << abs->personarry[abs->m_Size].m_Age << endl; cout << "添加成功!" << endl; break; } else { cout << "您输入的年龄超出范围,请重新输入!" << endl; } } while(true) { //添加性别 int sex; cout << "请输入性别(1:男 2:女)" << endl;//////// cin >> sex; // if(sex == 1) // { // abs->personarry[abs->m_Size].m_Sex = "男"; // break; // } // else if(sex == 0) if(sex == 1 || sex == 2) { //abs->personarry[abs->m_Size].m_Sex = "女"; abs->personarry[abs->m_Size].m_Sex = (sex)? "男": "女"; cout << "您添加的用户性别为:" << abs->personarry[abs->m_Size].m_Sex << endl; cout << "添加成功" << endl; break; } else { cout << "您输入的信息有误,请重新输入" << endl; } } //添加电话; cout << "请输入电话号码:" << endl; //string number; //cin >> number; cin >> abs->personarry[abs->m_Size].m_Phone; //abs->personarry[abs->abs->personarry[abs->m_Size].m_Phone_Size].m_Phone = number; cout << "您添加的用户号码为:" << abs->personarry[abs->m_Size].m_Phone << endl; //string addr; cout << "请输入用户地址:" << endl; cin >> abs->personarry[abs->m_Size].m_Addr; //cin >> addr; //abs->personarry[abs->m_Size].m_Addr = addr; cout << "您添加的用户地址为:" << abs->personarry[abs->m_Size].m_Addr << endl; cout << abs->m_Size + 1 << "号联系人添加成功!" << endl; abs->m_Size++; system("pause"); //system("cls"); } } void showperson(Addressbooks *abs) { //判断通讯录中人数是否为0,如果为0提示记录为空 //如果不为0,显示记录的联系人信息 if(abs->m_Size == 0) { cout << "当前记录为空" << endl; } else { for(int i = 0; i < abs->m_Size; i++) { cout << "姓名:" << abs->personarry[i].m_Name << "\t"; cout << "性别:" << abs->personarry[i].m_Sex << "\t"; cout << "年龄:" << abs->personarry[i].m_Age << "\t"; cout << "电话:" << abs->personarry[i].m_Phone << "\t"; cout << "住址:" << abs->personarry[i].m_Addr << endl; } } system("pause"); //system("cls"); } //检查联系人是否存在,如果存在,返回联系人的数组具体位置,不存在返回-1 int isexist(Addressbooks *abs, string name) { for(int i = 0; i < abs->m_Size; i++) { if(abs->personarry[i].m_Name == name) { return i;//找到了,返回这个人在数组中的下标编号 } //// else //// { //// return -1; //// } } return -1;///////// } void deleteperson(Addressbooks *abs) { cout << "请输入你需要删除的联系人:" << endl; string name; cin >> name; int ret = isexist(abs, name); if(ret != -1) { //查找到进行删除的联系人 for(int i = ret; i < abs->m_Size; i++) { //数据前移 abs->personarry[i] = abs->personarry[i + 1]; } abs->m_Size--; cout << "删除成功" << endl; } else { cout << "查无此人" << endl; } system("pause"); //system("cls"); } //4.查找指定的联系人 //void findperson(Assressbooks *abs) void findperson(Addressbooks *abs) { cout << "请输入您要查找的联系人:" << endl; string name; cin >> name; int ret = isexist(abs, name); if(ret != -1) { cout << "姓名:" << abs->personarry[ret].m_Name << "\t" << "性别:" << abs->personarry[ret].m_Sex << "\t" << "年龄:" << abs->personarry[ret].m_Age << "\t" << "电话:" << abs->personarry[ret].m_Phone << "\t" << "住址:" << abs->personarry[ret].m_Addr << "\t" << endl;///// } else { } system("pause"); //system("cls"); } void modifyperson(Addressbooks *abs) { cout << "请输入您要修改的联系人姓名:" << endl; string name; cin >> name; int ret = isexist(abs, name); if(ret != -1) { /////string name; cout << "请输入修改后的姓名:" << endl; cin >> abs->personarry[ret].m_Name; //cin >> name; //abs->personarry[ret].m_Name = name; while(true) { int sex; cout << "请输入修改后的性别:" << endl;/////// cin >> sex; /////if(seex == 1) if(sex == 1) { abs->personarry[ret].m_Sex = "男"; break; } else if(sex == 2)//////0) { abs->personarry[ret].m_Sex = "女"; break; } else { cout << "您输入的信息有误,请重新输入:" << endl; } } cout << "请输入修改后的电话号码:" << endl; //int number; cin >> abs->personarry[ret].m_Phone; ///cin >> number; ///abs->personarry[ret].m_Phone = number; cout << "请输入修改后的联系人地址:" << endl; //string adress; //cin >> adress; cin >> abs->personarry[ret].m_Addr; //abs->personarry[ret].m_Addr = adress; cout << "修改成功" << endl; } else { cout << "查无此人" << endl; } system("pause"); //system("cls"); } void clearperson(Addressbooks *abs) { char goal; cout << "是否需要清除所有联系人" << endl; cin >> goal; while(true) { if(goal == 'y') { abs->m_Size = 0; cout << "删除成功,已清除所有联系人!" << endl; break; } else if(goal == 'n') { cout << "取消删除1" << endl; break; } else { cout << "您的输入有误,请重新输入" << endl; } } system("pause"); //system("cls"); } //菜单界面 void showmenu() { system("cls");//// 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; } int main() { //创建通讯录结构体变量 Addressbooks abs; //////abs.m _Size = 0; abs.m_Size = 0; int select = 0; while(true) { showmenu(); cin >> select; switch(select) { case 1://添加联系人 addperson(&abs);//利用地址传递可以修饰实参 break; case 2://; showperson(&abs); break; case 3://; deleteperson(&abs); break; case 5://; modifyperson(&abs); break; case 6://; clearperson(&abs); break; case 0://; cout << "是否退出系统:" << endl; char goal; cin >> goal; if(goal == 'y') { cout << "欢迎下次使用" << endl; system("pause"); return 0; break; } default: cout << "您输入有误,请重新输入!" << endl; break; } } ////showmenu(); system("pause"); return 0; }