Teacher information management system

Posted by 303tech on Sun, 06 Mar 2022 01:04:15 +0100

[this article is the author's final course design assignment, so it is long. The author's programming ability is weak, and many codes need to be improved. For the first time, I hope you can forgive me and forgive me; those who have optimization opinions are welcome to share with you in the comment area! I hope this article can help students in need!

Note: This article is long and the complete code is at the end of the article. Students who do not want to read the text can directly click the directory to jump!]  

1. Purpose and requirements:

Purpose:

(1) Familiar with and consolidate the basic concepts and knowledge of C programming language.

(2) Cultivate students' ability to study independently and think independently, learn to find data and be good at analyzing data.

(3) Cultivate students' ability to independently design a complete system and independently debug programs.

requirement:

(1) Design a main function and several sub functions, and each sub function completes a relatively independent sub function.

(2) When the program is running, first check the password and then display the menu. And can call the corresponding function according to the menu.

Functional requirements of password program section:

1) prompt the user to enter a password.

2) after the user enters the password, if the password is correct, the user will be prompted to pass and follow-up procedures can be executed; Otherwise, it will not pass.

3) limit the number of user's password input (e.g. repeated input for 3 times is wrong), and then automatically exit the system.

(3) When displaying data, one page cannot be displayed, and it can be displayed in pages.

2. Main contents:

Teacher information includes teacher number, name, gender, age, educational background, professional title, salary, address, telephone number, etc. (teacher number is not repeated). Try to design a teacher information management system to provide the following functions:

(1) The system works in menu mode.

(2) Teacher information input function.

(3) Teacher information deletion function.

(4) Teacher information browsing function.

(5) Query and sorting function: (at least one query method)

1) query by teacher number.

2) query by professional title, etc.

catalogue

1, Curriculum design purpose

1.1 be familiar with and consolidate the basic concepts and knowledge of C programming language.

1.2 cultivate students' ability to study independently and think independently, learn to find data and be good at analyzing data.

1.3 cultivate students' ability to independently design a complete system and independently debug programs.

2, Course design content

2.1 the system works in menu mode 

2.2 teacher information entry function

2.3 teacher information deletion function

2.4 teacher information browsing function 

2.5 query function

2.6 sorting function

3, Curriculum design requirements

3.1 design a main function and multiple sub functions, and each sub function completes a relatively independent sub function.

3.2 when the program is running, first check the password and then display the menu. And can call the corresponding function according to the menu.

3.3 when displaying data, one page cannot be displayed, and it can be displayed in pages.

4, Analysis and implementation of curriculum design

4.1 data structure

4.2 initialize teacher information system

4.3 teacher information entry function

4.4 teacher information deletion function

4.5 teacher information browsing function

Teacher information inquiry function

4.7 teacher information sorting function

4.8 file saving function

4.9 file reading function

5, System commissioning and testing

5.1 start interface

5.2 login operation

5.3 start menu

5.4 [ 1 ] input function

5.5 [2] delete function

5.6 [3] browsing function

5.7 [4] query by teacher number

5.8 [ 5 ] query by professional title

5.9 [6] sort by teacher number

5.10 [7] sort by name

5.11 [8] saving documents

5.12 [0] close menu

6, Complete code

Familiar with and consolidate the basic concepts and knowledge of C programming language.

Cultivate students' ability to study independently and think independently, learn to find data and be good at analyzing data.

Cultivate students' ability to independently design a complete system and independently debug programs.

Teacher information includes teacher number, name, gender, age, educational background, professional title, salary, address, telephone number, etc. (teacher number is not repeated). Try to design a teacher information management system to provide the following functions:

The system works in menu mode 

Teacher information entry function

Teacher information deletion function

Teacher information browsing function 

Query function

2.5.1 query by teacher number

2.5.2 query by professional title

Sorting function

2.6.1 sort by teacher number

2.6.2 sorting by name

Design a main function and several sub functions, and each sub function completes a relatively independent sub function.

When the program is running, first check the password and then display the menu. And can call the corresponding function according to the menu.

Functional requirements of password program section:

3.2.1 prompt the user to enter a password.

