Teacher salary management system (C language)

【aim of design】

1. Teaching purpose

This course design is a comprehensive and comprehensive training for students after studying the "C Language Programming" course. Through the course design, students can better master the method of using C language for programming, and deepen their understanding of the characteristics of C language and the use of C language. Understand the programming and development process and strengthen hands-on skills. Its main purpose is:

(1) Further cultivate students’ C language programming ideas and deepen their understanding of basic language elements and control structures of high-level languages;

(2) Provide training on key and difficult contents in C language, independently complete programming tasks with a certain workload, and emphasize good programming style.

(3) Master the programming skills of C language and the method of debugging programs on the computer.

(4) Master common algorithms in programming.

2. Teaching requirements

(1) It is required to choose any number of topics from the given topics. Each student must complete the course design independently and cannot plagiarize from each other.

(2) After the design is completed, defend the work completed.

(3) It is required to write a detailed course design report.

(4) For programming questions, the corresponding program must be submitted, and annotated source programs must be provided that can run normally.

【demand analysis】

1. Question

Depending on the requirements, the system should contain the following information:

The information for each teacher is:

Teacher number, name, gender, unit name, home address, contact number,

Basic salary, allowances, living allowance, payable wages,

Telephone bills, water and electricity bills, rent, income tax, health fees, provident fund, total deductions, and actual wages.

According to the requirements, the functions that the system should implement include the following:

1. Teacher information processing:

(1) Enter teacher information

(2) Insert (modify) teacher information:

(3) Delete teacher information

(4) Browse teacher information

2. Teacher data processing:

(1) Enter basic data such as teachers’ basic salary, allowances, living allowances, telephone bills, water and electricity bills, rent, income tax, health expenses, provident fund, etc. according to the teacher number.

(2) Calculation of teachers’ actual wages, payable wages, and total deductions.

1. Salary payable = basic salary + allowance + living allowance;

2. Total deduction = telephone bill + water and electricity bill + rent + income tax + health fee + provident fund;

3. Actual wages = wages due – total deductions.

(3) Teacher data management

Tip: Enter new data and write the changed information to the file

(4) Teacher data query

Tip: Enter the teacher number or other information, all data information will be read out and displayed.

(5) Teachers’ comprehensive information output

Tip: Output teacher information to the screen.

 2. System

It is implemented in C language and can run on the operating system WINDOWS.

3. Operational requirements

Teacher data can be entered without restrictions.

The interface is friendly and easy to operate. For example, operations such as deletion, modification, and query can be performed based on the teacher's number or name. After the program completes the addition, deletion, modification, and query operations, the program returns to the previous menu.

It is highly fault-tolerant. For example, when entering to add teacher information, the teacher's number, phone number, gender, etc. will be verified. If an error is entered, an error message will be prompted. The program will not terminate abnormally, and the program handles the problem of duplicate names.

【Overall design】

1. System process design

According to the system requirements, design the flow chart shown in Figure 1.

4f631add2c874389b75a765bb6c254bf.png

Figure 1 - System flow

2. System module design

According to the functional design of the system, it is shown in Figure 2.

53dd9dac1a894b7fa5a815d08cc962bc.png

 Figure 2 - System Function Diagram

Main interface module : Complete the display of the system menu

Function selection module : complete the corresponding functions according to the user's selection

Add teacher information : After the user enters teacher information from the interface, the teacher data information is entered into the file.

Delete teacher information : Based on user input, determine whether to delete teacher information based on teacher number or name, or to return to the home page or exit the program.

Deleting teacher information based on teacher number : After the user inputs the teacher number to be deleted from the interface, the system queries the teacher information node in the qualified teacher information link list, outputs the node on the interface, and asks the user whether to confirm the deletion. If the user confirms, the queried linked list node will be deleted and the linked list will be stored in the file; if the user cancels, the deletion function will end and the previous menu will be returned; if the user makes an input error, the loop will continue to allow the user to choose confirm/cancel.

Deleting teacher information based on the teacher's name : After the user inputs the name of the teacher to be deleted from the interface, the system queries the teacher information node in the qualified teacher information link list, eliminates duplicate names, outputs the node on the interface, and asks the user if he is sure to delete it. . If the user confirms, the queried linked list node will be deleted and the linked list will be stored in the file; if the user cancels, the deletion function will end and the previous menu will be returned; if the user makes an input error, the loop will continue to allow the user to choose confirm/cancel.

Modify teacher information : Complete the function of judging whether to modify teacher information based on teacher number or name based on user input, or to return to the home page or exit the program.

Modify teacher information based on teacher number : After the user inputs the teacher number to be modified from the interface, the system queries the teacher information node in the qualified teacher information link list, outputs the node on the interface, and asks the user whether to confirm the modification. If the user confirms, let the user input the teacher information to be modified from the interface, modify the queried linked list node, and store the linked list into the file; if the user cancels, the modification function ends and returns to the previous menu; if the user enters If there is an error, the loop will continue to let the user choose confirm/cancel.

Modify teacher information based on teacher name : After the user inputs the teacher name to be modified from the interface, the system queries the teacher information node in the qualified teacher information link list to eliminate the impact of duplicate names, outputs the node on the interface, and asks the user if he is sure to modify it. . If the user confirms, let the user input the teacher information to be modified from the interface, modify the queried linked list node, and store the linked list into the file; if the user cancels, the modification function ends and returns to the previous menu; if the user enters If there is an error, the loop will continue to let the user choose confirm/cancel.

Query teacher information : Based on user input, determine whether to query teacher information based on teacher number or name, or output teacher information in order of teacher number, output teacher information in order of salary from low to high, or return to the home page ,exit the program.

Query teacher information based on teacher number : complete the function of the user inputting the teacher number to be queried from the interface, querying the system for qualified teacher information list, and displaying it on the interface.

Query teacher information based on teacher name : Complete the function where the user inputs the name of the teacher to be queried from the interface, queries the system for the qualified teacher information link list, and displays it on the interface.

Display teacher information by teacher number : Complete the function of displaying user input from the interface in rows or columns, and displaying all teacher information from low to high by teacher number.

Display teacher information by salary (low - > high ) : Complete the function of displaying the user input from the interface in rows or columns, and displaying all teacher information from low to high in salary.

The module outline is shown in Figure 3

d15511055c3740008949ca666f1365dc.png

 Figure 3 - A flow chart describing the general execution of each function corresponding to the menu

【detailed design】

1. Data structure design

According to the system requirements, the data that needs to be saved in the system includes teacher data information, and its structure is shown in Table 1.

Table 1 - Teacher data information

Data item name

Data item system representation

type of data

Data length

Remark

Teacher ID

id

integer

 

 

Name

name

string

20

 

gender

sex

string

5

 

company name

workPlace

string

100

 

Home address

address

string

100

 

contact number

tel

string

20

 

Basic wage

salary

floating point

 

 

allowance

benefit

floating point

 

 

Living allowance

livingAllowance

floating point

 

 

Wages due

shouldPay

floating point

 

 

telephone fee

telBill

floating point

 

 

Utility bills

waterBill

floating point

 

 

rent

rentPay

floating point

 

 

income tax

incomeTax

floating point

 

 

hygiene fee

cleanBill

floating point

 

 

provident fund

fund

floating point

 

 

Total deductions

totalDeduct

floating point

 

 

actual wages

realPay

floating point

 

 

The structure created using C language is as follows:

