当前位置:文档之家› 职工信息管理系统源代码

职工信息管理系统源代码

#include<stdio.h>#include<string.h>#include<stdlib.h>int N=2;struct worker /*定义一个职工信息的结构体*/{int num; /*定义一个职工信息的结构体*/char name[20]; /*定义一个职工信息的结构体*/char sex[10]; //用字符串存放职工的性别数据char birth[20]; //用字符串存放职工的出生年月数据char deta[20]; //用字符串存放职工的工作年月数据char education[20]; //用字符串存放职工的学历数据double salary; //用字符串存放职工的性别数据char work[30]; //用字符串存放职工的职务数据char addr[20]; //用字符串存放职工的地址数据char tel[20]; //用字符串存放职工的电话数据}worker[100]; //用结构体数组存放职工的所有信息void menu();void input();void save();void read();void display();void add();void search();void search_education();void search_num();void del();void modify();void main(){char s;system("color 3f");printf("\n\n");printf(" \t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");printf(" \t┃**********************************************************┃\n");printf(" \t┃***┏━━━━━━━━━━━━━━━━━━━━━━━━┓***┃\n");printf(" \t┃***┃************************************************┃***┃\n");printf(" \t┃***┃*** ****┃***┃\n");printf(" \t┃***┃*** 欢迎使用职工信息管理系统****┃***┃\n");printf(" \t┃***┃*** ****┃***┃\n");printf(" \t┃***┃*** ****┃***┃\n");printf(" \t┃***┃*** 制作人吴青伶****┃***┃\n");printf(" \t┃***┃*** ****┃***┃\n");printf(" \t┃***┃*** 2013.12.28 ****┃***┃\n");printf(" \t┃***┃*** ****┃***┃\n");printf(" \t┃***┃************************************************┃***┃\n");printf(" \t┃***┗━━━━━━━━━━━━━━━━━━━━━━━━┛***┃\n");printf(" \t┃**********************************************************┃\n");printf(" \t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");do{menu();printf("Do you want to continue?(y/n)");s=getchar();}while(s=='Y'||s=='y');}void menu()/*菜单函数*/{int b;printf("\t \n");printf("\t \n");printf("\t \n");printf("\t \n");printf("\t \n");printf("\t 请选择\n");printf("\t ------------------------------\n");printf("\t || || \n");printf("\t || 1. 职工信息录入|| \n");printf("\t || 2. 职工信息浏览|| \n");printf("\t || 3. 职工信息添加|| \n");printf("\t || 4. 职工信息查询|| \n");printf("\t || 5. 职工信息删除|| \n");printf("\t || 6. 职工信息修改|| \n");printf("\t || 0. 退出|| \n");printf("\t -------------------------------\n");printf("\n 输入你的选择项(0~6):\n");scanf("%d",&b);switch(b){case 1 : //职工信息录入、保存input();save();getchar();break;case 2 ://读入职工信息并显示read();display();getchar();break;case 3 : //添加职工信息add();getchar();break;case 4 : //查询职工信息search();getchar();break;case 5 : //读入职工信息并实现职工信息删除功能read();del();getchar();break;case 6 : //读入职工信息并修改职工信息read();modify();getchar();break;case 0 : exit(0);//退出default:break;}}void input() //录入职工信息{int i,flag,j;printf("输入职工个数(默认个数为2):");scanf("%d",&N);fflush(stdin);//清除缓存for(i=0;i<N;i++){loop:printf("职工号:");scanf("%d",&worker[i].num); fflush(stdin);flag=1;if(worker[N].num<0)printf("请输入正整数:\n");elsefor(j=0;j<i;j++){if(worker[i].num==worker[j].num){flag=0;printf("工号重复,请重新输入!\n");goto loop;}}printf("姓名:");scanf("%s",worker[i].name); fflush(stdin);printf("性别:");scanf(" %s",worker[i].sex); fflush(stdin);printf("出生年月:");scanf("%d",&worker[i].birth); fflush(stdin);printf("工作年月:");scanf("%lf",&worker[i].deta); fflush(stdin);printf("请输入学历(中专=zz,大专=dz,本科=bk,研究生=yjs):\n");scanf("%s",worker[i].education);fflush(stdin);printf("工资:");scanf("%lf",worker[i].salary); fflush(stdin);printf("职务:");scanf("%s",worker[i].work); fflush(stdin);printf("住址:");scanf("%s",worker[i].addr); fflush(stdin);printf("电话:");scanf("%s",worker[i].tel); fflush(stdin);}printf("\n创建完毕!\n");}void save() //保存函数{FILE *fp;int i;if((fp=fopen("worker.txt","wb"))==NULL)//以只写方式打开一个二进制文件{printf("\ncannot open file\n");exit(0);}for(i=0;i<N;i++)fwrite(&worker[i],sizeof(struct worker),1,fp);//将内存中的职工信息数据输出到磁盘文件中去(二进制)fclose(fp);}void read() //读入函数{FILE *fp;int i=0;if((fp=fopen("worker.txt","rb"))==NULL) //以只读方式为输出打开磁盘文件中的一个二进制文件{printf("\ncannot open file\n");exit(0);}do{fread(&worker[i],sizeof(struct worker),1,fp);//从worker.txt文件向内存读入数据(二进制)i++;}while(!feof(fp));fclose(fp);N=i-1;}void display() //信息浏览函数{int i;for(i=0;i<N;i++){printf("职工号:%d\n",worker[i].num);printf("姓名:%s\n",worker[i].name);printf("性别:%s\n",worker[i].sex);printf("出生年月:%d\n",worker[i].birth);printf("工作年月:%d\n",worker[i].deta);printf("学历:%s\n",worker[i].education);printf("工资:%lf\n",worker[i].salary);printf("职务:%d\n",worker[i].work);printf("住址:%s\n",worker[i].addr);printf("电话:%s\n",worker[i].tel);}}void add() //添加函数{FILE *fp;int i;struct worker p;double t;printf("\n请输入新增加职工的信息:\n");printf("请输入职工号:\n");scanf("%d",&p.num);for(i=0;i<N;i++){while(worker[i].num==p.num){printf("工号重复,请重新输入!\n");scanf("%d",&p.num);}}printf("请输入姓名:\n");scanf("%s",);printf("请输入性别:\n");scanf("%s",p.sex);printf("请输入出生年月:\n");scanf("%d",&p.birth);printf("请输入工作年月:\n");scanf("%d",&p.deta);printf("请输入学历(中专=zz,大专=dz,本科=bk,研究生=yjs:):\n");scanf("%s",cation);printf("请输入工资:\n");scanf("%lf",&t);p.salary=t;printf("请输入职务:\n");scanf("%s",p.work);printf("请输入住址:\n");scanf("%s",p.addr);printf("请输入电话:\n");scanf("%s",p.tel);printf("\n添加完毕!\n");if((fp=fopen("worker.txt","ab"))==NULL)//以追加方式打开一个二进制文件尾部{printf("\ncannot open file\n");exit(0);}fwrite(&p,sizeof(struct worker),1,fp);fclose(fp);}void search() //查询函数{int c;do{puts("\nsearch by=>\n1.学历2.职工号3.取消并返回");printf("Which you needed?:\n");if(c>3||c<1){puts("\nchoice error!please again!");getchar();//此语句用于接受在执行上一句时最后输出的回车符}}while(c>3||c<1);{switch(c){case 1:search_education();break;case 2:search_num();break;case 3:menu();break;}}}void search_education() //按学历查询函数{int i,flag=0;char s[10];printf("\n请输入你要查询的学历:\n");scanf("%s",s);for(i=0;i<N;i++){if(strcmp(s,worker[i].education)==0){printf("职工号:%d\n",worker[i].num);printf("姓名:%s\n",worker[i].name);printf("性别:%s\n",worker[i].sex);printf("出生年月:%d\n",worker[i].birth);printf("工作年月:%d\n",worker[i].deta);printf("学历(中专=zz,大专=dz,本科=bk,研究生=yjs):%s\n",worker[i].education);printf("工资:%lf\n",worker[i].salary);printf("职务:%lf\n",worker[i].work);printf("住址:%s\n",worker[i].addr);printf("电话:%s\n",worker[i].tel);flag++;}}if(flag==0)printf("\n对不起没有找到!\n");getchar();}void search_num() //按工号查询函数{int i,s,flag=0;printf("\n输入要查询的职工号!:\n");for(i=0;i<N;i++){if(s==worker[i].num){printf("职工号:%d\n",worker[i].num);printf("姓名:%s\n",worker[i].name);printf("性别:%s\n",worker[i].sex);printf("出生年月:%d\n",worker[i].birth);printf("工作年月:%d\n",worker[i].deta);printf("学历(中专=zz,大专=dz,本科=bk,研究生=yjs):%s\n",worker[i].education);printf("工资:%lf\n",worker[i].salary);printf("职务:%lf\n",worker[i].work);printf("住址:%s\n",worker[i].addr);printf("电话:%s\n",worker[i].tel);flag++;}}if(flag==0)printf("\n对不起没有找到! please");getchar();}void del() //删除函数{int i,j;FILE *fp;char name[20];char c;if((fp=fopen("worker.txt","wb"))==NULL)//以只写方式打开一个二进制文件{printf("\ncannot open file\n");exit(0);}printf("请输入要删除的职工的姓名:");scanf("%s",name);for(i=0;i<N;i++){if(strcmp(name,worker[i].name)==0)//判断输入的姓名和原来的姓名是否相同{printf("找到该职工,是否删除?(y/n) ") ;fflush(stdin);scanf("%c",&c);if(c =='Y'||c=='y'){for(j=i;j<N;j++)worker[j]=worker[j+1];printf("删除成功! \n");//break;}}}if(i>=N){printf("没有找到该职工!\n ");return;}else{N=N-1;for(i=0;i<N;i++)if(fwrite(&worker[i],sizeof(struct worker),1,fp)!=1){printf("\ncannot save file\n");getchar();}fclose(fp);}}void modify() //修改函数{struct worker p;FILE *fp;int i,n;double t;printf("\n输入要修改的职工号:\n");scanf("%d",&n);for(i=0;i<N;i++){if(n==worker[i].num){printf("找到该职工的信息(任意键继续)!");printf("\n请输入职工的信息!\n");printf("请输入姓名:\n");scanf("%s",);printf("请输入性别:\n");scanf("%s",p.sex);printf("请输入出身年月:\n");scanf("%d",&p.birth);printf("请输入工作年月:\n");scanf("%lf",&p.deta);printf("请输入学历(中专=zz,大专=dz,本科=bk,研究生=yjs):\n");scanf("%s",cation);printf("请输入工资:\n");scanf("%lf",&t);p.salary=t;printf("请输入职务:\n");scanf("%s",p.work);printf("请输入住址:\n");scanf("%s",p.addr);printf("请输入电话:\n");scanf("%s",p.tel);p.num=n;if((fp=fopen("worker.txt","r+"))==NULL)//以读写的方式将修改的信息写入磁盘文件{printf("\ncannot open file\n");exit(0);}fseek(fp,i*sizeof(struct worker),0);//将位置指针移到i*sizeof(struct worker)个字节处fwrite(&p,sizeof(struct worker),1,fp);fclose(fp);printf("修改成功!\n");break;}}if(i>=N)printf("\n未找到该职工!\n");}。

相关主题