3.2.2 after the user enters the password, if the password is correct, the user will be prompted to pass and follow-up procedures can be executed; Otherwise, it will not pass.

3.2.3 limit the number of user's password input (e.g. repeated input for 3 times is wrong), and then automatically exit the system.

When displaying data, one page cannot be displayed, and it can be displayed in pages.

Because the teacher system needs to save and read files when it is in normal use, the function of saving and reading files is added on the basis of realizing the functions required by the curriculum design.

Figure 1 mind map

data structure

The data structure is set as the structure Teacher, and most of the data types contained are character type. Although the age and salary can choose integer and floating-point type, for the convenience of input and unified format, all data element types are set as character type array.

This project chooses the form of linked list as the storage structure, so it sets the structure of a node and the linked list structure connecting each node.

typedef struct Teacher  //Teacher structure
{
    char number[20];    //Teacher number
    char name[15];  //name
    char sex[5];    //Gender
    char age[5];    //Age
    char degree[10];    //education
    char title[10]; //title
    char income[10];   //wages
    char add[20];   //address
    char tel[15];   //Telephone
}Teacher;

typedef struct Node //Teacher's node
{
    struct Teacher teacher;
    struct Node* next;
}Node;

typedef struct List //Teacher's linked list
{
    Node* first; //Head node to facilitate sorting operation
    Node* last;
    int sum;    //Total number of Teachers
}List;

Initialize teacher information system

Define a List variable T in the main function, generate an empty header node through the initialization function, and then read the existing content in the file.

//Initialize teacher system
void Init(List* T)
{
    T->first = T->last = (Node*)malloc(sizeof(Node));
    T->first->next = NULL;
    T->sum = 0;
    Read(T);    //Read in file contents during initialization
    cout << "Please press enter to enter the system" << endl;
    getchar();  //If this line is commented out, this function directly jumps to the next operation (in the main function)
    system("cls");  //Clear screen
}

Teacher information entry function

When entering information, the while loop is used to facilitate the entry of multiple teacher information. When the entry is completed, enter 0 to exit the entry operation. At the same time, sum will count the number of teachers, which can facilitate the operation of subsequent functions.

//Input information
void Insert(List* T)
{
    Node* p, * q;
    p = T->first;
    while (p->next != NULL)
    {
        p = p->next;
    }

    while (1)
    {
        q = (Node*)malloc(sizeof(Node));
        cout << "-----------------------------------Input information------------------------------------" << endl;
        cout << "                Please enter the following information separately,Or enter 0 and enter to select return:" << endl;
        cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
        cin >> q->teacher.number;
        if (strcmp(q->teacher.number, "0") == 0)
        {
            break;
        }

        cin >> q->teacher.name >> q->teacher.sex >> q->teacher.age >> q->teacher.degree >> q->teacher.title >> q->teacher.income >> q->teacher.add >> q->teacher.tel;
        q->next = NULL;
        p->next = T->last = q;
        p = q;
        T->sum++;
    }
}

Teacher information deletion function

When sum equals 0, it indicates that there is no teacher information in the system, so the prompt statement is returned. When the system is not empty, the user will be prompted to enter the teacher number to be deleted for deletion. If the teacher number to be deleted does not exist in the system, the user will be prompted. When the deletion operation is successful, sum --, update the number of teachers in the system.

//Delete operation
void Delete(List* T)
{
    Node* p, * q;
    char n[20];
    p = T->first->next;
    if (T->sum == 0)
    {
        cout << "Wrong! No teacher information can be deleted! return!" << endl;
        return;
    }

    cout << "Please enter the teacher number to delete:";
    cin >> n;
    while (p!= NULL)
    {
        if (strcmp(p->teacher.number, n) == 0)
        {
            break;
        }
        p = p->next;
    }
    if (p != NULL)
    {
        q = T->first;
        while (q->next != p)
        {
            q = q->next;
        }
        q->next = q->next->next;
        free(p);
        T->sum--;
        cout << "Delete succeeded!" << endl;
    }
    else
    {
        cout << "There is no such person found, unable to delete!" << endl;
        return;
    }

}

Teacher information browsing function

