C語言實現員工工資管理系統
本文實例為大傢分享瞭C語言實現員工工資管理系統的具體代碼,供大傢參考,具體內容如下
看點:
1. 枚舉類型的使用(錄入與輸出)
2. 文件的使用:用文件來存儲員工信息,開始運行程序時從文件中讀取員工信息到鏈表退出程序時將員工信息保存到文件中。
2_1. 存儲結構體這樣的數據塊所使用的文件操作函數;
2_2. feof()函數作為循環條件的正確使用方法;
3. 鏈表的相關操作
3_1. 結構體中char類型的數據寫入方式
4. while循環下實現的菜單與子菜單
註:部分功能還沒有實現,涉及到單鏈表的遍歷,比較簡單。
頭文件:標頭.h
#include<malloc.h> #include<stdio.h> #include<stdlib.h> #include<string.h> //枚舉型崗位:銷售員、技術員、銷售經理、經理 enum workpost { SALESMAN = 0, TECHNICIAN, SALESMANAGER, MANAGER }; //員工類型:鏈表結點 struct Employee { int id; //員工號:主鍵,唯一 char name[10]; //姓名 char sex[3]; //性別:男或女 int age; //年齡 char department[20]; //部門 enum workpost post; //枚舉型崗位 int workHour; //當月工作小時數 double sales; //當月月銷售額 double salary; //月工資 struct Employee * next;//鏈表的指針域 }; #define LENGTH 50 //全局變量 Employee Employees[LENGTH]; typedef int DataType; Employee *head; int select = 1;//功能選擇變量 int subselect = 1;//子功能選擇變量 FILE *fp; int i = 0;//循環變量 //函數聲明 void ListInitiate(Employee **head); void Load(); void Check(); void AddData(); void UpdataData(); void QueryData(); void DeleteData(); void Sorting(); void Statistics(); void Save(); //菜單函數 //要求:至少有兩層菜單。 void Menu() { while (select) { printf("————————員工工資管理系統————————\n"); printf("————1. 增添數據 2. 修改數據————\n"); printf("————3. 查找數據 4. 刪除數據————\n"); printf("————5. 排序函數 6. 統計數據————\n"); printf("————0. 退出程序 7. 查看鏈表————\n"); printf("請輸入你的選擇:\n"); scanf("%d", &select); switch (select) { case 1: AddData(); break; case 2: UpdataData(); break; case 3: QueryData(); break; case 4: DeleteData(); break; case 5: Sorting(); break; case 6: Statistics(); break; case 7: Check(); break; case 0: break; default: printf("輸入錯誤!請重新選擇。\n"); } } } //功能函數 //初始化鏈表函數 void ListInitiate(Employee **head)//head是一個指針,*head也是一個指針 { *head = (Employee *)malloc(sizeof(Employee));//申請頭結點 (*head)->next = NULL; } //必須用文件來存儲員工信息: //開始運行程序時從文件中讀取員工信息到鏈表,退出程序時將員工信息保存到文件中。 void Load() { if ((fp = fopen("Employee.txt", "ab+")) == NULL) { printf("打開文件出錯!\n"); exit(1); } //未知文件長度,使用feof(FILE *Stream)判斷文件結尾 while (!feof(fp)) { //feof(fp)通過fread/fscanf是否讀寫出錯判斷,應該在讀寫操作之後進行判斷,否則多進行一輪循環 if (fread(&Employees[i], sizeof(struct Employee), 1, fp) == 0) { continue; } Employee *p, *q;//分別用於指向第i個結點和待插入的新結點 p = head; while (p->next != NULL)//把指針P定位到鏈表尾位置 { p = p->next; } q = (Employee *)malloc(sizeof(Employee));//用指針Q申請新結點 //存入數據到鏈表結點 q->id = i; q->id++; strcpy(q->name, Employees[i].name); strcpy(q->sex, Employees[i].sex); q->age = Employees[i].age; strcpy(q->department, Employees[i].department); q->post = Employees[i].post; q->workHour = Employees[i].workHour; q->sales = Employees[i].sales;//當月月銷售額 q->salary = Employees[i].salary;//月工資 q->next = p->next;//Q指針的next域指向P指針的next域,即第i個結點 p->next = q; i++; } fclose(fp); } //檢查鏈表數據輸入是否準確 void Check() { Employee *p; p = head; if (p->next == NULL) { printf("鏈表為空!\n"); } else { printf("鏈表數據內容為:\n"); while (p->next != NULL) { p = p->next; printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } printf("輸出完畢!\n"); } } //增添數據 //添加一個員工,隻輸入該員工的基本信息(姓名、年齡、性別、部門、崗位)。 void AddData() { Employee *p, *q;//分別用於指向第i個結點和待插入的新結點 p = head; while (p->next != NULL)//把指針P定位到鏈表尾位置 { p = p->next; } q = (Employee *)malloc(sizeof(Employee));//用指針Q申請新結點 i++; q->id = i; printf("請輸入員工姓名:\n"); scanf("%s", q->name); printf("請輸入員工年齡:\n"); scanf("%d", &q->age); printf("請輸入員工性別:\n"); scanf("%s", q->sex); printf("請輸入員工部門:\n"); scanf("%s", q->department); printf("請輸入員工崗位:\n"); scanf("%d", &q->post); q->workHour = 0; if (q->post == 3) { q->salary = 8000.0; } else { q->salary = 0.0; } q->sales = 0.0; q->next = p->next;//Q指針的next域指向P指針的next域,即第i個結點 p->next = q; printf("添加成功!\n"); } //修改數據 /* 1)根據員工號來修改任意員工的基本信息(姓名、年齡、性別、部門、崗位)。 2)批量計算所有技術員的當月工資額(需填寫技術員當月工作時間)。 3)批量計算所有銷售員的當月工資額(需填寫銷售員當月銷售額);然後批量計算所有銷售經理的當月工資額。 */ void UpdataData() { subselect = 1; Employee *p, *p1; p = head; while (subselect) { printf("—————— 修改數據的二級菜單 ——————\n"); printf("——1. 根據員工號來修改任意員工的基本信息\n"); printf("——2. 批量計算所有技術員的當月工資額\n"); printf("——3. 批量計算所有銷售員的當月工資額,然後批量計算所有銷售經理的當月工資額\n"); printf("——0. 退出此子菜單\n"); printf("請輸入你的選擇:\n"); scanf("%d", &subselect); switch (subselect) { case 1: p = head; int ID; printf("請輸入待修改的員工信息的員工號:\n"); scanf("%d", &ID); while (p->next != NULL) { p = p->next; if (p->id == ID) { printf("請輸入新的員工姓名:\n"); scanf("%s", p->name); printf("請輸入新的員工年齡:\n"); scanf("%d", &p->age); printf("請輸入新的員工性別:\n"); scanf("%s", p->sex); printf("請輸入新的員工部門:\n"); scanf("%s", p->department); printf("請輸入新的員工崗位:\n"); scanf("%d", &p->post); printf("信息錄入成功!\n"); break; } } break; case 2: //批量計算所有技術員的當月工資額,技術員工資:工作時間*小時工資(100元每小時) p = head; while (p->next != NULL) { p = p->next; if (p->post == 1) { printf("請填寫員工號為%d的技術員當月工作時間:\n", p->id); scanf("%d", &p->workHour); p->salary = p->workHour * 100; } } break; case 3: //銷售員工資:銷售額*4%提成; p = head; while (p->next != NULL) { p = p->next; if (p->post == 0) { printf("請填寫員工號為%d的銷售員當月銷售額:\n", p->id); scanf("%lf", &p->sales); p->salary = p->sales*0.04; } } //銷售經理:底薪(5000)+所轄部門銷售額總額*0.5%。 p = head; while (p->next != NULL) { p = p->next; p1 = head; double sum_of_sales = 0.0; if (p->post == 2) { while(p1->next != NULL) { p1 = p1->next; if (strcmp(p->department, p1->department)==0) { sum_of_sales += p1->sales ; } } p->salary = sum_of_sales * 0.005 + 5000; } } break; case 0: break; default: printf("輸入錯誤!請重新選擇。\n"); } } } //查詢數據 void QueryData() { subselect = 1; Employee *p; p = head; while (subselect) { printf("—————— 查詢數據的二級菜單 ——————\n"); printf("——1. 根據工號或者姓名查詢員工信息\n"); printf("——2. 按部門顯示本部門全部員工信息\n"); printf("——3. 分別顯示4種崗位的員工信息\n"); printf("——4. 分頁顯示全部員工的信息\n"); printf("——0. 退出此子菜單\n"); printf("請輸入你的選擇:\n"); scanf("%d", &subselect); switch (subselect) { case 1: p = head; int ID; printf("請輸入工號:\n"); scanf("%d", &ID); while (p->next != NULL) { p = p->next; if (p->id == ID) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); printf("輸出完畢!\n"); } } break; case 2: char Department[20]; printf("請輸入部門名稱:\n"); scanf("%s", &Department); p = head; while(p->next != NULL) { p = p->next; if (strcmp(p->department, Department)==0) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } printf("輸出完畢!\n"); break; case 3: printf("SALESMAN:\n"); p = head; while (p->next != NULL) { p = p->next; if (p->post==0) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } printf("TECHNICIAN:\n"); p = head; while (p->next != NULL) { p = p->next; if (p->post==1) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } printf("SALESMANAGER:\n"); p = head; while (p->next != NULL) { p = p->next; if (p->post==2) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } printf("MANAGER:\n"); p = head; while (p->next != NULL) { p = p->next; if (p->post==3) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } printf("輸出完畢!\n"); break; case 4: printf("分頁功能略難(沒學過),暫時留空吧\n"); break; case 0: break; default: printf("輸入錯誤!請重新選擇。\n"); } } } //刪除數據 void DeleteData(){ Employee *p, *s; p = head; printf("請輸入要刪除員工信息的員工號:\n"); scanf("%d", &i); while (p->next != NULL) { s = p; p = p->next; if(p->id==i) break; } s->next = s->next->next; free(p); } //按要求排序 void Sorting() { subselect = 1; Employee *p, *p1; int z = 0; int f=0; int count = 0; double s[20]; double storeNum; p = head; while (subselect) { printf("—————— 排序數據的二級菜單 ——————\n"); printf("——1. 所有員工信息按當月工資從高到低並顯示\n"); printf("——2. 某個崗位的員工信息按當月工資從高到低顯示\n"); printf("——0. 退出此子菜單\n"); printf("請輸入你的選擇:\n"); scanf("%d", &subselect); switch (subselect) { case 1: p = head; z = 0; while (p->next != NULL) { p = p->next; s[z] = p->salary; z++; } //冒泡排序 for (int i = 0; i < z-1; i++)//因為進行兩兩比較,10個數最壞的情況就需要進行9趟 { for (int j = 0; j < (z-1) - i; j++)//因為每進行一趟就可以排好一個數,所以循環次數-i { if (s[j] > s[j+1])//兩兩比較 { double temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } } z--; for (int i = z; i >= 0; i--) { if(storeNum == s[i]) { continue; } p = head; while (p->next != NULL) { p = p->next; storeNum = s[i]; if(p->salary==s[i]) { printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } } break; case 2: int Post; z = 0; storeNum = 0.0; printf("請輸入崗位信息:(SALESMAN = 0, TECHNICIAN, SALESMANAGER, MANAGER)\n"); scanf("%d", &Post); p = head; while(p->next != NULL) { p = p->next; if (p->post==Post) { s[z] = p->salary; z++; } } //冒泡排序 for (int i = 0; i < z-1; i++) { for (int j = 0; j < (z-1) - i; j++)//因為每進行一趟就可以排好一個數,所以循環次數-i { if (s[j] > s[j+1])//兩兩比較 { double temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } } z--; for (int i = z; i >= 0; i--) { if(storeNum == s[i]) { continue; } p = head; while (p->next != NULL) { p = p->next; if(p->salary==s[i]&&p->post==Post) { storeNum = s[i]; printf("%d %s %s %d %s ", p->id, p->name, p->sex, p->age, p->department); switch (p->post) { case 0: printf("SALESMAN "); break; case 1: printf("TECHNICIAN "); break; case 2: printf("SALESMANAGER"); break; case 3: printf("MANAGER "); break; default: printf("數據有誤!\n"); break; } printf(" %d %.2lf %.2lf\n", p->workHour, p->sales, p->salary); } } } break; case 0: break; default: printf("輸入錯誤!請重新選擇。\n"); } } } //統計數據 void Statistics() { printf("hh\n"); } //保存鏈表數據到文件 void Save() { Employee *p; p = head; i = 0; //第一步將鏈表中的結點信息存入數組中 while (p->next != NULL) { p = p->next; Employees[i].id = p->id; strcpy(Employees[i].name, p->name); strcpy(Employees[i].sex, p->sex); Employees[i].age = p->age; strcpy(Employees[i].department, p->department); Employees[i].post = p->post; Employees[i].workHour = p->workHour; Employees[i].sales = p->sales;//當月月銷售額 Employees[i].salary = p->salary;//月工資 i++; } //第二步將數組數據錄入文本文件 if ((fp = fopen("employee.txt", "wb+")) == NULL) { printf("打開文件出錯!\n"); exit(1); } p = head; i = 0; while (p->next != NULL) { p = p->next; if (fwrite(&Employees[i], sizeof(struct Employee), 1, fp) != 1) { printf("寫入文件出錯!\n"); } i++; } fclose(fp); printf("成功保存數據到文本文件employee\n"); }
Main函數:
#include"標頭.h" int main() { ListInitiate(&head); Load();//加載已有的數據到鏈表 Menu(); Save();//保存數據到文件 printf("歡迎下次使用!\n"); }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。