typedef struct Teacher{

int id;//教师号

char name[20];//姓名

char sex[5];//性别

char workPlace[100];//单位名称

char address[100];//家庭住址

char tel[20];//联系电话

float salary;//基本工资

float benefit;//津贴

float livingAllowance;//生活补贴

float  shouldPay;//应发工资

float telBill;//电话费

float warterBill;//水电费

float rentPay;//房租

float  incomeTax;//所得税

float cleanBill;//卫生费

float fund;//公积金

float  totalDeduct;//合计扣款

float realPay;//实发工资

struct Teacher *pnext;

} teacher;

2. Interface design

Main interface:

According to the system requirements, design a character interface to display the system's menu. In order to reflect the friendliness, the number corresponding to the menu item will be displayed in front of each menu.

The user enters the number in front of the menu item, the main menu disappears, enters the corresponding interface of the module, performs the corresponding operation, and returns to the main interface after completion.

 

The input interface is shown in Figure 4.

b24d39b6d85f4752a049fe0f35ffeefb.png

 Figure 4 - Input interface

The interface for adding teacher information is shown in Figure 5.

15380dcbeaf346debcbda833795596e2.png

Figure 5 - Add teacher information interface

The interface for deleting teacher information is shown in Figure 6.

391b3033af8c43a1b2c3f5dd3a97ef57.png

 Figure 6 - Delete teacher information interface

The interface for deleting teacher information based on teacher number is shown in Figure 7.

74e619b6fdea47feafc6134cd8ee97a5.png

Figure 7 - Delete teacher information interface based on teacher number

The interface for deleting teacher information based on the teacher’s name is shown in Figure 8 and Figure 9.

b39d59f9945f4d9eb908016797a3ca86.png

Figure 8 - Delete teacher information based on teacher name (with duplicate names) interface

2bd1cde995054ce48ad5a6860244943b.png

 

Figure 9 - Delete teacher information based on teacher name (no duplicate names) interface

The interface for modifying teacher information is shown in Figure 10.

3209c1b80e504710b53078a423d14e17.png

Figure 10 - Modify teacher information interface

The interface for modifying teacher information based on teacher number is shown in Figure 11.

9d6a231a4d3742d4b0b797efc00489b6.png

Figure 11 - Modify teacher information interface based on teacher number

The interface for modifying teacher information based on the teacher's name is shown in Figure 12 and Figure 13.

dea306f7a4a044d98f6d85e4d2a6a7a4.png

Figure 12 - Modify teacher information interface based on teacher name (duplicate)

c47501e0e0df47a3bf0c3ee207f7c21b.png

Figure 13 - Modify teacher information based on teacher name

The interface for querying teacher information is shown in Figure 14.

66563b187bba4a889d1522801a3dec87.png

Figure 14 - Query teacher information interface

The interface for querying teacher information based on the teacher number is shown in Figure 15.

298edd766ced44d5b55d974fab5c199e.png

Figure 15 - Interface for querying teacher information based on teacher number

The interface for querying teacher information based on the teacher’s name is shown in Figure 16.

ebd3897b39b541099a6733d15f66d3cb.png

Figure 16 - Interface for querying teacher information based on teacher name

The teacher information interface displayed by teacher number is shown in Figure 17.

4e779b158a3b481da56e7a4302570535.png

Figure 17 - Display teacher information interface by teacher number

The interface for displaying teacher information by salary (low->high) is shown in Figure 18.

fa7802581ceb4e7d817dbefb5f6233a4.png

Figure 18 - Display teacher information interface by salary (low->high)

3. Module implementation

(1)Main interface implementation

Call the printf() function to print the character information that needs to be displayed on the screen.

(2) Function selection module implementation

Accept the menu selection input from the keyboard, judge and call the corresponding function to complete its corresponding function. Implement function selection.

Corresponding function:

void menu(){

system("cls");

char ch;

printf("***************教师工资管理系统***************\n");

printf("-------------------菜单列表-------------------\n");

printf("*               1:添加教师信息               *\n");

printf("*               2.删除教师信息               *\n");

printf("*               3:修改教师信息               *\n");

printf("*               4:查询教师信息               *\n");

printf("*               0.退出程序                   *\n");

printf("**********************************************\n");

do{

printf("请选择:");

ch = getchar();

fflush(stdin);

switch(ch){

case '1':

addNode();

break;

case '2':

deleteNodeByIdOrName();

break;

case '3':

editNodeByIdOrName();

break;

case '4':

searchNodeByNameOrIdOrAll();

break;

case '0':

exit(0);

}

}while(ch!='0');

}

 

A. Add teacher information module

Module description:

The user inputs teacher information from the interface, stores the teacher information into a new linked list node, and stores the linked list storing teacher data information into a file.

Module process:

c88d1b9cf21a4a9aa63bf12e8de25761.png

Figure 19 - addNode() function

 

Corresponding function:

void addNode(){

system("cls");

teacher *pnew;

char ch;

pnew=(teacher *)malloc(sizeof(teacher));

    printf("请输入教师信息:\n");

    printf("==========================================\n");

do{

    printf("请输入教师号:");

    scanf("%d",&pnew->id);

}while(check_number(pnew->id));



    printf("请输入教师姓名:");

    scanf("%s",&pnew->name);

do{

printf("请输入教师性别:");

        scanf("%s",&pnew->sex);

}while(check_sex(pnew->sex));

    

    printf("请输入教师单位名称:");

    scanf("%s",&pnew->workPlace);

    printf("请输入教师家庭住址:");

    scanf("%s",&pnew->address);

do{

printf("请输入教师的电话号码:");

scanf("%s",&pnew->tel);

}while(check_tel(pnew->tel));    

printf("==========================================\n");

printf("请输入教师基本工资:");

scanf("%f",&pnew->salary);

printf("请输入教师的津贴:");

scanf("%f",&pnew->benefit);

printf("请输入教师的生活补贴:");

scanf("%f",&pnew->livingAllowance);

pnew->shouldPay=pnew->salary+pnew->benefit+pnew->livingAllowance;

printf("========应发工资为:%.2f=============\n",pnew->shouldPay);

printf("请输入教师的电话费:");

scanf("%f",&pnew->telBill);

        printf("请输入教师的水电费:");

scanf("%f",&pnew->waterBill);

printf("请输入教师的房租:");

scanf("%f",&pnew->rentPay);

printf("请输入教师的所得税:");

scanf("%f",&pnew->incomeTax);

printf("请输入教师的卫生费:");

scanf("%f",&pnew->cleanBill);

printf("请输入教师的公积金:");

scanf("%f",&pnew->fund);

pnew->totalDeduct=pnew->telBill+pnew->waterBill+pnew->rentPay+pnew->incomeTax+pnew->cleanBill+pnew->fund;

printf("========合计扣款为:%.2f=============\n",pnew->totalDeduct);

pnew->realPay=pnew->shouldPay-pnew->totalDeduct;

printf("==========实发工资为:%.2f=============\n",pnew->realPay);

printf("==========================================\n");

showNode(pnew);

printf("==========================================\n");

printf("确认录入该教师的信息吗(y/n):?\n");

 do{

ch = getch();

if(ch == 'n'){

printf("取消成功!\n");

printf("\n-------按任意键回到主菜单--------");

   getch();

   getchar();

   menu();

   return;

}else if(ch =='y'){

if(pHead==NULL){

pHead=pnew;

pHead->pnext=NULL;

printf("录入成功!\n");

   link_save();

   printf("按任意键回到主菜单");

getch();

getchar();

menu();

return;

}

if(pHead->id>pnew->id){

  teacher *pr=pHead;

  pHead=pnew;

  pHead->pnext=pr;

}else{

  teacher *p;

  teacher *bf;

  p=pHead;

  while(p->id<pnew->id){

bf=p;

p=p->pnext;

if(p==NULL){

bf->pnext=pnew;

pnew->pnext=NULL;

printf("录入成功!\n");

   link_save();

   printf("\n-------按任意键回到主菜单--------");

   getch();

   getchar();

   menu();

   return;

}

 }

pnew->pnext=p;

bf->pnext=pnew;

}

   printf("录入成功!\n");

   link_save();

printf("\n-------按任意键回到主菜单--------");

   getch();

   getchar();

   menu();

}else{

printf("确认录入该教师的信息吗(y/n):?\n");

}



} while(ch!='n'||ch!='y');

}

 B. Delete teacher information module