According to the requirements of course design, when there is too much data displayed, it needs to be displayed in pages. Assume that there is only one page displayed in [maxo] according to the user's requirements, and then there is only one page displayed in [maxo]; When there is more than one page on the first page, prompt "next page" and "return"; On the last page, prompt "previous page" and "back"; Prompt "previous", "next" and "back" in the middle.

//Pageable browsing function
void Browser(List* T)
{
    int i = 0, j = 0, k = 0, _page = 0;
    char ch;
    Node* page[20];
    Node* p = T->first->next;
    Node* q;
    page[0] = T->first->next;//
    q = (Node*)malloc(sizeof(Node*));

    while (p != NULL)
    {
        p = p->next;
        i++;
        j = 10 - i;
        if (j == 0 && p != NULL)
        {
            j++;
            page[j] = p;
            _page++;
            //system("cls");
            i = 0;//
        }
    }
    i = 1;
    cout << "----------------------------------Browse menu------------------------------------" << endl;
    while (i)//
    {
        j = 10;//
        q = page[k];
        cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
        while (q != NULL && j > 0)//
        {
            j--;
            cout << q->teacher.number << "\t" << q->teacher.name << "\t" << q->teacher.sex << "\t" << q->teacher.age <<
                "\t" << q->teacher.degree << "\t" << q->teacher.title << "\t" << q->teacher.income << "\t" << q->teacher.add << "\t" << q->teacher.tel << endl << endl;
            q = q->next;
        }
        cout << "------------------------------------------------------------------------------" << endl;
        if (_page == 0)
        {
            cout << "Input[ o]Return to the previous menu." << endl;
            cin >> ch;
            break;
        }
        else if (k > 0 && k < _page)//
        {
            cout << "Input[<]Select view previous page and enter[ o]Return to the previous menu,Input[>]Select view next page." << endl;
            cin >> ch;
            switch (ch)
            {
            case '<':
                k--;
                break;
            case 'o':
                i = 0;
                break;
            case '>':
                k++;
                break;
            }
        }

        else if (k == _page)
        {
            cout << "Input[<]Select view previous page,Input[ o]Return to the previous menu." << endl;
            cin >> ch;
            switch (ch)
            {
            case '<':
                k--;
                break;
            case 'o':
                i = 0;
                break;
            }
        }
        else if (k == 0)
        {
            cout << "Input[>]Select view next page,Input[ o]Return to the previous menu." << endl;
            cin >> ch;
            switch (ch)
            {
            case 'o':
                i = 0;
                break;
            case '>':
                k++;
                break;
            }
        }
        system("cls");
    }
    //cout << "------------------------------------------------------------------------------" << endl;
}

Teacher information query function

There are two query methods: by teacher number or professional title. The query process is highly similar. In order to save space, only one complete code is released, and the other only gives the replacement content.

Here, the method of sequential traversal is used for query, and the teacher numbers are compared one by one. If the match is successful, the teacher information will be returned, otherwise the user will be prompted that the search fails. Where the header file < string h> strcmp function in. (similarly, just change p - > teacher.number in line 16 of the code to p - > teacher.title to realize the function of querying by professional title)

//Query by teacher number
void Check_Num(List* T)
{
    char num[20];
    Node* p = T->first;
    cout << "Please enter the teacher number to query:";
    cin >> num;

    if (p == NULL)
    {
        cout << "The system is empty and there is no teacher information!" << endl;
        return;
    }
    while (p != NULL)
    {
        if (strcmp(p->teacher.number, num) == 0)
        {
            cout << "---------------------------------------Query result of teacher number-----------------------------------------" << endl;
            cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
            cout << p->teacher.number << "\t" << p->teacher.name << "\t" << p->teacher.sex << "\t" << p->teacher.age << "\t" << p->teacher.degree << "\t"
                << p->teacher.title << "\t" << p->teacher.income << "\t" << p->teacher.add << "\t" << p->teacher.tel << endl;
            break;
        }
        p = p->next;
    }
    if (p == NULL)
    {
        cout << "The queried teacher information does not exist!" << endl;
        return;
    }
}
//Query by professional title
void Check_Title(List* T)
{
    char t[10];
    Node* p = T->first;
    cout << "Please enter the professional title to query:";
    cin >> t;

    if (p == NULL)
    {
        cout << "The system is empty and there is no teacher information!";
        return;
    }
    while (p != NULL)
    {
        if (strcmp(p->teacher.title, t) == 0)
        {
            cout << "---------------------------------------Title Query Results-----------------------------------------" << endl;
            cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
            cout << p->teacher.number << "\t" << p->teacher.name << "\t" << p->teacher.sex << "\t" << p->teacher.age << "\t" << p->teacher.degree <<
                "\t" << p->teacher.title << "\t" << p->teacher.income << "\t" << p->teacher.add << "\t" << p->teacher.tel << endl;
            //break;
        }
        p = p->next;
    }
    if (p == NULL)
    {
        //Cout < < "query teacher information does not exist!"<< endl;
        return;
    }
}

