C語言鏈表實現工資管理系統
本文實例為大傢分享瞭C語言鏈表實現工資管理系統的具體代碼,供大傢參考,具體內容如下
自己的作業,分享一下,自己為瞭調試方便,又多加入瞭一些功能
題目;建立工資管理系統,對職工工資的相關信息進行管理。職工工資相關信息包括職工工號,職工姓名,月份,每月工資和年度總工資等,具體要求如下;
1、建立該系統的存儲結構
2、錄入職工某個月的工資
3、查找某個職工某個月的工資
4、修改某個職工某個月的工資
5、刪除每個職工的工資相關信息
6、統計某個職工年度總工資
7、對職工的月工資或年度總工資進行排名
源碼
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct worker { char no[12]; //職工工號 char name[40]; //姓名 int month[12]; //月份 float gz[12]; //月工資 float total; //年度總工資 struct worker *next; //指向下一節點的指針 } Worker; //相關的函數聲明 Worker *CreateList(Worker *L, int n); //根據輸入的職工人數,批量創建節點 void DeleteList(Worker *L, char n[]); //刪除節點 void LIstSearch(Worker *L, char n[]); //查找職工工資信息 void InsertInfo(Worker *L); //插入職工工資信息 void SearchMonthSalary(Worker *L, char n[], int m); //查找某個職工的某個月的工資 void Input(Worker *p, int i); //為節點的數據域賦值 void Print(Worker *L); //輸出整個鏈表的數據 void Modify(Worker *L, char n[], int m, float s); //修改某個職工的工資 void menu(); //工資管理系統的菜單 void Save(Worker *L); //將職工的工資信息保存至文件 void Bubble_sort(Worker *L); //冒泡排序實現對鏈表節點的排序 void StatiTotal(Worker *L,char n[]); //統計某個職工年度總工資 //根據輸入的職工人數,批量創建節點 Worker *CreateList(Worker *L, int n) //n為輸入的職工人數 { int i; for (i = 0; i < n; i++) { Worker *p; //將新生成的節點插入到鏈表中 p = NULL; p = (Worker *)malloc(sizeof(Worker)); Input(p, i); //為節點的數據域賦值 p->next = L->next; L->next = p; } return L; } void DeleteList(Worker *L, char n[]) //按姓名刪除職工信息 { int i; Worker *p = L->next, *pre = L; //定義p指針指向頭節點的指向,定義pre指向頭節點,pre始終指向p的前驅節點 if (p == NULL) printf("數據為空,無法刪除!"); else { while (strcmp(p->name, n) != 0) { pre = p; p = pre->next; if (p == NULL) { printf("沒有找到相關信息,無法刪除\n"); return; } } pre->next = p->next; free(p); printf("刪除成功"); } } //向鏈表中插入職工工資信息 void InsertInfo(Worker *L) { int j, k; Worker *p = NULL; p = (Worker *)malloc(sizeof(Worker)); //生成一個新節點p p->total = 0; printf("請輸入要插入的職工的職工工號:"); scanf("%s", &p->no); printf("請輸入要插入的職工的姓名:"); scanf("%s", &p->name); for (k = 0, j = 1; j <= 12, k < 12; j++, k++) p->month[k] = j; printf("請輸入要插入的職工的月工資(用空格隔開):"); for (j = 0; j < 12; j++) { scanf("%f", &p->gz[j]); //輸入每個月的工資 p->total += p->gz[j]; //計算總工資 } p->next = L->next; L->next = p; printf("插入成功!"); } void LIstSearch(Worker *L, char n[]) { int i; Worker *p = L->next; while (p != NULL) { if (strcmp(p->name, n) == 0) { printf("\n該職工的工資的信息如下:\n"); printf("職工編號:"); printf("%s\t\n", p->no); printf("姓名:"); printf("%s\n", p->name); printf("該職工12個月的月工資如下:"); for (i = 0; i < 12; i++) printf("%.2f ", p->gz[i]); printf("\n該職工的年度總工資為:"); printf("%.2f", p->total); printf("\n\n"); printf("\n"); p = p->next; } else p = p->next; } } void SearchMonthSalary(Worker *L, char n[], int m) { int i; Worker *p = L->next; while (p != NULL) { if (strcmp(p->name, n) == 0) { switch (m) { case 1: printf("該職工%d月的工資為%.2f", p->month[0], p->gz[0]); break; case 2: printf("該職工%d月的工資為%.2f", p->month[1], p->gz[1]); break; case 3: printf("該職工%d月的工資為%.2f", p->month[2], p->gz[2]); break; case 4: printf("該職工%d月的工資為%.2f", p->month[3], p->gz[3]); break; case 5: printf("該職工%d月的工資為%.2f", p->month[4], p->gz[4]); break; case 6: printf("該職工%d月的工資為%.2f", p->month[5], p->gz[5]); break; case 7: printf("該職工%d月的工資為%.2f", p->month[6], p->gz[6]); break; case 8: printf("該職工%d月的工資為%.2f", p->month[7], p->gz[7]); break; case 9: printf("該職工%d月的工資為%.2f", p->month[8], p->gz[8]); break; case 10: printf("該職工%d月的工資為%.2f", p->month[9], p->gz[9]); break; case 11: printf("該職工%d月的工資為%.2f", p->month[10], p->gz[10]); break; case 12: printf("該職工%d月的工資為%.2f", p->month[11], p->gz[11]); break; default: break; } p = p->next; } else p = p->next; } } void Bubble_sort(Worker *L)//冒泡排序實現對鏈表節點的排序 { Worker *p,*q,*tail,*l; tail = NULL; while((L->next->next) != tail) { p = L; q = L->next; while(q->next != tail) { if((q->total) > (q->next->total)) { p->next = q->next; q->next = q->next->next; p->next->next = q; q = p->next; } q = q->next; p = p->next; } tail = q; } printf("排序完成!年度總工資從小到大結果如下:\n"); l=L->next; while(l!=NULL) { if(l->next!=NULL) { printf("%s->",l->name); l=l->next; } else { printf("%s",l->name); l=l->next; } } } void StatiTotal(Worker *L,char n[]) //統計某個職工年度總工資 { int i; Worker *p=L->next; while(p!=NULL) { p->total=0; if(strcmp(p->name,n)==0) { for(i=0;i<12;i++) p->total+=p->gz[i]; printf("%s的年度總工資為%.2f",n,p->total); break; } else p=p->next; } } void Modify(Worker *L, char n[], int m, float s) { int i,j; Worker *p = L->next; while (p != NULL) { if (strcmp(p->name, n) == 0) { switch (m) { case 1: p->gz[0] = s; break; case 2: p->gz[1] = s; break; case 3: p->gz[2] = s; break; case 4: p->gz[3] = s; break; case 5: p->gz[4] = s; break; case 6: p->gz[5] = s; break; case 7: p->gz[6] = s; break; case 8: p->gz[7] = s; break; case 9: p->gz[8] = s; break; case 10: p->gz[9] = s; break; case 11: p->gz[10] = s; break; case 12: p->gz[11] = s; break; default: break; } p->total=0; for(j=0;j<12;j++) p->total+=p->gz[j]; p=p->next; } else p = p->next; } printf("修改成功!"); } void Input(Worker *p, int i) { int j, k; p->total = 0; printf("請輸入第%d名職工的職工工號:", i + 1); scanf("%s", &p->no); printf("請輸入第%d名職工的姓名:", i + 1); scanf("%s", &p->name); for (k = 0, j = 1; j <= 12, k < 12; j++, k++) p->month[k] = j; printf("請輸入第%d名職工的月工資(用空格隔開):", i + 1); for (j = 0; j < 12; j++) { scanf("%f", &p->gz[j]); //輸入每個月的工資 p->total += p->gz[j]; //計算總工資 } } void Print(Worker *L) //打印所有職工工資信息 { int i; Worker *p = L->next; while (p != NULL) { printf("職工編號:"); printf("%s\t\n", p->no); printf("姓名:"); printf("%s\n", p->name); printf("該職工12個月的月工資如下:"); for (i = 0; i < 12; i++) printf("%.2f ", p->gz[i]); printf("\n該職工的年度總工資為:"); printf("%.2f", p->total); printf("\n\n"); p = p->next; } } void Save(Worker *L) { int i; Worker *p = L->next; FILE *fp = fopen("WorkerSalaryInfo.txt", "w"); while (p != NULL) { fprintf(fp, "職工編號:"); fprintf(fp, "%s\t\n", p->no); fprintf(fp, "姓名:"); fprintf(fp, "%s\n", p->name); fprintf(fp, "該職工12個月的月工資如下:"); for (i = 0; i < 12; i++) fprintf(fp, "%.2f ", p->gz[i]); fprintf(fp, "\n該職工的年度總工資為:"); fprintf(fp, "%.2f", p->total); fprintf(fp, "\n\n"); p = p->next; } fclose(fp); printf("保存成功,已保存至當前目錄下的‘WorkerSalaryInfo.txt'文件中"); } void menu() { printf("\t\t\t\t\t \n"); printf("\t\t\t\t\t▔▔▔▔▔▔▔歡迎進入工資管理系統▔▔▔▔▔▔▔\n"); printf("\t\t\t\t\t1.錄入職工每個月的工資信息 \n"); printf("\t\t\t\t\t2.按姓名查找某個員工各月的工資 \n"); printf("\t\t\t\t\t3.按姓名查找某個職工的某個月的工資 \n"); printf("\t\t\t\t\t4.修改某個職工某個月的工資 \n"); printf("\t\t\t\t\t5.刪除某個職工的相關信息 \n"); printf("\t\t\t\t\t6.插入職工工資信息 \n"); printf("\t\t\t\t\t7.統計某個職工年度總工資 \n"); printf("\t\t\t\t\t8.對職工的年度總工資進行從小到大排名 \n"); printf("\t\t\t\t\t9.輸出所有職工工資信息 \n"); printf("\t\t\t\t\t10、將所有職工的工資信息保存至文件 \n"); printf("\t\t\t\t\t0.退出 \n"); printf("\t\t\t\t\t▁▁▁▁▁▁▁▁▁▁謝謝使用▁▁▁▁▁▁▁▁▁\n"); } int main() { int item, n, m; //item用於接收輸入的命令,n用於接收輸入的職工人數 float s; char nam[10]; Worker *L = (Worker*)malloc(sizeof(Worker)); L->next=NULL; do { system("cls"); menu(); printf("\n請輸入相應的數字,進行相應的操作:\n"); scanf("%d", &item); switch (item) { case 1: printf("請輸入您要錄入的職工人數:"); scanf("%d", &n); L = CreateList(L, n); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 2: printf("請輸入您要查找的職工姓名:"); scanf("%s", &nam); LIstSearch(L, nam); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 3: printf("請輸入您要查找的職工的姓名:"); scanf("%s", &nam); printf("請輸入您要查找該職工第幾個月的工資(1到12):"); scanf("%d", &m); SearchMonthSalary(L, nam, m); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 4: printf("請輸入您要修改的職工姓名:"); scanf("%s", &nam); printf("\n請輸入您要修改的月份:"); scanf("%d", &m); printf("\n請輸入您修改後的數據:"); scanf("%f", &s); Modify(L, nam, m, s); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 5: printf("請輸入您要刪除的職工姓名:"); scanf("%s", &nam); DeleteList(L, nam); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 6: InsertInfo(L); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 7: printf("請輸入您要查詢的職工姓名:"); scanf("%s",&nam); StatiTotal(L,nam); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 8: Bubble_sort(L); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 9: printf("全部職工的信息如下:\n\n"); Print(L); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 10: Save(L); getchar(); printf("\n請按任意鍵返回主菜單\n"); getchar(); break; case 0: printf("謝謝您使用工資管理系統,即將退出工資管理系統....."); exit(0); break; } printf("\n\n\n\n"); } while (item); return 0; }
部分運行結果截圖:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。