Module description:

Accept the menu selection input from the keyboard, judge and call the corresponding function to complete its corresponding function. Implement function selection.

Corresponding function:

void deleteNode(teacher *node){

char ch;

printf("------------以下是要删除的教师信息------------\n");

showNode(node);

printf("----------------------------------------------\n");

printf("确定删除该信息吗(y/n):?\n");

do{

ch = getch();

if(ch == 'n'){

printf("取消成功!\n");

printf("--------按任意键回到菜单---------\n");

   getch();

   deleteNodeByIdOrName();

   return;

}else if(ch =='y'){

if(node->id==pHead->id){

pHead=pHead->pnext;

free(node);

}else if(node->pnext==NULL){

teacher *k;

k=pHead;

while(k->pnext!=node){

k=k->pnext;

}

k->pnext=NULL;

}else{

teacher *d=pHead;

while(d->pnext!=node){

d=d->pnext;

}

d->pnext=node->pnext;

}

link_save();

printf("---------删除成功!---------------\n");

printf("--------按任意键回到菜单---------\n");

   getch();

   deleteNodeByIdOrName();

return;

}else{

printf("确定删除该信息吗(y/n):?\n");

}



} while(ch!='n'||ch!='y');  

}

 

 

        (a) Delete teacher information based on teacher number

Module description:

After the user enters the teacher number to be deleted from the interface, the system queries the teacher information node in the qualified teacher information link list, outputs the node on the interface, and asks the user whether to confirm the deletion. If the user confirms, the queried linked list node will be deleted and the linked list will be stored in the file; if the user cancels, the deletion function will end and the previous menu will be returned; if the user makes an input error, the loop will continue to allow the user to choose confirm/cancel.

Module process:

cd298ac433d142aca367789993901fc4.png

Figure 20 - deleteNodeById() function

 

Corresponding program:

find=findLinkById();

if(find!=NULL){

deleteNode(find);

}else{

printf("系统中没有找到此教师\n");

printf("--------按任意键回到菜单---------\n");

getch();

deleteNodeByIdOrName();

}

        (b) Delete teacher information based on teacher name

Module description:

The user enters the name of the teacher to be deleted from the interface, and the system queries the teacher information node in the qualified teacher information link list, eliminates duplicate name factors, outputs the node on the interface, and asks the user whether to confirm the deletion. If the user confirms, the queried linked list node will be deleted and the linked list will be stored in the file; if the user cancels, the deletion function will end and the previous menu will be returned; if the user makes an input error, the loop will continue to allow the user to choose confirm/cancel.

Module process:

a75e475c82744c49902b4598de33b692.png

Figure 21 - deleteNodeByName() function

 

Corresponding program:

printf("请输入教师姓名:");

scanf("%s",&name);

nodes=findNodesByName(name,copyLink(pHead));

if(link_length(nodes)==1){

find=findLinkByName(nodes->name);

deleteNode(find);

break;

}else if(link_length(nodes)>1){

printf("有教师重名,删除失败!\n重名结果如下:\n");

printf("==========================================\n");

showLinkByRow(nodes);

printf("==========================================\n");

printf("\n提示:请根据教师号删除\n\n");

printf("--------按任意键回到菜单---------\n");

getch();

deleteNodeByIdOrName();

break;

break;

}else{

printf("系统中没有找到此教师\n");

printf("--------按任意键回到菜单---------\n");

getch();

deleteNodeByIdOrName();

break;

}

C. Modify teacher information module

Module description:

Based on the user's input, it is determined whether to modify the teacher information based on the teacher number or name, or to return to the home page or exit the program.

Corresponding function:

void editNode(teacher *node){

char ch;

printf("------------以下是要修改的教师信息------------\n");

showNode(node);

printf("----------------------------------------------\n");

printf("确定修改该信息吗(y/n):?\n");

do{

ch = getch();

if(ch == 'n'){

printf("取消成功!\n");

printf("--------按任意键回到菜单---------\n");

   getch();

   editNodeByIdOrName();

   return;

}else if(ch =='y'){

printf("请重新输入以下信息:\n");

    printf("==========================================\n");

    printf("请输入教师姓名:");

    scanf("%s",&node->name);

    printf("请输入教师性别:");

        scanf("%s",&node->sex);

    printf("请输入教师单位名称:");

    scanf("%s",&node->workPlace);

    printf("请输入教师家庭住址:");

    scanf("%s",&node->address);

    printf("请输入教师的电话号码:");

scanf("%s",&node->tel);

printf("==========================================\n");

printf("请输入教师基本工资:");

scanf("%f",&node->salary);

printf("请输入教师的津贴:");

scanf("%f",&node->benefit);

printf("请输入教师的生活补贴:");

scanf("%f",&node->livingAllowance);

node->shouldPay=node->salary+node->benefit+node->livingAllowance;

printf("=========应发工资为:%.2f==========\n",node->shouldPay);

printf("请输入教师的电话费:");

scanf("%f",&node->telBill);

        printf("请输入教师的水电费:");

scanf("%f",&node->waterBill);

printf("请输入教师的房租:");

scanf("%f",&node->rentPay);

printf("请输入教师的所得税:");

scanf("%f",&node->incomeTax);

printf("请输入教师的卫生费:");

scanf("%f",&node->cleanBill);

printf("请输入教师的公积金:");

scanf("%f",&node->fund);

node->totalDeduct=node->telBill+node->waterBill+node->rentPay+node->incomeTax+node->cleanBill+node->fund;

printf("========合计扣款为:%.2f==========\n",node->totalDeduct);

node->realPay=node->shouldPay-node->totalDeduct;

printf("========实发工资为:%.2f===========\n",node->realPay);

printf("==========================================\n");



link_save();

printf("---------修改成功!---------------\n");

printf("--------按任意键回到菜单---------\n");

   getch();

   editNodeByIdOrName();

return;

}else{

printf("确定修改该信息吗(y/n):?\n");

}



} while(ch!='n'||ch!='y');

}

 

(a) Modify teacher information according to teacher number

Module description:

  Enter the teacher number to be modified from the interface, query the teacher information node in the qualified teacher information link list in the system, output the node on the interface, and ask the user whether to confirm the modification. If the user confirms, let the user input the teacher information to be modified from the interface, modify the queried linked list node, and store the linked list into the file; if the user cancels, the modification function ends and returns to the previous menu; if the user enters If there is an error, the loop will continue to let the user choose confirm/cancel.