Teacher information sorting function

Because the linked list storage method is adopted, the quick sorting method is selected. The sorting process is roughly shown in the figure below

Figure 2 Schematic diagram of sorting

Like the query function, the sorting function also has two sorting methods: by teacher number and name. The sorting process is also highly similar, so only one complete function is given. (just change StrCmp (s - > teacher. Number, P - > next - > teacher. Number) > 0 in line 20 of the code to StrCmp (s - > teacher. Name, P - > next - > teacher. Name) > 0 to realize the function of sorting by name)

//Sort by teacher number
void Sort_Num(List* T)
{
    if (T->sum == 0 || T->sum == 1)
    {
        cout << "No teacher information or only one information, no sorting!" << endl;
        return;
    }
    Node* s = T->first->next;
    Node* q = s->next;
    T->last = s;
    T->last->next = NULL;

    while (q != NULL)
    {
        s = q;
        q = q->next;

        Node* p = T->first;
        while (p->next != NULL && strcmp(s->teacher.number, p->next->teacher.number) > 0)
        {
            p = p->next;
        }
        if (p->next == NULL)
        {
            T->last = s;
        }

        s->next = p->next;
        p->next = s;
    }
}
//Sort by name
void Sort_Name(List* T)
{
    if (T->sum == 0 || T->sum == 1)
    {
        cout << "No teacher information or only one information, no sorting!" << endl;
        return;
    }
    Node* s = T->first->next;
    Node* q = s->next;
    T->last = s;
    T->last->next = NULL;

    while (q != NULL)
    {
        s = q;
        q = q->next;

        Node* p = T->first;
        while (p->next != NULL && strcmp(s->teacher.name, p->next->teacher.name) > 0)
        {
            p = p->next;
        }
        if (p->next == NULL)
        {
            T->last = s;
        }

        s->next = p->next;
        p->next = s;
    }
}

Save file function

Save all the data in the List variable T to the "teacher information management system. txt" file for easy reading in the next operation. After saving successfully, the information of successful saving will be displayed to the user. The saving operation adopts overwrite saving, because the file content will be read at the beginning of initialization, so there is no need to worry about data loss.

//Save file
void Save(List* T)
{
    ofstream outfile;
    Node* p = T->first;

    if (p == NULL)
    {
        cout << "The system is empty!" << endl;
        return;
    }
    else
    {
        p = p->next;
    }
    outfile.open("Teacher information management system.txt");
    while (p != NULL)
    {
        outfile << p->teacher.number << " " << p->teacher.name << " " << p->teacher.sex << " " << p->teacher.age << " " << p->teacher.degree << " " <<
            p->teacher.title << " " << p->teacher.income << " " << p->teacher.add << " " << p->teacher.tel << endl;
        p = p->next;
    }
    cout << "----------File saved successfully!----------" << endl;
    outfile.close();
}

Read file function

The read file is read by default during initialization and the teacher information is imported. Therefore, in order to prevent duplication of information, the file read function is not displayed in the function menu.

