C++ linked list to achieve the address book management system

  • 2020-06-23 01:16:07
  • OfStack

With the data structure inside the linear structure of the linked list to achieve, for your reference, the specific content is as follows

File operation not written

There is a login operation, copy the source code needs to change the password file location of the login module

Using the VS2017 compiler, you need to keep the beginning: #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);// Initialize the table 
void InsertLNode(LinkList &L,LNode *s);// Insert a new node forward 
LinkList SearchName(LinkList L);// Search by name 
LinkList SearchNum(LinkList L);// Search by student number 
void DelLNode(LinkList &L,LinkList p);// delete p node 
void PrintLNode(LinkList p);// Print the node 
void PrintList(LinkList L);// Print the table 
/*---------------- The system function ----------------*/
void CreateLinkList(LinkList &L);// Create a list 
void DelName(LinkList &L);// Delete address book members by name 
void DelNum(LinkList &L);// Delete address book member by student number 
void saveRecord(LinkList L);// Store information 
void loadRecord(LinkList &L);// Loading information 
/*--------------------------------------*/
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 successful !" << endl;
 system("pause");
 menu();
 }
 break;
 case 2:
 clear();
 {
 LinkList b = SearchName(L);
 header();
 PrintLNode(b);
 cout << "\n\n\n successful !" << 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();// Version information 
 Secret();// Password to login 
 menu();
 return 0;
}
 
// Initialize the table 
void InitList(LinkList & L)
{
 L = new LNode;// Application head 
 L->next= NULL;
}
 
// insert 1 Pieces of information 
void InsertLNode(LinkList & L, LNode *s)
{
 s->next = L->next;
 L->next = s;
}
 
// Search by name 
LinkList SearchName(LinkList L)
{
 char name[20];
 cout << " Please enter the name you are looking for: " << endl;
 cin >> name;
 LinkList p = L->next;
 while (p) {
 // If found, exit the loop , return p
 if (strcmp(p->name, name) == 0)
 break;
 else
 p = p->next;
 }
 return p;
}
 
// Search by student number 
LinkList SearchNum(LinkList L)
{
 char num[10];
 cout << " Please enter the student number you are looking for: " << endl;
 cin >> num;
 LinkList p = L->next;
 while (p) {
 // If found, exit the loop , return p
 if (strcmp(p->num, num) == 0)
 break;
 else
 p = p->next;
 }
 return p;
}
 
// Remove nodes 
void DelLNode(LinkList &L,LinkList p)
{
 LinkList s=NULL, q;
 q = L->next;
 // will s Point to the p In front of 1 A node 
 while (q&&q!=p) {
 s = q;
 q = q->next;
 }
 s->next = q->next;
 delete q;
}
 
// print 1 Pieces of information 
void PrintLNode(LinkList p)
{
 printf("%15s", p->num);
 printf("%15s", p->name);
 printf("%15s", p->telNum);
 printf("%15s\n",p->qq);
}
 
// Print address book 
void PrintList(LinkList L)
{
 clear();
 header();
 LinkList p = L->next;
 while (p) {
 PrintLNode(p);
 p = p->next;
 }
 system("pause");
}
 
// Add information 
void CreateLinkList(LinkList & L)
{
 char ans = 'y';
 n = 0;
 while (ans=='y'||ans=='Y') {
 system("cls");
 LNode *p = new LNode;
 cout << " Please enter your student number: " << endl;
 cin >> p->num;
 cout << " Please enter your name: " << endl;
 cin >> p->name;
 cout << " Please enter the telephone number: " << endl;
 cin >> p->telNum;
 cout << " Please enter the QQ No. : " << endl;
 cin >> p->qq;
 InsertLNode(L,p);
 n++;
 cout<<" Do you want to continue? (Y/N)"<<endl;
 getchar();
 ans=getchar();
 }
 system("pause");
}
 
// Delete by name 
void DelName(LinkList &L)
{
 char name[20];
 LinkList p;
 cout << " Please enter the name of the student you want to delete: " << endl;
 cin >> name;
 p = SearchName(L);
 if (p) {
 DelLNode(L,p);
 }
 system("pause");
}
 
// Delete by student number 
void DelNum(LinkList & L)
{
 char num[20];
 LinkList p;
 cout << " Please enter the student id you want to delete: " << endl;
 cin >> num;
 p = SearchName(L);
 if (p) {
 DelLNode(L, p);
 }
 system("pause");
}
 
// Store information 
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();
}
 
// Loading information 
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);
}
 
// Console style 
void fun() {
 system("color 2a");
 system("title  Student address book information management system ");
}
 