Module process:

3cc1f6f8c9c14a5195b1b21f57db3a90.png

Figure 22 - editNodeById() function

 

 

Corresponding program:

find=findLinkById();

if(find!=NULL){

editNode(find);

}else{

printf("系统中没有找到此教师\n");

printf("--------按任意键回到菜单---------\n");

getch();

editNodeByIdOrName();

}

 

(b) Modify teacher information based on teacher name

Module description:

  The user inputs the name of the teacher to be modified from the interface, and the system queries the teacher information node in the qualified teacher information link list to eliminate the influence of duplicate names. The node is output on the interface and the user is asked whether he is sure to modify it. If the user confirms, let the user input the teacher information to be modified from the interface, modify the queried linked list node, and store the linked list into the file; if the user cancels, the modification function ends and returns to the previous menu; if the user enters If there is an error, the loop will continue to let the user choose confirm/cancel.

Module process:

fe995d3a395d4a66ab9254d47f97d227.png

Figure 23 - editNodeByName() function

 

Corresponding program:

printf("请输入教师姓名:");

scanf("%s",&name);

nodes=findNodesByName(name,copyLink(pHead));

if(link_length(nodes)==1){

find=findLinkByName(nodes->name);

editNode(find);

break;

}else if(link_length(nodes)>1){

printf("有教师重名,修改失败!\n重名结果如下:\n");

printf("==========================================\n");

showLinkByRow(nodes);

printf("==========================================\n");

printf("\n提示:请根据教师号修改\n\n");

printf("--------按任意键回到菜单---------\n");

getch();

editNodeByIdOrName();

break;

}else{

printf("系统中没有找到此教师\n");

printf("--------按任意键回到菜单---------\n");

getch();

editNodeByIdOrName();

break;

}

 

D. Query teacher information module

Module description:

        Based on the user's input, it is determined whether to query teacher information based on teacher number or name, or to output teacher information in order of teacher number, to output teacher information in order of salary from low to high, or to return to the homepage and exit the program.

 

Corresponding function:

teacher * findLinkById(){

int id;

printf("请输入教师号:");

scanf("%d",&id);

if(pHead==NULL){

   return NULL;

}else{

teacher *p=pHead;

while(id!=p->id){

p=p->pnext;

if(p==NULL){

   return NULL;

   }

}

return p;

}

}

teacher * findLinkByName(char *name){

if(pHead==NULL){

return NULL;

}else{

teacher *p=pHead;

while((strcmp(name,p->name))!=0){

p=p->pnext;

if(p==NULL){

   return NULL;

}

}

return p;



}

}

teacher *findNodesByName(char *name,teacher *lista){



teacher* listb;

if (lista == NULL) return NULL;

while(lista!=NULL){

if(strcmp(name,lista->name)!=0){

lista = lista->pnext;

}else {

listb = (teacher*)malloc(sizeof(teacher));

listb->id=lista->id;

strcpy(listb->name,lista->name);

strcpy(listb->sex,lista->sex);

strcpy(listb->workPlace,lista->workPlace);

strcpy(listb->address,lista->address);

strcpy(listb->tel,lista->tel);

listb->salary=lista->salary;

listb->benefit=lista->benefit;

listb->livingAllowance=lista->livingAllowance;

listb->shouldPay=lista->shouldPay;

listb->telBill=lista->telBill;

listb->waterBill=lista->waterBill;

listb->rentPay=lista->rentPay;

listb->incomeTax=lista->incomeTax;

listb->cleanBill=lista->cleanBill;

listb->fund=lista->fund;

listb->totalDeduct=lista->totalDeduct;

listb->realPay=lista->realPay;

listb->pnext = findNodesByName(name,lista->pnext);

return listb;

}

}

}

 

(a) Query teacher information based on teacher number

Module description:

Complete the function of the user inputting the teacher number to be queried from the interface, querying the qualified teacher information link list in the system, and displaying it on the interface.

Module process:

11b63926acb8432ca316e23912da1d42.png

Figure 24 – findLinkById() function

 

Corresponding function:

find=findLinkById();

if(find!=NULL){

printf("==========================================\n");

showNode(find);

printf("==========================================\n");

}else{

printf("系统中没有找到此教师\n");

}

printf("--------按任意键回到菜单---------\n");

getch();

searchNodeByNameOrIdOrAll();

break;

 

(b) Query teacher information based on teacher name

Module description:

Complete the function of the user inputting the name of the teacher to be queried from the interface, querying the system for qualified teacher information link list, and displaying it on the interface.

Module process:

d033a5b9d3194c46ac5cb9353fac640b.png

Figure 25 - findNodesByName() function

 

Corresponding function:

printf("请输入教师姓名:");

scanf("%s",&name);

find = findNodesByName(name,copyLink(pHead));

if(find!=NULL){

printf("==========================================\n");

if(link_length(find)==1){

showLink(find);

}else{

showLinkByRow(find);

}

printf("==========================================\n");

}else{

printf("系统中没有找到此教师\n");

}

printf("--------按任意键回到菜单---------\n");

getch();

searchNodeByNameOrIdOrAll();

break;

 

(c) Output teacher information in order of teacher number

Module description:

The user can choose from the interface to display in rows or columns, and to display all teacher information from low to high by teacher number.

Module process:

35373f56ba8c4d94a35d9126fe94a894.png

Figure 26 - showLinkById() function

 

Corresponding function:

void showLinkByRowOrCol(teacher *head){

char ch;

printf("请选择按行输出(1)or按列输出(2):\n");

ch = getchar();

fflush(stdin);

switch(ch){

case '1':

showLinkByRow(head);

break;

case '2':

showLink(head);

break;

case '0':

exit(0);

}



}

void showLink(teacher *head){

teacher *p = head;

while (p !=NULL){

printf("教师号:%d\n姓名:%s\n性别:%s\n单位名称:%s\n家庭住址:%s\n联系电话:%s\n"

"基本工资:%.2f\n津贴:    %.2f\n生活补贴:%.2f\n"

"应发工资:%.2f\n电话费:  %.2f\n水电费:  %.2f\n房租:    %.2f\n所得税:  %.2f\n"

"卫生费:  %.2f\n公积金:  %.2f\n合计扣款:%.2f\n实发工资:%.2f\n\n",

p->id,p->name,p->sex,p->workPlace,p->address,p->tel,p->salary,p->benefit,

p->livingAllowance,p->shouldPay,p->telBill,p->waterBill,

p->rentPay,p->incomeTax,p->cleanBill,p->fund,p->totalDeduct,p->realPay);



p = p->pnext;

}

}

 

 

(d) Output teacher information by salary (low - > high )

Module description:

The user inputs from the interface whether to display in rows or columns, copies a linked list, and uses recursive sorting to display all teacher information from low to high in the linked list.

Module process:

d361ad1f662a4990bdea102df5b21b7b.png

Figure 27 - showLinkByRealPay() function

 

Corresponding function:

teacher* merge(teacher* list1,teacher* list2){

    teacher* newList = (teacher*)malloc(sizeof(teacher));

    teacher* nHead = newList;

    teacher* nTail = newList;

    newList->realPay = 0;

    newList->pnext = NULL;

    teacher* p = list1;

    teacher* q =list2;

    while(p!=NULL && q!=NULL){

        if(p->realPay <= q->realPay){

            nTail->pnext = p;

            nTail = p;

            p=p->pnext;

        }else{

            nTail->pnext = q;

            nTail = q;

            q=q->pnext;

        }

    }

    if(p!=NULL) nTail->pnext = p;

    if(q!=NULL) nTail->pnext = q;

    return nHead->pnext;

}