//read file
void Read(List* T)
{
    ifstream infile;

    Node* p, * q;
    p = T->first;
    while (p->next != NULL)
    {
        p = p->next;
    }
    infile.open("Teacher information management system.txt");

    while (1)
    {
        q = (Node*)malloc(sizeof(Node));
        infile >> q->teacher.number;
        if (infile.peek() == EOF)
            break;

        infile >> q->teacher.name >> q->teacher.sex >> q->teacher.age >> q->teacher.degree >>
            q->teacher.title >> q->teacher.income >> q->teacher.add >> q->teacher.tel;

        q->next = NULL;
        p->next = T->last = q;
        p = q;
        T->sum++;
    }
    cout << "----------File read succeeded!----------" << endl;
    infile.close();
}

Start interface

When the program is running, the start interface is displayed as shown in the figure. Press enter to enter the next operation

Figure 3

Login operation

(1) the login password given in the code is 123456. When the password is entered incorrectly, as shown in Figure 4, the user will be prompted to have several login opportunities.

Figure 4

(2) When all four opportunities are used up and the password is still wrong, exit the system and the following interface will be displayed

Figure 5

(3) When the password is correct, prompt to enter the next operation

Figure 6

opening menu

When the password is entered correctly, enter the menu, as shown in Figure 7. Each function has a corresponding selection number. Enter a number and press enter to enter the corresponding function.

Figure 7

[1] Input function

When the entry is completed, operate according to the system prompt, enter [0] and press enter to end the entry function, re-enter the menu and select subsequent operations. If you are not sure whether the entry is successful, you can enter [3] to browse.

Figure 8

Figure 9 , Figure 10

[2] Delete function

Enter the teacher number to be deleted and press enter. If the deleted teacher number exists in the system, the deletion operation can be completed.

Figure 11

Figure 12 , Figure 13 , before deletion

Figure 14 , Figure 15 , after deletion

If the deleted teacher number does not exist in the system, the user will be prompted to check that there is no such person.

Figure 16

[3] Browsing function

One page displays the information of ten teachers. When there is only one page, prompt the user to enter [o] to return to the menu; When there is more than one page on the first page, prompt "next page" and "return"; On the last page, prompt "previous page" and "back"; Prompt "previous", "next" and "back" in the middle.

There are just 11 teachers read from the file, so it is displayed on two pages.

Figure 17 , Figure 18 , browsing information

[4] Query by teacher number

If the teacher to be queried is in the system, return the teacher information.

Figure 19

If it does not exist, the user will be prompted that it does not exist.

Figure 20

[5] Query by professional title

When the information exists, as shown in Figure 21

Figure 21

When the professional title does not exist, as shown in Figure 22, return to the menu directly

Figure 22

[6] Sort by teacher number

Sorted by teacher number, as shown in Figure 23 and figure 24

Figure 23 , figure 24 after sorting by teacher number

[7] Sort by name

Sorted by name, as shown in figures 25 and 26

Figure 25 # figure 26 after sorting by name

[8] Save file

Figure 27

[0] close menu

When entering [0], close the menu and exit the system, and the following interface is displayed:

Figure 28

   

#include<iostream>
#include<string.h>
#include<assert.h>
#include<fstream>

using namespace std;

typedef struct Teacher  //Teacher structure
{
    char number[20];    //Teacher number
    char name[15];  //name
    char sex[5];    //Gender
    char age[5];    //Age
    char degree[10];    //education
    char title[10]; //title
    char income[10];   //wages
    char add[20];   //address
    char tel[15];   //Telephone
}Teacher;

typedef struct Node //Teacher's node
{
    struct Teacher teacher;
    struct Node* next;
}Node;

typedef struct List //Teacher's linked list
{
    Node* first; //Head node to facilitate sorting operation
    Node* last;
    int sum;    //Total number of Teachers
}List;

void Read(List* T);

//Initialize teacher system
void Init(List* T)
{
    T->first = T->last = (Node*)malloc(sizeof(Node));
    T->first->next = NULL;
    T->sum = 0;
    Read(T);    //Read in file contents during initialization
    cout << "Please press enter to enter the system" << endl;
    getchar();  //If this line is commented out, this function directly jumps to the next operation (in the main function)
    system("cls");  //Clear screen
}

//Display interface at the beginning
void Begin()
{
    cout << "Current time:";
    system("date/t");   //Displays the current date
    //system ( "time/t");   // Current moment
    cout << "*********************************************" << endl;
    cout << "* ----------------------------------------- *" << endl;
    cout << "*|                                         |*" << endl;
    cout << "*|                                         |*" << endl;
    cout << "*|       Welcome to the teacher information management system!         |*" << endl;
    cout << "*|                                         |*" << endl;
    cout << "*|           ---------------               |*" << endl;
    cout << "*|                                         |*" << endl;
    cout << "* ----------------------------------------- *" << endl;
    cout << "*********************************************" << endl;
    cout << endl;
    cout << "Please press enter to log in" << endl;
    getchar();  //If this line is commented out, this function directly jumps to the next operation (in the main function)
    system("cls");  //Clear screen
}

//Display interface at the end
void End()
{
    system("cls");
    cout << "Current time:";
    system("date/t");
    cout << "*****************************************************" << endl;
    cout << "* ------------------------------------------------- *" << endl;
    cout << "*|                                                 |*" << endl;
    cout << "*|                                                 |*" << endl;
    cout << "*|     Thank you for visiting the teacher information management system!look forward to seeing you next time!      |*" << endl;
    cout << "*|                                                 |*" << endl;
    cout << "*|                                                 |*" << endl;
    cout << "* ------------------------------------------------- *" << endl;
    cout << "*****************************************************" << endl;
    cout << endl;
}

//Input information
void Insert(List* T)
{
    Node* p, * q;
    p = T->first;
    while (p->next != NULL)
    {
        p = p->next;
    }

    while (1)
    {
        q = (Node*)malloc(sizeof(Node));
        cout << "-----------------------------------Input information------------------------------------" << endl;
        cout << "                Please enter the following information separately,Or enter 0 and enter to select return:" << endl;
        cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
        cin >> q->teacher.number;
        if (strcmp(q->teacher.number, "0") == 0)
        {
            break;
        }

        cin >> q->teacher.name >> q->teacher.sex >> q->teacher.age >> q->teacher.degree >> q->teacher.title >> q->teacher.income >> q->teacher.add >> q->teacher.tel;
        q->next = NULL;
        p->next = T->last = q;
        p = q;
        T->sum++;
    }
}

//Delete operation
void Delete(List* T)
{
    Node* p, * q;
    char n[20];
    p = T->first->next;
    if (T->sum == 0)
    {
        cout << "Wrong! No teacher information can be deleted! return!" << endl;
        return;
    }

    cout << "Please enter the teacher number to delete:";
    cin >> n;
    while (p!= NULL)
    {
        if (strcmp(p->teacher.number, n) == 0)
        {
            break;
        }
        p = p->next;
    }
    if (p != NULL)
    {
        q = T->first;
        while (q->next != p)
        {
            q = q->next;
        }
        q->next = q->next->next;
        free(p);
        T->sum--;
        cout << "Delete succeeded!" << endl;
    }
    else
    {
        cout << "There is no such person found, unable to delete!" << endl;
        return;
    }

}

//Pageable browsing function
void Browser(List* T)
{
    int i = 0, j = 0, k = 0, _page = 0;
    char ch;
    Node* page[20];
    Node* p = T->first->next;
    Node* q;
    page[0] = T->first->next;//
    q = (Node*)malloc(sizeof(Node*));

    while (p != NULL)
    {
        p = p->next;
        i++;
        j = 10 - i;
        if (j == 0 && p != NULL)
        {
            j++;
            page[j] = p;
            _page++;
            //system("cls");
            i = 0;//
        }
    }
    i = 1;
    cout << "----------------------------------Browse menu------------------------------------" << endl;
    while (i)//
    {
        j = 10;//
        q = page[k];
        cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
        while (q != NULL && j > 0)//
        {
            j--;
            cout << q->teacher.number << "\t" << q->teacher.name << "\t" << q->teacher.sex << "\t" << q->teacher.age <<
                "\t" << q->teacher.degree << "\t" << q->teacher.title << "\t" << q->teacher.income << "\t" << q->teacher.add << "\t" << q->teacher.tel << endl << endl;
            q = q->next;
        }
        cout << "------------------------------------------------------------------------------" << endl;
        if (_page == 0)
        {
            cout << "Input[ o]Return to the previous menu." << endl;
            cin >> ch;
            break;
        }
        else if (k > 0 && k < _page)//
        {
            cout << "Input[<]Select view previous page and enter[ o]Return to the previous menu,Input[>]Select view next page." << endl;
            cin >> ch;
            switch (ch)
            {
            case '<':
                k--;
                break;
            case 'o':
                i = 0;
                break;
            case '>':
                k++;
                break;
            }
        }

        else if (k == _page)
        {
            cout << "Input[<]Select view previous page,Input[ o]Return to the previous menu." << endl;
            cin >> ch;
            switch (ch)
            {
            case '<':
                k--;
                break;
            case 'o':
                i = 0;
                break;
            }
        }
        else if (k == 0)
        {
            cout << "Input[>]Select view next page,Input[ o]Return to the previous menu." << endl;
            cin >> ch;
            switch (ch)
            {
            case 'o':
                i = 0;
                break;
            case '>':
                k++;
                break;
            }
        }
        system("cls");
    }
    //cout << "------------------------------------------------------------------------------" << endl;
}

//Query by teacher number
void Check_Num(List* T)
{
    char num[20];
    Node* p = T->first;
    cout << "Please enter the teacher number to query:";
    cin >> num;

    if (p == NULL)
    {
        cout << "The system is empty and there is no teacher information!" << endl;
        return;
    }
    while (p != NULL)
    {
        if (strcmp(p->teacher.number, num) == 0)
        {
            cout << "---------------------------------------Query result of teacher number-----------------------------------------" << endl;
            cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
            cout << p->teacher.number << "\t" << p->teacher.name << "\t" << p->teacher.sex << "\t" << p->teacher.age << "\t" << p->teacher.degree << "\t"
                << p->teacher.title << "\t" << p->teacher.income << "\t" << p->teacher.add << "\t" << p->teacher.tel << endl;
            break;
        }
        p = p->next;
    }
    if (p == NULL)
    {
        cout << "The queried teacher information does not exist!" << endl;
        return;
    }
}

//Query by professional title
void Check_Title(List* T)
{
    char t[10];
    Node* p = T->first;
    cout << "Please enter the professional title to query:";
    cin >> t;

    if (p == NULL)
    {
        cout << "The system is empty and there is no teacher information!";
        return;
    }
    while (p != NULL)
    {
        if (strcmp(p->teacher.title, t) == 0)
        {
            cout << "---------------------------------------Title Query Results-----------------------------------------" << endl;
            cout << "Teacher number\t full name\t Gender\t Age\t education\t title\t wages\t address\t Telephone" << endl;
            cout << p->teacher.number << "\t" << p->teacher.name << "\t" << p->teacher.sex << "\t" << p->teacher.age << "\t" << p->teacher.degree <<
                "\t" << p->teacher.title << "\t" << p->teacher.income << "\t" << p->teacher.add << "\t" << p->teacher.tel << endl;
            //break;
        }
        p = p->next;
    }
    if (p == NULL)
    {
        //Cout < < "query teacher information does not exist!"<< endl;
        return;
    }
}

//Sort by teacher number
void Sort_Num(List* T)
{
    if (T->sum == 0 || T->sum == 1)
    {
        cout << "No teacher information or only one information, no sorting!" << endl;
        return;
    }
    Node* s = T->first->next;
    Node* q = s->next;
    T->last = s;
    T->last->next = NULL;

    while (q != NULL)
    {
        s = q;
        q = q->next;

        Node* p = T->first;
        while (p->next != NULL && strcmp(s->teacher.number, p->next->teacher.number) > 0)
        {
            p = p->next;
        }
        if (p->next == NULL)
        {
            T->last = s;
        }

        s->next = p->next;
        p->next = s;
    }
}

