数据结构链表实验4
通讯录管理 By sky
1.添加记录
2.删除记录
3.输出记录
4.按姓名查找
5.保存记录
6.按ID排序
7.清空记录
8.退出
Enter:
源码如下:
#include<iostream> #include<fstream> #include<malloc.h> #include<vector> #include<string> #include<cstring> #include<windows.h> using namespace std; typedef struct info{ char id[50]; char name[50]; char tel[50]; struct info *next; }Info; int OpenFile(vector<string>& svec) { ifstream inFile("wj.txt"); string s; while(inFile >> s) { svec.push_back(s); } inFile.close(); return 1; } int AddToFile(Info *p) { p = p->next; fstream outFile; outFile.open("wj.txt",ostream::out); if(!outFile) return 0; while(p!=NULL) { outFile << "/n" << p->id << " " << p->name << " "<< p->tel <<"/n"; p = p->next; } outFile.close(); return 1; } int ReadToInfo(vector<string> sevc,Info *&p) { Info *s,*r; r = p; for (vector<string>::iterator iter = sevc.begin(); iter != sevc.end(); iter+=3) { s = (Info*)malloc(sizeof(Info)); strcpy_s(s->id , (*iter).c_str()); strcpy_s(s->name , (*(iter+1)).c_str()); strcpy_s(s->tel , (*(iter+2)).c_str()); r->next = s; r = s; } r->next = NULL; return 1; } void Show(Info *p) { p=p->next; cout << "id" << "/t" << "name" << "/t" << "tel" <<endl; while(p != NULL) { cout << p->id << "/t" << p->name << "/t" << p->tel <<endl; p = p->next; } } int Insert(Info *&p,string id,string name,string tel) { Info *s,*r; r=p; while(r->next!=NULL) r=r->next; s = (Info*)malloc(sizeof(Info)); strcpy_s(s->id,id.c_str()); strcpy_s(s->name,name.c_str()); strcpy_s(s->tel,tel.c_str()); r->next=s; r=s; r->next = NULL; return 1; } int Delete(Info *&p,string id) { Info *sl=p->next,*q,*f=p; int flag=0; while(sl != NULL) { if(strcmp(sl->id,id.c_str())==0) { q = sl;flag=1; break; } f = sl; sl=sl->next; } if(!flag) return 0; f->next = q->next; free(q); return 1; } int Search(Info *p,string name) { Info *sl = p->next; int flag = 0; while(sl != NULL) { if(strcmp(sl->name,name.c_str()) == 0) { cout << "信息如下:" <<endl; cout << "ID = " << sl->id <<'/n'<< "Name = " << sl->name <<'/n'<<"Tel = " <<sl->tel <<endl; flag = 1; break; } sl = sl->next; } if(!flag) return 0; return 1; } int ClearAll(Info *&p) { Info *sl=p->next,*q,*f=p; while(sl!=NULL) { q = sl; f->next = q->next; sl = sl->next; free(q); } p->next=NULL; return 1; } int Sort(Info *&p) { char str[50]; string _id,_name,_tel; Info *q,*sl=p->next,*sl2; q= (Info*)malloc(sizeof(Info)); q->next =NULL; while(sl!=NULL) { strcpy_s(str,"9999"); while(sl!=NULL) { if(strcmp(sl->id,str)<=0) { strcpy_s(str,sl->id); sl2 = sl; } sl=sl->next; } _id=sl2->id; _name=sl2->name; _tel=sl2->tel; Insert(q,_id,_name,_tel); Delete(p,_id); if(p ==NULL) break; sl = p->next; } p = q; return 0; } int main() { Info *p; p = (Info*)malloc(sizeof(Info)); p->next = NULL; vector<string> svec; string id,name,tel; if(OpenFile(svec)) cout << "打开文件成功" <<endl; if(ReadToInfo(svec,p)) cout << "创建链表成功" <<endl; int Flag=1; Sleep(1000); while(Flag){ system("cls"); cout << "/t/t/t/t" << "通讯录管理 By sky" <<endl; cout << "/t/t/t/t" << "1.添加记录"<<endl; cout << "/t/t/t/t" << "2.删除记录"<<endl; cout << "/t/t/t/t" << "3.输出记录"<<endl; cout << "/t/t/t/t" << "4.按姓名查找"<<endl; cout << "/t/t/t/t" << "5.保存记录"<<endl; cout << "/t/t/t/t" << "6.按ID排序"<<endl; cout << "/t/t/t/t" << "7.清空记录" <<endl; cout << "/t/t/t/t" << "8.退出"<<endl; int n; cout << "/t/t/t/t" << "Enter:"; cin >> n; int flag1,flag2,flag3; switch(n) { case 1: flag1=1; while(flag1) { cout << "请输入同学的ID:"; cin >> id; cout << "请输入同学的姓名:"; cin >> name; cout << "请输入同学的电话:"; cin >> tel; if(Insert(p,id,name,tel)) cout << "成功添加到链表尾!"<<endl; cout << "按0返回,按1继续添加:" <<endl; cin >> flag1; } break; case 2: flag2=1; while(flag2) { cout << "请输入要删除的同学ID:"; cin >> id; if(Delete(p,id)) { cout << "删除成功!" <<endl; cout << "按0返回,按1继续删除" <<endl; cin >>flag2; } else { cout <<"ID不存在,请重新输入!" <<endl; cout << "按0返回,按1重新输入" <<endl; cin >>flag2; } } break; case 3: system("cls"); getchar(); Show(p); cout << "按任意键返回" <<endl; if(getchar()) break; case 4: flag3=1; while(flag3) { cout << "请输入要查询的姓名:"; cin >> name; if(Search(p,name)) { cout << "按0返回,按1继续查找" <<endl; cin >>flag3; } else { cout <<name<<"不存在!"<<endl; cout << "按0返回,按1继续查找" <<endl; cin >>flag3; } } break; case 5: system("cls"); getchar(); if(AddToFile(p)) cout << "保存文件成功" <<endl; cout << "按任意键返回" <<endl; if(getchar()) break; case 6: system("cls"); getchar(); if(Sort(p)) cout <<"排序成功,信息如下:"<<endl; Show(p); cout << "按任意键返回" <<endl; if(getchar()) break; case 7: getchar(); if(ClearAll(p)) { system("del wj.txt"); cout << "清除完毕!并已删除文件." <<endl; } cout << "按任意键返回" <<endl; if(getchar()) break; case 8: Flag=0; break; } } return 0; }