C语言课综合实验
#include<stdlib.h>#include<stdio.h>#include <windows.h>#include<string.h>#define N 100typedef struct workers{int jobNo; //员工号char name[15]; //姓名char sex[15]; //性别int age; //年龄char degree[15];//学历char position[15]; //职务 int salary;//工资 char addr[15]; //住址 char tel[15]; //电话 }WORKERS;WORKERS worker[N];WORKERS t, t1;int Read();void Save(int n);void Input();void Update();void Delete();void Inquiry();void Inquiry_jobNo();void Inquiry_name();void Inquiry_sex();void Inquiry_range_age();void Inquiry_degree();void Inquiry_position();void Inquiry_range_salary();void Inquiry_addr();void Inquiry_tel();void Array();void Statistics();void Output();void Change();void Menu();int main(){system("color F0");int flag;while (1){system("cls");Menu();printf("输入你选择的序号:");scanf("%d", &flag);switch (flag){case 0: system("cls"); printf(" \n\t\t\t\n提示:退出系统!\n\t\t\t感谢您的使用\n\t\t\t再次敲击回车退出\n "); getchar(); getchar(); exit(8); break;case 1: Input(); break;case 2: Update(); break;case 3: Inquiry(); getchar(); getchar(); break;case 4: Array(); getchar(); getchar(); break;case 5: Statistics(); getchar(); getchar(); break;case 6: Output(); getchar(); getchar(); break;default: printf("提示:输入错误!\n "); break;}}}int Read(){FILE* fp;int i = 0;if ((fp = fopen("data01.dat", "rb")) == NULL) {printf("Cannot open file!\n");exit(0);}else {do {fread(&worker[i], sizeof(WORKERS), 1, fp);i++;} while (feof(fp) == 0);}fclose(fp);return(i - 1);}void Save(int n){FILE* fp;if ((fp = fopen("data01.dat", "wb")) == NULL) /*创建文件并判断是否能打开*/{printf("cannot open file!\n");system("pause");exit(0);}for (int i = 0; i < n; i++) { /*将内存中员工的信息输出到磁盘文件中去*/if (fwrite(&worker[i], sizeof(WORKERS), 1, fp) != 1)printf("file write error!\n");}fclose(fp);}void Input(){int n = Read(), k, m, num;printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("请输入要增加的员工人数:");scanf("%d", &m);for (int i = n; i < n + m; i++){printf("\n输入第%d个员工信息\n", i + 1);printf("输入该员工员工号:");scanf("%d", &num);while (1){k = 0;for (int j = 0; j < i; j++){if (worker[j].jobNo == num){k = 1; break;}}if (k == 0)break;printf("\t学号重复,请重新输入!\n");scanf("%d", num);}getchar();worker[i].jobNo = num;printf("请输入姓名:");scanf("%s", worker[i].name);printf("请输入性别:");scanf("%s", worker[i].sex);printf("请输入年龄:");scanf("%d", &worker[i].age);printf("请输入学历:");scanf("%s", worker[i].degree);printf("请输入职务:");scanf("%s", worker[i].position);printf("请输入工资:");scanf("%d", &worker[i].salary);printf("请输入住址:");scanf("%s", worker[i].addr);printf("请输入电话:");scanf("%s", worker[i].tel);system("cls");printf("\n提示:已经完成一条记录的添加!\n\n");printf("\n");}printf("\n提示:成功录入所有员工信息!\n敲击回车返回上一级\n");getchar();getchar();Save(n + m);}void Update(){int t;system("cls");while (1){printf("\n ");printf("更新成员信息:\n ");printf("-----------------------------------------\n ");printf(" 0. 返回主菜单\n ");printf(" 1.添加成员信息 \n ");printf(" 2.删除成员信息 \n ");printf(" 3.修改成员信息 \n ");printf("-----------------------------------------\n ");printf("\t\t\t\t\t\tdesigned by LONG\n ");getchar;printf("输入号码:");scanf("%d", &t);system("cls");switch (t){case 0:system("cls"); main(); break;case 1:printf("添加成员信息:\n"); Input();Update(); break;case 2:printf("删除成员信息:\n"); Delete(); break;case 3:printf("修改成员信息:\n"); Change(); break;default:printf("输入有误!\n"); break;}}}void Display() /*浏览函数*/{int i;int n = Read();printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("\n目前贵公司所有员工信息:\n"); //printf("\n 原有员工信息:\n"); printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");for (i = 0; i < n; i++)printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);printf("\n\n\n");}void Delete()//删除{int n = Read();int i, j, t, m, f, Num;Display(); /*调用浏览函数*/printf("\n请输入要删除的员工的员工号: ");scanf("%d", &Num);for (f = 1, i = 0; f && i < n; i++){if (worker[i].jobNo == Num){printf("\n已找到此人,原始记录为:\n");printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);printf("\n确认删除请按 1,取消删除请按 0: ");scanf("%d", &m);if (m == 1) /*如果删除,则其他的信息都向前移一行*/{for (j = i; j < n - 1; j++)worker[j] = worker[j + 1];/*当找到第j个员工时,for语句后j自加了1,所以下面的应该把改后的 信息赋值给第j-1个人*/f = 0;}else f = 2;}}system("cls");switch (f) {case 0: n = n - 1;printf("\n\t\t\t\t删除成功!\n");Save(n);/*调用保存函数*/Display(); /*调用浏览函数*/break;case 1: printf("\n\t\t\t对不起,贵公司没有该员工!\n");Display(); /*调用浏览函数*/break;case 2: printf("\n\t\t\t\t取消删除!\n");break;}printf("\n继续删除请按1,退出删除并返回上一级请按0: ");scanf("%d", &t);system("cls");switch (t){case 1: Delete();break;case 0:Update();break;default: break;}system("cls");}void Change(){int jobNo; //员工号char name[15];//姓名char sex[15]; // 性别int age;// 年龄char degree[20]; //学历char position[15];//职位int salary; //工资char addr[20];//住址char Tel[15];//电话int b = 1, c, i, n, t, k = 0;int m = Read();/*导入文件内的信息*/printf("\n");Display();printf("请输入要修改的员工的员工号: ");scanf("%d", &jobNo);system("cls");for (i = 0; i < m; i++){if (worker[i].jobNo == jobNo){k = 1;printf("\n已找到该员工号员工,其记录为:\n");printf("\n员工号\t姓名\t性别\t年龄\t学历\t职位\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);printf("\n确认修改请按1,取消修改请按0:");scanf("%d", &n);if (n == 1){printf("\n需要进行修改的选项\n 1.员工号 2.姓名 3.性别 4.年龄 5.学历 6.职位 7.工资 8.住址 9.电话 \n");printf("请输入你想修改的那一项序号: ");scanf("%d", &c);do {switch (c){case 1: printf("员工号改为: ");scanf("%d", &jobNo);worker[i].jobNo = jobNo;break;case 2: printf("姓名改为: ");scanf("%s", name);strcpy(worker[i].name, name);break;case 3: printf("性别改为:");scanf("%s", sex);strcpy(worker[i].sex, sex);break;case 4: printf("年龄改为: ");scanf("%d", &age);worker[i].age = age;break;case 5: printf("学历改为: ");scanf("%s", °ree);strcpy(worker[i].degree, degree);break;case 6: printf("职位改为: ");scanf("%s", &position);strcpy(worker[i].position, position);break;case 7: printf("基本工资改为: ");scanf("%d", salary);worker[i].salary = salary;break;case 8: printf("住址改为: ");scanf("%s", &addr);strcpy(worker[i].addr, addr);break;case 9: printf("电话改为: ");scanf("%s", Tel);strcpy(worker[i].tel, Tel);break;default: break;}printf("\n确认修改 请按1,重新输入 请按2: ");scanf("%d", &b);if (b == 1){system("cls");Save(m);//储存修改后的数据printf("\n");Display();//浏览修改后的数据}} while (b == 2);}}else if (i == (m - 1)) i++;}if (k == 0) {printf("\n对不起,贵公司没有该员工!!\n");getchar();}else {printf("修改成功!\n");}printf("\n继续修改请按 1,退出修改并返回上一级请按 0: ");scanf("%d", &t);system("cls");switch (t){case 1: Display();Change();break;case 0: Update();break;default:break;}}void Inquiry(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");int c, t;printf("\n 1.员工号 2.姓名 3.性别 4.年龄 5.学历 6.职位 7.工资 8.住址 9.电话 0.返回主菜单\n选择查询的依据:\n");getchar();scanf("%d", &c);switch (c){case 1: Inquiry_jobNo();break;case 2: Inquiry_name();break;case 3: Inquiry_sex();break;case 4: Inquiry_range_age();break;case 5: Inquiry_degree();break;case 6: Inquiry_position();break;case 7: Inquiry_range_salary();break;case 8: Inquiry_addr();break;case 9: Inquiry_tel();break;case 0: main();default: break;}printf("\n继续查询请按1,返回主菜单请按0: ");scanf("%d", &t);switch (t){case 1:Inquiry();case 0:main();default:break;}}void Inquiry_jobNo(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 员工号 查询: \n");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的员工号: ");scanf("%d", &t.jobNo);for (int i = 0;i < len;i++)if (worker[i].jobNo == t.jobNo) {for (int j = 0;j < 1;j++) {printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");}printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求员工!\n");printf("\n继续按员工号查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_jobNo();case 0:Inquiry();}}void Inquiry_name(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 姓名 查询: \n ");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的姓名: ");scanf("%s", &t.name);for (int i = 0;i < len;i++)if (strcmp(worker[i].name, t.name) == 0) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按姓名查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_name();case 0:Inquiry();}}void Inquiry_sex(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 性别 查询: \n ");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的性别: ");scanf("%s", &t.sex);for (int i = 0;i < len;i++)if (strcmp(worker[i].sex, t.sex) == 0) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按性别查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_sex();case 0:Inquiry();}}void Inquiry_range_age(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 年龄 查询: \n ");int n = 0, a;int len = Read();printf("\n请依次输入要查询的员工年龄的最小值、最大值(左闭右开): \n");scanf("%d%d", &t.age, &t1.age);for (int i = 0;i < len;i++)if (worker[i].age < t1.age && worker[i].age >= t.age) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按年龄查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_range_age();case 0:Inquiry();}}void Inquiry_degree(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 学历 查询: \n ");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的学历: ");scanf("%s", &t.degree);for (int i = 0;i < len;i++)if (strcmp(worker[i].degree, t.degree) == 0) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按学历查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_degree();case 0:Inquiry();}}void Inquiry_position(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 职务 查询: \n");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的职位: ");scanf("%s", &t.position);for (int i = 0;i < len;i++)if (strcmp(worker[i].position, t.position) == 0) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按职位查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_position();case 0:Inquiry();}}void Inquiry_range_salary(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 工资 查询: \n ");int n = 0, a;int len = Read();printf("\n请依次输入要查询的员工工资的最小值、最大值(左闭右开): \n");scanf("%d%d", &t.salary, &t1.salary);for (int i = 0;i < len;i++)if (worker[i].salary < t1.salary && worker[i].salary >= t.salary) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按工资查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_range_salary();case 0:Inquiry();}}void Inquiry_addr(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 住址 查询: \n ");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的住址: ");scanf("%s", &t.addr);for (int i = 0;i < len;i++)if (strcmp(worker[i].addr, t.addr) == 0) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按住址查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_addr();case 0:Inquiry();}}void Inquiry_tel(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按 电话 查询: \n ");int n = 0, a;int len = Read();printf("\n请输入要查询的员工的电话: ");scanf("%s", &t.tel);for (int i = 0;i < len;i++)if (strcmp(worker[i].tel, t.tel) == 0) {if (n == 0)printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);n++;}if (n == 0)printf("\n对不起,贵公司没有符合该要求的员工!\n");elseprintf("\n查询到有%d个员工符合要求\n", n);printf("\n继续按电话查询请按1,返回上一级请按0: ");scanf("%d", &a);switch (a){case 1:Inquiry_tel();case 0:Inquiry();}}void Array(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");printf("按员工号排序为:\n");int k;int m = Read();for (int i = 0; i < m - 1; i++)for (int j = 0; j < m - i - 1; j++)if (worker[j].jobNo > worker[j + 1].jobNo){t = worker[j];worker[j] = worker[j + 1];worker[j + 1] = t;}printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");for (int i = 0; i < m; i++)printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);printf("\n\n敲击回车返回主菜单\n");}void Statistics(){int len = Read();int i = 0, j = 0, m = 0, n = 0;for (int a = 0; a < len; a++) {if (strcmp(worker[a].sex, "男") == 0)i++;if (strcmp(worker[a].sex, "女") == 0)j++;}printf("\n员工总人数:%d\n男员工总人数:%d\n女员工总人数:%d\n", len, i, j);printf("\n\n敲击回车返回主菜单\n");}void Output(){system("cls");printf("\t\t\t\t\t\tdesigned by LONG\n ");int i;int n = Read();printf("\n贵公司所有员工信息:\n"); //printf("\n 原有员工信息:\n"); printf("\n员工号\t姓名\t性别\t年龄\t学历\t职务\t 工资\t住址\t 电话 \n");for (i = 0; i < n; i++)printf("\n % d\t % s\t % s\t % d\t % s\t % s\t % d\t % s\t % s\n", worker[i].jobNo, worker[i].name, worker[i].sex, worker[i].age, worker[i].degree, worker[i].position, worker[i].salary, worker[i].addr, worker[i].tel);printf("\n\n敲击回车返回主菜单\n");}void Menu(){printf("\t企业员工信息管理系统\n");printf("==================菜 单==================\n ");printf(" 1.录入员工信息 \n ");printf(" 2.更新员工信息 \n ");printf(" 3.查询员工信息 \n ");printf(" 4.排列员工信息 \n ");printf(" 5.统计员工信息 \n ");printf(" 6.输出员工信息 \n ");printf(" 0. 退出 \n ");printf("=========================================\n ");printf("\t\t\t\t\t\tdesigned by LONG\n ");}