teacher* sortList(teacher* head){

    if(head==NULL) return head;

    if(head->pnext==NULL) return head;

    teacher* slow = head;

    teacher* fast = head;

    teacher* pre = NULL;

    while(fast->pnext!=NULL && fast->pnext->pnext!=NULL){

        pre=slow;

        slow=slow->pnext;

        fast=fast->pnext->pnext;

    }

    if(fast->pnext!=NULL){

        pre=slow;

        slow=slow->pnext;

    }

    teacher* mid = slow;

    pre->pnext = NULL;

    teacher* L1 =sortList(head);

    teacher* L2 =sortList(mid);

    return merge(L1,L2);

}

showLinkByRowOrCol(sortList(copyLink(pHead)));

[Reading impressions]

Highlights of the program are:

  1. Efficiency: Linked lists are used to store teacher data, and additions and deletions are highly efficient.
  2. Data security: All linked list data is stored in a txt file. The first thing to do when compiling the program is to read the file. All the data that needs to be saved is placed in the file to prevent data loss.
  3. Program security: The user input information is verified during the input process, and the program exits abnormally caused by data format errors.
  4. New features: Use merge sort to output teacher information in order of teacher salary from low to high, with lower time complexity; use bubble sort to sort teacher information each time it is inserted into the linked list by teacher number.
  5. Bugs excluded except to implement basic functions:
    1. Solved the problem of deletion, modification and search when there are duplicate names in the linked list. When deleting or changing names, the original program was to find one and terminate it. Later, it was modified to find the duplicate name information and output it, and then let the user delete or change it through the teacher account.
    2. Correctness verification when entering information has been solved. The verification parameters are: teacher number (not repeated), teacher gender (male or female), phone number (requires correct format)

The disadvantages are:

  1. The function reuse rate is low. The linked list operation written is basically only for the teacher class, and it is difficult to move it to other projects.
  2. The interface is not beautiful enough. Because it is terminal output, the interface relies entirely on printf().
  3. A linked list is copied and takes up space resources. The problem of duplicate names was not considered when designing the program at the beginning, so the linked list operations were all sorted by teacher number. However, when sorting by salary, not copying the linked list will disrupt the order. If you create an additional function to sort by teacher number during design, this step is not needed.

[C program source code]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAME "teacherMsg.txt"
typedef struct Teacher{
	int id; 
	char name[20];
	char sex[5];
	char workPlace[100];
	char address[100];
	char tel[20];
	float salary;
	float benefit;
	float livingAllowance;
	float  shouldPay;
	float telBill;
	float waterBill;
	float rentPay;
	float  incomeTax;
	float cleanBill;
	float fund;
	float  totalDeduct;
	float realPay;
	struct Teacher *pnext;
} teacher;
teacher * pHead = NULL;
FILE *file;
void menu();

int check_number(int id);
int check_tel(char *s);
int check_sex(char *s); 
int link_length(teacher *head);

void read_file();
void link_save();

void showLink(teacher *head);
void showLinkByRow(teacher *head);
void showNode(teacher *node);
void showLinkByRowOrCol();

void addNode();

void deleteNodeByIdOrName();
void deleteNode(teacher *node);

void editNodeByIdOrName();
void editNode(teacher *node);

void searchNodeByNameOrIdOrAll();
teacher* findLinkById();
teacher* findLinkByName(char *name);
teacher *findNodesByName(char *name,teacher *head);

teacher* merge(teacher* list1,teacher* list2);
teacher* sortList(teacher* head);
teacher* copyLink(teacher* head);

int main(){
	read_file();
	menu();
}

void menu(){
	system("cls");
	char ch;
	printf("***************教师工资管理系统***************\n");
	printf("-------------------菜单列表-------------------\n");
	printf("*               1:添加教师信息               *\n");
	printf("*               2.删除教师信息               *\n");
	printf("*               3:修改教师信息               *\n");
	printf("*               4:查询教师信息               *\n");
	printf("*               0.退出程序                   *\n");
	printf("**********************************************\n");
	do{
		printf("请选择:");
		ch = getchar();
		fflush(stdin);
		switch(ch){
			case '1':
				addNode();
				break;
			case '2':
				deleteNodeByIdOrName();
				break;
			case '3':
				editNodeByIdOrName();
				break;
			case '4':
				searchNodeByNameOrIdOrAll();
				break;
			case '0':
				exit(0);
		}
	}while(ch!='0');
}

int check_number(int id){
	teacher *p; 
	if(pHead==NULL){
		return 0;
	}else{
		p=pHead;
		while(p->id!=id){
			p=p->pnext;
			if(p==NULL)
			return 0;
		}
		printf("教师号已存在!\n");
		return 1;
	}
}
int check_tel(char *s){
	if(strlen(s) == 11&&s[0]=='1'&&((s[1]=='3') || (s[1]=='5') || (s[1]=='7') || (s[1]=='8'))){
       return 0;   	
    }else{
    	printf("手机号码不合法!\n"); 
    	return 1;
	}
}
int check_sex(char *s){
	if(strcmp(s,"男")==0||strcmp(s,"女")==0){
       return 0;   	
    }else{
    	printf("性别格式有误!\n"); 
    	return 1;
	}
}
int link_length(teacher *head){
    int length =  0;
    teacher *n = head;
     while(NULL != n){
        ++length;
        n = n->pnext;
    }
     return length;
}

void read_file(){
	file=fopen(FILENAME,"r+");
	if(file==NULL){
		file=fopen(FILENAME,"w");
		if(file==NULL)
		printf("\n\t文件创建失败!");
	}else{
		
		int k,len=0,c=0;
		while((k=fgetc(file))!=EOF){
			if(k=='\n'){
				if(c!=1)
				len++;
			}
			c++;
		}
	
		int id;
		char name[20];
		char sex[5];
		char workPlace[100];
		char address[100];
		char tel[20];
		float salary;
		float benefit;
		float livingAllowance;
		float  shouldPay;
		float telBill;
		float waterBill;
		float rentPay;
		float  incomeTax;
		float cleanBill;
		float fund;
		float  totalDeduct;
		float realPay;
		teacher *p=(teacher *)malloc(sizeof(teacher));
		rewind(file);
		int i; 
		for(i=0;i<len;i++){
			fscanf(file,"%d%s%s%s%s%s%f%f%f%f%f%f%f%f%f%f%f%f",
			&id,name,sex,workPlace,address,tel,&salary,&benefit,&livingAllowance,
			&shouldPay,&telBill,&waterBill,&rentPay,&incomeTax,&cleanBill,&fund,&totalDeduct,&realPay);
			teacher *pn=(teacher *)malloc(sizeof(teacher));
			pn->id=id;
			strcpy(pn->name,name);
			strcpy(pn->sex,sex);
			strcpy(pn->workPlace,workPlace);
			strcpy(pn->address,address);
			strcpy(pn->tel,tel);
			pn->salary=salary;
			pn->benefit=benefit;
			pn->livingAllowance=livingAllowance;
			pn->shouldPay=shouldPay;
			pn->telBill=telBill;
			pn->waterBill=waterBill;
			pn->rentPay=rentPay;
			pn->incomeTax=incomeTax;
			pn->cleanBill=cleanBill;
			pn->fund=fund;
			pn->totalDeduct=totalDeduct;
			pn->realPay=realPay;
			pn->pnext=NULL;
			p->pnext=pn;
			p=pn;
	
			if(pHead==NULL){
				pHead=p;
			}
					
		}
		
	}
	fclose(file);
}
void link_save(){
	
	teacher *p = pHead;
    if ((file = fopen(FILENAME, "w")) == NULL){
        printf("文件创建失败!\n");
        exit(1);
    }
    while(p!=NULL){
    	 fprintf(file,"%d %s %s %s %s %s %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
				p->id,p->name,p->sex,p->workPlace,p->address,p->tel,p->salary,p->benefit,
				p->livingAllowance,p->shouldPay,p->telBill,p->waterBill,p->rentPay,
				p->incomeTax,p->cleanBill,p->fund,p->totalDeduct,p->realPay);
         p=p->pnext;
	}
    	fclose(file);
}