//Sort by name
void Sort_Name(List* T)
{
    if (T->sum == 0 || T->sum == 1)
    {
        cout << "No teacher information or only one information, no sorting!" << endl;
        return;
    }
    Node* s = T->first->next;
    Node* q = s->next;
    T->last = s;
    T->last->next = NULL;

    while (q != NULL)
    {
        s = q;
        q = q->next;

        Node* p = T->first;
        while (p->next != NULL && strcmp(s->teacher.name, p->next->teacher.name) > 0)
        {
            p = p->next;
        }
        if (p->next == NULL)
        {
            T->last = s;
        }

        s->next = p->next;
        p->next = s;
    }
}

//Save file
void Save(List* T)
{
    ofstream outfile;
    Node* p = T->first;

    if (p == NULL)
    {
        cout << "The system is empty!" << endl;
        return;
    }
    else
    {
        p = p->next;
    }
    outfile.open("Teacher information management system.txt");
    while (p != NULL)
    {
        outfile << p->teacher.number << " " << p->teacher.name << " " << p->teacher.sex << " " << p->teacher.age << " " << p->teacher.degree << " " <<
            p->teacher.title << " " << p->teacher.income << " " << p->teacher.add << " " << p->teacher.tel << endl;
        p = p->next;
    }
    cout << "----------File saved successfully!----------" << endl;
    outfile.close();
}

//read file
void Read(List* T)
{
    ifstream infile;

    Node* p, * q;
    p = T->first;
    while (p->next != NULL)
    {
        p = p->next;
    }
    infile.open("Teacher information management system.txt");

    while (1)
    {
        q = (Node*)malloc(sizeof(Node));
        infile >> q->teacher.number;
        if (infile.peek() == EOF)
            break;

        infile >> q->teacher.name >> q->teacher.sex >> q->teacher.age >> q->teacher.degree >>
            q->teacher.title >> q->teacher.income >> q->teacher.add >> q->teacher.tel;

        q->next = NULL;
        p->next = T->last = q;
        p = q;
        T->sum++;
    }
    cout << "----------File read succeeded!----------" << endl;
    infile.close();
}

//Password login function
bool Log_in()
{
    int n = 3;
    char key[15];
    char password[15] = { "123456" };
    cout << "Please enter the login password:";
    cin >> key;
    while (n > 0 && strcmp(key, password) != 0)
    {
        cout << "You have" << n << "Once, please re-enter the correct password:";
        cin >> key;
        n--;
    }
    system("cls");
    if (n <= 0)
    {
        cout << "The opportunity has been used up. The program ends!" << endl;
        return false;
    }
    else
    {
        cout << "The password is correct, please continue to select!" << endl;
        return true;
    }
}

int main()
{
    List T;
    int select = 1; //Control the progress of the cycle
    Begin();    //Display start interface
    //Password login
    int L = Log_in();
    if (L == 0)
    {
        return 0;
    }
    getchar();
    //Initialize the linked list and load the file information
    Init(&T);
    //Function selection menu
    while (select)
    {
        cout << "***********************************************" << endl;
        cout << "* ------------------menu--------------------- *" << endl;
        cout << "*|                                           |*" << endl;
        cout << "*|   [0]Close menu     [1] Input function         |*" << endl;
        cout << "*|   [2]Delete function     [3] Browsing function         |*" << endl;
        cout << "*|   [4]Query by teacher number [5] query by professional title       |*" << endl;
        cout << "*|   [6]Sort by teacher number [7] sort by name       |*" << endl;
        cout << "*|   [8]Save file                           |*" << endl;
        cout << "*|                                           |*" << endl;
        cout << "* ------------------------------------------- *" << endl;
        cout << "***********************************************" << endl;
        cout << "Please select:";
        cin >> select;

        if (select == 0)
        {
            break;
        }
        switch (select)
        {
        case 1:
            Insert(&T);
            break;
        case 2:
            Delete(&T);
            break;
        case 3:
            Browser(&T);
            break;
        case 4:
            Check_Num(&T);
            break;
        case 5:
            Check_Title(&T);
            break;
        case 6:
            Sort_Num(&T);
            break;
        case 7:
            Sort_Name(&T);
            break;
        case 8:
            Save(&T);
            break;
        case 9:
            Read(&T);
            break;
        }//switch
    }//while
    End();  //Display end interface
    return 0;
}

Topics: C C++