// Version information 
void ver()
{
 yanshi((char*)"\t \3\3\3\3\3\3\3 Welcome to the Address Book information management system \3\3\3\3\3\3\3\n\n\n\n");
 cout << "\t    Student address book information management system \n\n\n\n\n";
 cout << "\t\t  version 1.0\n\n\n\n\n";
 cout << "\t\t  xxxxxxxxx  XXX \n\n\n\n\n";
 cout << "\t\t  Loading......\n\n" << endl;
 Sleep(3000);
 system("cls");
 
}
 
// Delay output 
void yanshi(char *p)
{
 while (1) {
 if (*p != 0)
 cout << *p++;
 else
 break;
 Sleep(50);
 }
 
}
 
// Clear the screen 
void clear()
{
 system("cls");
}
 
// header 
void header()
{
 printf("%15s%15s%15s%15s\n"," Student id "," The name "," The phone ","QQ");
}
 
/*-------------------------------- The login module ----------------------------------*/
/*-------------------------------- The login module ----------------------------------*/
/*-------------------------------- The login module ----------------------------------*/
struct UsrInfo// Username account and password information 
{
 char UsrName[20];
 char Psword[20];
};
/*
 Note that the username in my file is 1 Okay, the password is 1 The sample. You only need to see the user name when you register, you don't need to see if the password matches, so set it i Is the counting variable, when i Not divisible 2
 When you access the user name, when i aliquot 2 "Is the time to access the password. Read in the password here 1 Just jump out of the row. 
*/
int regest(struct UsrInfo* usr) {// The registration procedure 
 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\\ Student address book management system \\static.dat", ios::in);// Read the file under this path 
 ofstream fout("E:\\Love-Study\\ Student address book management system \\static.dat", ios::app);// In the same 1 Write to the file if the registration is successful 
 while (std::getline(fin, temp))// Every time reading 1 Row data entry temp In the. 
 {
 i++;
 if (i % 2 == 0) continue;// I'm accessing the password here 1 Okay, jump out. 
 if (!strcmp(usrname, temp.c_str())) flag = 1;//flag=1 Indicates that the user name has been registered 
 }
 fin.close();
 if (flag) {
 return 0;// There was a duplicate account name 
 }
 else {// Didn't register 
 fout << usrname << endl;// Writes the subscriber's user name to the file, and then changes 1 line 
 fout << psword << endl;// Write the password and wrap 
 fout.close();
 return 1;// Registered successfully 
 }
}
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\\ Student address book management system \\static.dat", ios::in);
 while (std::getline(fin, temp1))
 {
 std::getline(fin, temp2);//1 Read two lines, one for username and the other for password 
 if (!strcmp(usrname, temp1.c_str())) {// Now that you have this username, let's see if the password matches 
 existname = 1;
 if (!strcmp(psword, temp2.c_str())) {// consistent 
 match = 1;
 break;
 }
 }
 }
 fin.close();
 if (!existname) {
 return 2;// No account name 
 }
 else {
 if (match) return 1;
 else return 3;// The username and password do not match 
 }
}
void Secret()
{
 clear();
 cout << "1. registered  2. The login " << endl;
 int c;
 cin >> c;
 switch (c) {
 case 1:
 {
 clear();
 UsrInfo test1;// Used to test the registration program. 
 char urr[20], prr[20];
 cout << " Please enter user name: " << endl;
 cin >> urr;
 cout << " Please enter your password: " << endl;
 cin >> prr;
 strcpy(test1.UsrName, urr);
 strcpy(test1.Psword, prr);
 switch (regest(&test1))
 {
 case 1:
 cout << " Registered successfully " << endl;
 system("pause");
 Secret();
 break;
 case 0:
 cout << " The user name has been registered, please select it again " << endl;
 system("pause");
 Secret();
 break;
 default:
 break;
 }
 break;
 }
 case 2:
 {
 clear();
 UsrInfo test2;// Used to test the registration program. 
 char ur[20], pr[20];
 cout << " Please enter user name: " << endl;
 cin >> ur;
 cout << " Please enter your password: " << endl;
 cin >> pr;
 strcpy(test2.UsrName, ur);
 strcpy(test2.Psword, pr);
 switch (login(&test2))
 {
 case 1:
 cout << " Login successful " << endl;
 system("pause");
 menu();
 break;
 case 3:
 cout << " Password mistake " << endl;
 system("pause");
 Secret();
 break;
 case 2:
 cout << " Do not have this username, please register " << endl;
 system("pause");
 Secret();
 break;
 default:
 break;
 }
 }
 }
}

Related articles: