您好,登录后才能下订单哦!
C++中怎么链表实现一个通讯录管理系统,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
具体内容如下
文件操作未写
有登录操作,复制源码需要更改登录模块的密码文件存放位置
使用VS2017编译器需要保留开头: #define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS#include "iostream"#include "cstdio"#include "fstream"#include "stdlib.h"#include "String"#include "iomanip"#include "windows.h"#define LEN 100using namespace std; using std::cin;using std::cout;using std::endl;using std::ifstream;using std::ofstream;using std::ios;using std::cerr;using std::string;using std::setw; typedef struct LNode { char num[10]; char name[20]; char telNum[12]; char qq[10]; struct LNode *next;}LNode,*LinkList; int n = 0; void InitList(LinkList &L);//初始化表void InsertLNode(LinkList &L,LNode *s);//前插法插入新结点LinkList SearchName(LinkList L);//按姓名查找LinkList SearchNum(LinkList L);//按学号查找void DelLNode(LinkList &L,LinkList p);//删除p结点void PrintLNode(LinkList p);//打印结点void PrintList(LinkList L);//打印表/*----------------系统函数----------------*/void CreateLinkList(LinkList &L);//创建链表void DelName(LinkList &L);//按姓名删除通讯录成员void DelNum(LinkList &L);//按学号删除通讯录成员void saveRecord(LinkList L);//存储信息void loadRecord(LinkList &L);//加载信息/*--------------------------------------*/void Secret();void fun();void ver();void yanshi(char *p);void clear();void header(); void menu() { LinkList L=NULL; int select; do { system("cls"); printf("\t\t\t Welcome to the address book information management system!\n\n\n"); printf("\t\t\t\t***************************************************\n"); printf("\t\t\t\t * │1.InitList 2.Add Message │ *\n"); printf("\t\t\t\t * │ │ *\n"); printf("\t\t\t\t * │3.Search Message 4.Save File │ *\n"); printf("\t\t\t\t * │ │ *\n"); printf("\t\t\t\t * │5.Sort Static 6.Load Message │ *\n"); printf("\t\t\t\t * │ │ *\n"); printf("\t\t\t\t * │7.Display Message 8.Delete Message│ *\n"); printf("\t\t\t\t * │ │ *\n"); printf("\t\t\t\t * │9.Save Message 0.Exit System │ *\n"); printf("\t\t\t\t***************************************************\n"); cout << endl; yanshi((char *)"\t\tPlease choose the mode of operation(1~8):\n");/* cout << "\t\tPlease choose the mode of operation(1~8):" << endl;*/ cin >> select; switch (select) { case 8: cout << "Please select the deletion method:\n1.Delete by student number 2.Delete by name\n" << endl; int x; cin >> x; switch (x) { case 1: DelNum(L); break; case 2: DelName(L); break; } case 6: loadRecord(L); break; case 5: break; case 4: saveRecord(L); break; case 3: clear(); cout << "Please select a search method:\n1.Find by student number 2.Find by name\n" << endl; int a; cin >> a; switch (a) { case 1: clear(); { LinkList aa = SearchNum(L); header(); PrintLNode(aa); cout << "\n\n\n成功!" << endl; system("pause"); menu(); } break; case 2: clear(); { LinkList b = SearchName(L); header(); PrintLNode(b); cout << "\n\n\n成功!" << endl; system("pause"); menu(); break; } } break; case 1: InitList(L); break; case 9: break; case 7: PrintList(L); break; case 2: CreateLinkList(L); break; case 0: cout << endl << endl << endl; cout << "The programe is over!" << endl << endl << endl; Sleep(2000); exit(0); break; } } while (select != 8);} int main() { fun(); ver();//版本信息 Secret();//密码登录 menu(); return 0;} //初始化表void InitList(LinkList & L){ L = new LNode;//申请头结点 L->next= NULL;} //插入一条信息void InsertLNode(LinkList & L, LNode *s){ s->next = L->next; L->next = s;} //按姓名查找LinkList SearchName(LinkList L){ char name[20]; cout << "请输入要查找的姓名:" << endl; cin >> name; LinkList p = L->next; while (p) { //如果找到,退出循环,返回p if (strcmp(p->name, name) == 0) break; else p = p->next; } return p;} //按学号查找LinkList SearchNum(LinkList L){ char num[10]; cout << "请输入要查找的学号:" << endl; cin >> num; LinkList p = L->next; while (p) { //如果找到,退出循环,返回p if (strcmp(p->num, num) == 0) break; else p = p->next; } return p;} //删除节点void DelLNode(LinkList &L,LinkList p){ LinkList s=NULL, q; q = L->next; //将s指向p前面的一个结点 while (q&&q!=p) { s = q; q = q->next; } s->next = q->next; delete q;} //打印一条信息void PrintLNode(LinkList p){ printf("%15s", p->num); printf("%15s", p->name); printf("%15s", p->telNum); printf("%15s\n",p->qq);} //打印通讯录void PrintList(LinkList L){ clear(); header(); LinkList p = L->next; while (p) { PrintLNode(p); p = p->next; } system("pause");} //添加信息void CreateLinkList(LinkList & L){ char ans = 'y'; n = 0; while (ans=='y'||ans=='Y') { system("cls"); LNode *p = new LNode; cout << "请输入学号:" << endl; cin >> p->num; cout << "请输入姓名:" << endl; cin >> p->name; cout << "请输入电话号码:" << endl; cin >> p->telNum; cout << "请输入QQ号:" << endl; cin >> p->qq; InsertLNode(L,p); n++; cout<<"是否继续?(Y/N)"<<endl; getchar(); ans=getchar(); } system("pause");} //按姓名删除void DelName(LinkList &L){ char name[20]; LinkList p; cout << "请输入要删除的学生姓名:" << endl; cin >> name; p = SearchName(L); if (p) { DelLNode(L,p); } system("pause");} //按学号删除void DelNum(LinkList & L){ char num[20]; LinkList p; cout << "请输入要删除的学生学号:" << endl; cin >> num; p = SearchName(L); if (p) { DelLNode(L, p); } system("pause");} //存储信息void saveRecord(LinkList L){ FILE *fp=NULL; int count = 0; if ((fp=(fopen("student.dat","wb")))==NULL) { cout << "Can't open this file!" << endl; Sleep(3000); } LinkList q = L->next; while (q) { fwrite(q, sizeof(LNode), 1, fp); count ++; q = q->next; } fclose(fp); cout << "Save the file successfully!" << endl; getchar();} //加载信息void loadRecord(LinkList & L){ FILE *fp=NULL; int count = 0; if ((fp=(fopen("student.dat", "rb"))) == NULL) { cout << "Can't open this file!" << endl; Sleep(3000); } LinkList p=NULL; while(1){ p = new LNode; if (fread(p, sizeof(LNode), 1, fp) > 0) { InsertLNode(L,p); count++; } else { break; } } fclose(fp); cout << endl << endl << "Load "<<count<<"messages!" << endl; Sleep(2200);} //控制台样式void fun() { system("color 2a"); system("title 学生通讯录信息管理系统");} //版本信息void ver(){ yanshi((char*)"\t \3\3\3\3\3\3\3欢迎使用通讯录信息管理系统\3\3\3\3\3\3\3\n\n\n\n"); cout << "\t 学生通讯录信息管理系统\n\n\n\n\n"; cout << "\t\t version 1.0\n\n\n\n\n"; cout << "\t\t xxxxxxxxx 某某某\n\n\n\n\n"; cout << "\t\t Loading......\n\n" << endl; Sleep(3000); system("cls"); } //延时输出void yanshi(char *p){ while (1) { if (*p != 0) cout << *p++; else break; Sleep(50); } } //清屏void clear(){ system("cls");} //表头void header(){ printf("%15s%15s%15s%15s\n","学号","姓名","电话","QQ");} /*--------------------------------登录模块----------------------------------*//*--------------------------------登录模块----------------------------------*//*--------------------------------登录模块----------------------------------*/struct UsrInfo//用户名的账户和密码信息{ char UsrName[20]; char Psword[20];};/*注意我的文件中用户名是一行,密码是一样。在注册的时候只需要看用户名,不需要看密码是不是相符,所以设置i为计数变量,当i不能整除2的时候是访问用户名的时候,当i整除2的时候是访问密码的时候。读入密码这一行直接跳出去就行了。*/int regest(struct UsrInfo* usr) {//注册程序 char usrname[20]; char psword[20]; strcpy_s(usrname, usr->UsrName); strcpy_s(psword, usr->Psword); string temp; int flag = 0; int i = 0; ifstream fin("E:\\Love-Study\\学生通讯录管理系统\\static.dat", ios::in);//在这个路径下读入文件 ofstream fout("E:\\Love-Study\\学生通讯录管理系统\\static.dat", ios::app);//在同一个路径下,如果注册成功则写入文件 while (std::getline(fin, temp))//每次读一行的数据进入temp中。 { i++; if (i % 2 == 0) continue;//访问的是密码这一行,跳出。 if (!strcmp(usrname, temp.c_str())) flag = 1;//flag=1说明用户名已经被注册了 } fin.close(); if (flag) { return 0;//之前有重复的账户名 } else {//没注册 fout << usrname << endl;//向文件写入注册者的用户名,然后换一行 fout << psword << endl;//写入密码,换行 fout.close(); return 1;//注册成功 }}int login(struct UsrInfo* usr) { char usrname[20]; char psword[20]; strcpy_s(usrname, usr->UsrName); strcpy_s(psword, usr->Psword); string temp1; string temp2; int existname = 0; int match = 0; int i = 0; ifstream fin("E:\\Love-Study\\学生通讯录管理系统\\static.dat", ios::in); while (std::getline(fin, temp1)) { std::getline(fin, temp2);//一次读进去两行,分别是用户名和密码 if (!strcmp(usrname, temp1.c_str())) {//有这个用户名了,接下来看看密码是不是相符的 existname = 1; if (!strcmp(psword, temp2.c_str())) {//相符 match = 1; break; } } } fin.close(); if (!existname) { return 2;//没有账户名 } else { if (match) return 1; else return 3;//用户名和密码不匹配 }}void Secret(){ clear(); cout << "1.注册 2.登录" << endl; int c; cin >> c; switch (c) { case 1: { clear(); UsrInfo test1;//用于测试注册程序的。 char urr[20], prr[20]; cout << "请输入用户名:" << endl; cin >> urr; cout << "请输入密码:" << endl; cin >> prr; strcpy(test1.UsrName, urr); strcpy(test1.Psword, prr); switch (regest(&test1)) { case 1: cout << "注册成功" << endl; system("pause"); Secret(); break; case 0: cout << "用户名已被注册,请重新选择用户名" << endl; system("pause"); Secret(); break; default: break; } break; } case 2: { clear(); UsrInfo test2;//用于测试注册程序的。 char ur[20], pr[20]; cout << "请输入用户名:" << endl; cin >> ur; cout << "请输入密码:" << endl; cin >> pr; strcpy(test2.UsrName, ur); strcpy(test2.Psword, pr); switch (login(&test2)) { case 1: cout << "登录成功" << endl; system("pause"); menu(); break; case 3: cout << "密码错误" << endl; system("pause"); Secret(); break; case 2: cout << "没有此用户名,请注册" << endl; system("pause"); Secret(); break; default: break; } } }}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。