void showLink(teacher *head){
	teacher *p = head;
	while (p !=NULL){
		printf("教师号:%d\n姓名:%s\n性别:%s\n单位名称:%s\n家庭住址:%s\n联系电话:%s\n"
				"基本工资:%.2f\n津贴:    %.2f\n生活补贴:%.2f\n"
			"应发工资:%.2f\n电话费:  %.2f\n水电费:  %.2f\n房租:    %.2f\n所得税:  %.2f\n"
			"卫生费:  %.2f\n公积金:  %.2f\n合计扣款:%.2f\n实发工资:%.2f\n\n",
			p->id,p->name,p->sex,p->workPlace,p->address,p->tel,p->salary,p->benefit,
			p->livingAllowance,p->shouldPay,p->telBill,p->waterBill,
			p->rentPay,p->incomeTax,p->cleanBill,p->fund,p->totalDeduct,p->realPay);	
				
		p = p->pnext;
	}
}
void showLinkByRow(teacher *head){
	teacher *t = head;
	teacher *p = head;
	int i;
	for(i=0;i<100;i++)printf("-");
	printf("\n");
	printf("教师号\t姓名\t性别\t单位\t家庭住址\t电话号\n");
	while (t !=NULL){
		printf("%d\t%-6s\t%2s\t%4s\t%4s\t\t%11s\t\n",
			t->id,t->name,t->sex,t->workPlace,t->address,t->tel);			
		t = t->pnext;
	}
	
	for(i=0;i<100;i++)printf("-");
	printf("\n");
	printf("教师号\t姓名\t基本工资 津贴   生活补贴 电话费  水电费\t房租  所得税  卫生费  公积金  应付工资  合计扣款  实付工资\n");
	while (p !=NULL){
		printf("%-8d%-8s%-9.1f%-7.1f%-9.1f%-8.1f%-7.1f%-6.1f%-8.1f%-8.1f%-8.1f%-10.1f%-10.1f%-.1f\n",
			p->id,p->name,p->salary,p->benefit,
			p->livingAllowance,p->shouldPay,p->telBill,p->waterBill,
			p->rentPay,p->incomeTax,p->cleanBill,p->fund,p->totalDeduct,p->realPay);	
				
		p = p->pnext;
	}
	for(i=0;i<100;i++)printf("-");
	printf("\n");
}
void showNode(teacher *node){
	
	    printf("教师号:%d\n姓名:%s\n性别:%s\n单位名称:%s\n家庭住址:%s\n联系电话:%s\n"
				"基本工资:%.2f\n津贴:    %.2f\n生活补贴:%.2f\n"
			"应发工资:%.2f\n电话费:  %.2f\n水电费:  %.2f\n房租:    %.2f\n所得税:  %.2f\n"
			"卫生费:  %.2f\n公积金:  %.2f\n合计扣款:%.2f\n实发工资:%.2f\n\n",
			node->id,node->name,node->sex,node->workPlace,node->address,node->tel,node->salary,node->benefit,
			node->livingAllowance,node->shouldPay,node->telBill,node->waterBill,
			node->rentPay,node->incomeTax,node->cleanBill,node->fund,node->totalDeduct,node->realPay);
	
}
void showLinkByRowOrCol(teacher *head){
	char ch;
		printf("请选择按行输出(1)or按列输出(2):\n");
		ch = getchar();
		fflush(stdin);
		switch(ch){
			case '1':
				showLinkByRow(head);
				break;
			case '2':
				showLink(head);
				break;
			case '0':
				exit(0);
		}

}

void addNode(){
	system("cls");
	teacher *pnew;
	char ch;
	pnew=(teacher *)malloc(sizeof(teacher));
	    printf("请输入教师信息:\n");
	    printf("==========================================\n");
	do{
	    printf("请输入教师号:");
	    scanf("%d",&pnew->id);
	}while(check_number(pnew->id));
	
	    printf("请输入教师姓名:");
	    scanf("%s",&pnew->name);
	do{
		printf("请输入教师性别:");
        scanf("%s",&pnew->sex);
	}while(check_sex(pnew->sex));
	    
	    printf("请输入教师单位名称:");			
	    scanf("%s",&pnew->workPlace);
	    printf("请输入教师家庭住址:");
	    scanf("%s",&pnew->address);
		do{
			printf("请输入教师的电话号码:");
			scanf("%s",&pnew->tel); 
		}while(check_tel(pnew->tel));    
		printf("==========================================\n");
		printf("请输入教师基本工资:");
		scanf("%f",&pnew->salary);
		printf("请输入教师的津贴:");
		scanf("%f",&pnew->benefit);
		printf("请输入教师的生活补贴:");			
		scanf("%f",&pnew->livingAllowance);
		pnew->shouldPay=pnew->salary+pnew->benefit+pnew->livingAllowance;
		printf("==========应发工资为:%.2f=============\n",pnew->shouldPay); 
		printf("请输入教师的电话费:");
		scanf("%f",&pnew->telBill);
        printf("请输入教师的水电费:");
		scanf("%f",&pnew->waterBill);
		printf("请输入教师的房租:");
		scanf("%f",&pnew->rentPay);
		printf("请输入教师的所得税:");
		scanf("%f",&pnew->incomeTax);
		printf("请输入教师的卫生费:");
		scanf("%f",&pnew->cleanBill);
		printf("请输入教师的公积金:");
		scanf("%f",&pnew->fund);
		pnew->totalDeduct=pnew->telBill+pnew->waterBill+pnew->rentPay+pnew->incomeTax+pnew->cleanBill+pnew->fund;
		printf("==========合计扣款为:%.2f=============\n",pnew->totalDeduct);
		pnew->realPay=pnew->shouldPay-pnew->totalDeduct;	
		printf("==========实发工资为:%.2f=============\n",pnew->realPay);
		printf("==========================================\n");
		showNode(pnew);
		printf("==========================================\n");	
		printf("确认录入该教师的信息吗(y/n):?\n");
	 do{	
			ch = getch(); 
				if(ch == 'n'){
					printf("取消成功!\n");
					printf("\n-------按任意键回到主菜单--------");
				  	getch();
				  	getchar();
				  	menu();
				  	return;
				}else if(ch =='y'){
					if(pHead==NULL){
						pHead=pnew;
						pHead->pnext=NULL;
						printf("录入成功!\n");
					  	link_save();
					  	printf("按任意键回到主菜单");
						getch();
						getchar();
						menu();
						return;
					}
					if(pHead->id>pnew->id){
					 	teacher *pr=pHead;
					 	pHead=pnew;
					 	pHead->pnext=pr;
					}else{
					 	teacher *p;
					 	teacher *bf;
					 	p=pHead;
					 	while(p->id<pnew->id){
							bf=p;
							p=p->pnext;
							if(p==NULL){
								bf->pnext=pnew;
								pnew->pnext=NULL;
								printf("录入成功!\n");
						  		link_save();
						  		printf("\n-------按任意键回到主菜单--------");
							  	getch();
							  	getchar();
							  	menu();
							  	return;
							}
						 }
						pnew->pnext=p;
						bf->pnext=pnew;
					}
				  	printf("录入成功!\n");
				  	link_save();
					printf("\n-------按任意键回到主菜单--------");
				  	getch();
				  	getchar();
				  	menu();
				}else{
					printf("确认录入该教师的信息吗(y/n):?\n");
				}
			
		} while(ch!='n'||ch!='y');
}

