用数据结构里面线性结构的链表实现,供大家参考,具体内容如下
文件操作未写
有登录操作,复制源码需要更改登录模块的密码文件存放位置
使用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 100 using 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)"<> 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 "< 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; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。