void deleteNode(teacher *node){
	char ch;
	printf("------------以下是要删除的教师信息------------\n");
	showNode(node);
	printf("----------------------------------------------\n");
	printf("确定删除该信息吗(y/n):?\n");
	do{
		ch = getch();
			if(ch == 'n'){
				printf("取消成功!\n");
				printf("--------按任意键回到菜单---------\n");
			  	getch();
			  	deleteNodeByIdOrName();
			  	return;
			}else if(ch =='y'){
				if(node->id==pHead->id){
					pHead=pHead->pnext;
					free(node);
				}else if(node->pnext==NULL){
					teacher *k;
					k=pHead;
					while(k->pnext!=node){
						k=k->pnext;
					}
					k->pnext=NULL;	
				}else{
					teacher *d=pHead;
					while(d->pnext!=node){
						d=d->pnext;
					}
					d->pnext=node->pnext;
				}
				link_save();
				printf("---------删除成功!---------------\n");
				printf("--------按任意键回到菜单---------\n");
		  		getch();
		  		deleteNodeByIdOrName();
				return;
			}else{
				printf("确定删除该信息吗(y/n):?\n");
			}
		
		} while(ch!='n'||ch!='y');  
} 
void deleteNodeByIdOrName(){
	system("cls");
	char ch;
	printf("***************教师工资管理系统***************\n");
	printf("-----------------删除教师信息-----------------\n");
	printf("*              1.根据教师号删除              *\n");
	printf("*              2.根据教师姓名删除            *\n");
	printf("*              3.回到主菜单                  *\n");
	printf("*              0.退出程序                    *\n");
	printf("**********************************************\n");
	do{
		printf("请选择:");
		ch = getchar();
		fflush(stdin);
		switch(ch){
			teacher *find;
			teacher *nodes;
			char name[20]; 
			case '1':
				find=findLinkById();
				if(find!=NULL){
					deleteNode(find);
				}else{
					printf("系统中没有找到此教师\n");
					printf("--------按任意键回到菜单---------\n");
					getch();
					deleteNodeByIdOrName();
				}
				break;
			case '2':
				printf("请输入教师姓名:");
				scanf("%s",&name);
				nodes=findNodesByName(name,copyLink(pHead));
				if(link_length(nodes)==1){
					find=findLinkByName(nodes->name);
					deleteNode(find);
					break;
				}else if(link_length(nodes)>1){
					printf("有教师重名,删除失败!\n重名结果如下:\n");
					printf("==========================================\n");
					showLinkByRow(nodes);
					printf("==========================================\n");
					printf("\n提示:请根据教师号删除\n\n");
					printf("--------按任意键回到菜单---------\n");
					getch();
					deleteNodeByIdOrName();
					break;
					break;
				}else{
					printf("系统中没有找到此教师\n");
					printf("--------按任意键回到菜单---------\n");
					getch();
					deleteNodeByIdOrName();
					break;
				}
			case '3':
				menu();
				break; 
			case '0':
				exit(0);
				break;
		}
	}while(ch!='0');
}

void editNode(teacher *node){
	char ch;
	printf("------------以下是要修改的教师信息------------\n");	
	showNode(node);
	printf("----------------------------------------------\n");
	printf("确定修改该信息吗(y/n):?\n");
	do{
		ch = getch();
			if(ch == 'n'){
				printf("取消成功!\n");
				printf("--------按任意键回到菜单---------\n");
			  	getch();
			  	editNodeByIdOrName();
			  	return;
			}else if(ch =='y'){
				printf("请重新输入以下信息:\n");
			    printf("==========================================\n");
			    printf("请输入教师姓名:");
			    scanf("%s",&node->name);
			    printf("请输入教师性别:");
		        scanf("%s",&node->sex);
			    printf("请输入教师单位名称:");			
			    scanf("%s",&node->workPlace);
			    printf("请输入教师家庭住址:");
			    scanf("%s",&node->address);
			    printf("请输入教师的电话号码:");
				scanf("%s",&node->tel); 
				printf("==========================================\n");
				printf("请输入教师基本工资:");
				scanf("%f",&node->salary);
				printf("请输入教师的津贴:");
				scanf("%f",&node->benefit);
				printf("请输入教师的生活补贴:");			
				scanf("%f",&node->livingAllowance);
				node->shouldPay=node->salary+node->benefit+node->livingAllowance;
				printf("==========应发工资为:%.2f=============\n",node->shouldPay); 
				printf("请输入教师的电话费:");
				scanf("%f",&node->telBill);
		        printf("请输入教师的水电费:");
				scanf("%f",&node->waterBill);
				printf("请输入教师的房租:");
				scanf("%f",&node->rentPay);
				printf("请输入教师的所得税:");
				scanf("%f",&node->incomeTax);
				printf("请输入教师的卫生费:");
				scanf("%f",&node->cleanBill);
				printf("请输入教师的公积金:");
				scanf("%f",&node->fund);
				node->totalDeduct=node->telBill+node->waterBill+node->rentPay+node->incomeTax+node->cleanBill+node->fund;
				printf("==========合计扣款为:%.2f=============\n",node->totalDeduct);
				node->realPay=node->shouldPay-node->totalDeduct;	
				printf("==========实发工资为:%.2f=============\n",node->realPay);
				printf("==========================================\n");
				
				link_save();
				printf("---------修改成功!---------------\n");
				printf("--------按任意键回到菜单---------\n");
		  		getch();
		  		editNodeByIdOrName();
				return;
			}else{
				printf("确定修改该信息吗(y/n):?\n");
			}
		
		} while(ch!='n'||ch!='y'); 
}
void editNodeByIdOrName(){
	system("cls");
	char ch;
	printf("***************教师工资管理系统***************\n");
	printf("----------------修改教师信息-----------------\n");
	printf("*              1.根据教师号修改              *\n");
	printf("*              2.根据教师姓名修改            *\n");
	printf("*              3.回到主菜单                  *\n");
	printf("*              0.退出程序                    *\n");
	printf("**********************************************\n");
	do{
		printf("请选择:");
		ch = getchar();
		fflush(stdin);
		switch(ch){
			teacher *find;
			teacher *nodes;
			char name[20];
			case '1':
				find=findLinkById();
				if(find!=NULL){
					editNode(find);
				}else{
					printf("系统中没有找到此教师\n");
					printf("--------按任意键回到菜单---------\n");
					getch();
					editNodeByIdOrName();
				}
				break;
			case '2':
				printf("请输入教师姓名:");
				scanf("%s",&name);
				nodes=findNodesByName(name,copyLink(pHead));
				if(link_length(nodes)==1){
					find=findLinkByName(nodes->name);
					editNode(find);
					break;
				}else if(link_length(nodes)>1){
					printf("有教师重名,修改失败!\n重名结果如下:\n");
					printf("==========================================\n");
					showLinkByRow(nodes);
					printf("==========================================\n");
					printf("\n提示:请根据教师号修改\n\n");
					printf("--------按任意键回到菜单---------\n");
					getch();
					editNodeByIdOrName();
					break;
				}else{
					printf("系统中没有找到此教师\n");
					printf("--------按任意键回到菜单---------\n");
					getch();
					editNodeByIdOrName();
					break;
				}
			case '3':
				menu();
				break; 
			case '0':
				exit(0);
				break;
		}
	}while(ch!='0');
}

void searchNodeByNameOrIdOrAll(){
	system("cls");
	char ch;
	printf("***************教师工资管理系统***************\n");
	printf("-----------------查询教师信息-----------------\n");
	printf("*              1.根据教师号查询              *\n");
	printf("*              2.根据教师姓名查询            *\n");
	printf("*              3.按教师号显示教师信息        *\n");
	printf("*              4.按工资(低->高)显示教师信息  *\n");
	printf("*              5.回到主菜单                  *\n");
	printf("*              0.退出程序                    *\n");
	printf("**********************************************\n");
	do{
		printf("请选择:");
		ch = getchar();
		fflush(stdin);
		switch(ch){
			teacher *find;
			char name[20];
			case '1':
				find=findLinkById();
				if(find!=NULL){
					printf("==========================================\n");
					showNode(find);
					printf("==========================================\n");
				}else{
					printf("系统中没有找到此教师\n");	
				}
				printf("--------按任意键回到菜单---------\n");
				getch();
				searchNodeByNameOrIdOrAll();
				break;
			case '2':
				printf("请输入教师姓名:");
				scanf("%s",&name);
				find = findNodesByName(name,copyLink(pHead));
				if(find!=NULL){
					printf("==========================================\n");
					if(link_length(find)==1){
						showLink(find);
					}else{
						showLinkByRow(find);
					}
					printf("==========================================\n");
				}else{
					printf("系统中没有找到此教师\n");
				} 
				printf("--------按任意键回到菜单---------\n");
				getch();
				searchNodeByNameOrIdOrAll();
				break;
			case '3':
				showLinkByRowOrCol(pHead);
				printf("--------按任意键回到菜单---------\n");
				getch();
				searchNodeByNameOrIdOrAll();
				break;
			case '4':
				showLinkByRowOrCol(sortList(copyLink(pHead)));
				printf("--------按任意键回到菜单---------\n");
				getch();
				searchNodeByNameOrIdOrAll();
				break;	
			case '5':
				menu();
				break; 
			case '6':
				exit(0);
				break;
		}
	}while(ch!='0');
}
teacher * findLinkById(){
	int id; 
	printf("请输入教师号:");
	scanf("%d",&id);
	if(pHead==NULL){
		  	return NULL;
	}else{
		teacher *p=pHead;
		while(id!=p->id){
			p=p->pnext;
			if(p==NULL){
			  	return NULL;
		  	}
		}
		return p;
	}
}
teacher * findLinkByName(char *name){
	if(pHead==NULL){
		return NULL;
	}else{
		teacher *p=pHead;
		while((strcmp(name,p->name))!=0){
			p=p->pnext;
			if(p==NULL){
		  		return NULL;
			}
		}
		return p;
	
	}
}
teacher *findNodesByName(char *name,teacher *lista){

	teacher* listb;
	if (lista == NULL) return NULL;
	while(lista!=NULL){
		if(strcmp(name,lista->name)!=0){
			lista = lista->pnext;
		}else {
			listb = (teacher*)malloc(sizeof(teacher));
			listb->id=lista->id;
			strcpy(listb->name,lista->name);
			strcpy(listb->sex,lista->sex);
			strcpy(listb->workPlace,lista->workPlace);
			strcpy(listb->address,lista->address);
			strcpy(listb->tel,lista->tel);
			listb->salary=lista->salary;
			listb->benefit=lista->benefit;
			listb->livingAllowance=lista->livingAllowance;
			listb->shouldPay=lista->shouldPay;
			listb->telBill=lista->telBill;
			listb->waterBill=lista->waterBill;
			listb->rentPay=lista->rentPay;
			listb->incomeTax=lista->incomeTax;
			listb->cleanBill=lista->cleanBill;
			listb->fund=lista->fund;
			listb->totalDeduct=lista->totalDeduct;
			listb->realPay=lista->realPay;	
			listb->pnext = findNodesByName(name,lista->pnext);
			return listb;
		}	
	}
}

teacher* merge(teacher* list1,teacher* list2){
    teacher* newList = (teacher*)malloc(sizeof(teacher));
    teacher* nHead = newList;
    teacher* nTail = newList;
    newList->realPay = 0;
    newList->pnext = NULL;
    teacher* p = list1;
    teacher* q =list2;
    while(p!=NULL && q!=NULL){
        if(p->realPay <= q->realPay){
            nTail->pnext = p;
            nTail = p;
            p=p->pnext;
        }else{
            nTail->pnext = q;
            nTail = q;
            q=q->pnext;
        }
    }
    if(p!=NULL) nTail->pnext = p;
    if(q!=NULL) nTail->pnext = q;
    return nHead->pnext;
}
teacher* sortList(teacher* head){
    if(head==NULL) return head;
    if(head->pnext==NULL) return head;
    teacher* slow = head;
    teacher* fast = head;
    teacher* pre = NULL;
    while(fast->pnext!=NULL && fast->pnext->pnext!=NULL){
        pre=slow;
        slow=slow->pnext;
        fast=fast->pnext->pnext;
    }
    if(fast->pnext!=NULL){
        pre=slow;
        slow=slow->pnext;
    }
    teacher* mid = slow;
    pre->pnext = NULL;
    teacher* L1 =sortList(head);
    teacher* L2 =sortList(mid);
    return merge(L1,L2);
}
teacher* copyLink(teacher* lista){
	teacher* listb;
	if (lista == NULL){
		return NULL;
	}else {
		listb = (teacher*)malloc(sizeof(teacher));
		listb->id=lista->id;
		strcpy(listb->name,lista->name);
		strcpy(listb->sex,lista->sex);
		strcpy(listb->workPlace,lista->workPlace);
		strcpy(listb->address,lista->address);
		strcpy(listb->tel,lista->tel);
		listb->salary=lista->salary;
		listb->benefit=lista->benefit;
		listb->livingAllowance=lista->livingAllowance;
		listb->shouldPay=lista->shouldPay;
		listb->telBill=lista->telBill;
		listb->waterBill=lista->waterBill;
		listb->rentPay=lista->rentPay;
		listb->incomeTax=lista->incomeTax;
		listb->cleanBill=lista->cleanBill;
		listb->fund=lista->fund;
		listb->totalDeduct=lista->totalDeduct;
		listb->realPay=lista->realPay;	
		listb->pnext = copyLink(lista->pnext);
		return listb;
	}
}

 

 

Guess you like

Origin blog.csdn.net/shengshanlaolin_/article/details/130756514
Recommended