|
|
#include<stdio.h>
|
|
|
#define N 50
|
|
|
struct worker
|
|
|
{
|
|
|
char id[50];
|
|
|
char name[20];
|
|
|
char sex[10];
|
|
|
char age[10];
|
|
|
char edu[20];
|
|
|
char salary[10];
|
|
|
char address[20];
|
|
|
char tel[20];
|
|
|
}worker[N];
|
|
|
void save(int n) //保存函数,保存n个记录
|
|
|
{ FILE *fp;
|
|
|
int i;
|
|
|
if((fp=fopen("worker.txt","wb+"))==NULL) //以读,写方式为输出打开一个二进制文件
|
|
|
{ printf("\nCannot open file\n");
|
|
|
}
|
|
|
for(i=0;i<n;i++)
|
|
|
if(fwrite(&worker[i],sizeof(struct worker),1,fp)!=1)//将输入的数据写入文件中
|
|
|
printf("file write error\n");
|
|
|
fclose(fp);
|
|
|
}
|
|
|
int load() //加载函数
|
|
|
{ FILE *fp;
|
|
|
int i;
|
|
|
if((fp=fopen("worker.txt","rb"))==NULL)
|
|
|
{printf("\nCannot open file\n");
|
|
|
return NULL;
|
|
|
}
|
|
|
for(i=0;!feof(fp);i++)
|
|
|
fread(&worker[i],sizeof(struct worker),1,fp);//从文件中读取出职工的数据
|
|
|
fclose(fp);
|
|
|
return(i-1);
|
|
|
}
|
|
|
void no_input(int i,int n) //工号输入函数,i表示第i个职工的信息,n表示比较到第n个职工
|
|
|
{ int j,k,w1;
|
|
|
do
|
|
|
{w1=0;
|
|
|
printf("工号:");
|
|
|
scanf("%s",worker[i].id);
|
|
|
for(j=0;worker[i].id[j]!='\0';j++)
|
|
|
if(worker[i].id[j]<'0'||worker[i].id[j]>'9') //判断工号是否为数字
|
|
|
{puts("请重新输入!\n");
|
|
|
w1=1;break;
|
|
|
}
|
|
|
if(w1!=1)
|
|
|
for(k=0;k<n;k++) //比较到第N个职工
|
|
|
if(k!=i&&strcmp(worker[k].id,worker[i].id)==0) //判断职工号是否有雷同
|
|
|
{puts("请重新输入!\n");
|
|
|
w1=1;break;
|
|
|
}
|
|
|
}
|
|
|
while(w1==1);
|
|
|
}
|
|
|
|
|
|
void input(int i) //输入一个记录的函数
|
|
|
{ no_input(i,i); //输入工号后继续输入其他数据
|
|
|
printf("姓名:");scanf("%s",worker[i].name);
|
|
|
printf("性别:");scanf(" %s",&worker[i].sex);
|
|
|
printf("年龄:");scanf("%s",&worker[i].age);
|
|
|
printf("工资:");scanf("%s",worker[i].salary);
|
|
|
printf("学历:");scanf("%s",&worker[i].edu);
|
|
|
printf("住址:");scanf("%s",worker[i].address);
|
|
|
printf("电话:");scanf("%s",worker[i].tel);
|
|
|
}
|
|
|
void printf_one(int i) //显示一个记录的函数
|
|
|
{
|
|
|
printf("%10s %10s %5s %5s %8s %6s %10s %10s\n",
|
|
|
worker[i].id,worker[i].name,worker[i].sex,worker[i].age,
|
|
|
worker[i].salary,worker[i].edu,worker[i].address,worker[i].tel);
|
|
|
}
|
|
|
void printf_back() //一个任务结束后选择是好似浏览还是返回
|
|
|
{ int k,w;
|
|
|
printf("\n\n\tSuccessful^-^.\n\n");
|
|
|
printf("接下来做什么?\n\n\t1).浏览全部\t2).返回: [ ]\b\b");
|
|
|
scanf("%d",&w);
|
|
|
if(w==1)browse();
|
|
|
else menu();
|
|
|
}
|
|
|
modify_data(int i,int n) //修改函数
|
|
|
{ int c,w1;
|
|
|
do
|
|
|
{puts("\nmodify by=>\n\n 1).工号 2).姓名 3).性别 4).年龄 5).工资 6).学历 7).地址 8).电话 9).取消");
|
|
|
printf("请选择?:[ ]\b\b");
|
|
|
scanf("%d",&c);
|
|
|
if(c>9||c<1)
|
|
|
{
|
|
|
puts("\nChoice error!please again!");
|
|
|
getchar();
|
|
|
}
|
|
|
}
|
|
|
while(c>9||c<1);
|
|
|
do
|
|
|
{switch(c) //通过switch结构选择要修改的内容
|
|
|
{case 1:no_input(i,n);break;
|
|
|
case 2:printf("name:");scanf("%s",worker[i].name);break;
|
|
|
case 3:printf("sex:");scanf("%s",worker[i].sex);break;
|
|
|
case 4:printf("age:");scanf("%s",worker[i].age);break;
|
|
|
case 5:printf("salary:");scanf("%s",worker[i].salary);break;
|
|
|
case 6:printf("edu:");scanf("%s",worker[i].edu);break;
|
|
|
case 7:printf("address:");scanf("%s",worker[i].address);break;
|
|
|
case 8:printf("tel:");scanf("%s",worker[i].tel);break;
|
|
|
case 9:menu();break;
|
|
|
}
|
|
|
puts("\nNow:\n");
|
|
|
printf_face();
|
|
|
printf_one(i);
|
|
|
printf("\n确定?\n\n\t1).是 2).否,重新修改 3).不保存退出 [ ]\b\b");
|
|
|
scanf("%d",&w1);
|
|
|
if(w1==1)save(n);
|
|
|
}
|
|
|
while(w1==2);
|
|
|
return(w1);
|
|
|
}
|
|
|
|
|
|
printf_face() //显示数据结构项目
|
|
|
{ printf("\n\t工号 姓名 性别 年龄 工资 学历 地址 电话号码\n"); }
|
|
|
|
|
|
void enter() //输入模块
|
|
|
{ int i,n;
|
|
|
printf("输入多少个职工信息(0-%d):",N-1);
|
|
|
scanf("%d",&n);
|
|
|
printf("\n 请输入职工信息\n\n");
|
|
|
for(i=0;i<n;i++)
|
|
|
input(i); //跳转输入数据的函数
|
|
|
if(i!=0)
|
|
|
save(n); //跳转保存至文件的函数
|
|
|
printf_back();
|
|
|
}
|
|
|
browse() //浏览模块
|
|
|
{ int i,j,n;
|
|
|
n=load();
|
|
|
printf_face();
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
if((i!=0)&&(i%10==0))
|
|
|
{printf("\n\n按任意键显示下一页...");
|
|
|
getch();
|
|
|
puts("\n\n");
|
|
|
}
|
|
|
printf_one(i);
|
|
|
}
|
|
|
printf("\t这里有 %d 条记录.\n",n);
|
|
|
printf("\n按任意键返回...");
|
|
|
getch();
|
|
|
menu();
|
|
|
}
|
|
|
modify() //修改模块
|
|
|
{
|
|
|
struct worker s;
|
|
|
FILE *fp;
|
|
|
int i,n,k,w0=1,w1,w2=0;
|
|
|
n=load();
|
|
|
do
|
|
|
{
|
|
|
k=-1;
|
|
|
printf_face();
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
if((i!=0)&&(i%10==0)) //目的是分屏显示
|
|
|
{printf("\n\n记住要修改的工号.按任意键显示下一页");
|
|
|
getch();
|
|
|
puts("\n\n");
|
|
|
}
|
|
|
printf_one(i); //调用显示一个记录的函数
|
|
|
}
|
|
|
do
|
|
|
{
|
|
|
printf("\n\n输入要修改的职工工号! 工号:");
|
|
|
scanf("%s",s.id); //输入要修改的ID
|
|
|
for(i=0;i<n;i++) //查找要修改的数据
|
|
|
if(strcmp(s.id,worker[i].id)==0)
|
|
|
{
|
|
|
k=i; //找到要修改的记录
|
|
|
s=worker[i]; //把worker[i]备份,以便恢复
|
|
|
}
|
|
|
if(k==-1)
|
|
|
printf("\n\n未找到 请重输");
|
|
|
}
|
|
|
while(k==-1); //当k=-1表示没有找到
|
|
|
printf_face(); //调用显示数据结构项目的函数
|
|
|
printf_one(k); //调用显示一个记录的函数
|
|
|
w1=modify_data(k,n); //修改记录并返回保存控制值w1,w1=1表示用户已确认修改
|
|
|
if(w1==1)
|
|
|
{
|
|
|
printf("\Successful^_^.\n\n修改另一个?\n\n\t1).是 2).否,保存退出\t[ ]\b\b");
|
|
|
scanf("%d",&w0);
|
|
|
w2=1; //用来控制保存,使w2=1是标记已有过修改
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
w0=0;
|
|
|
if(w2==1)
|
|
|
worker[k]=s;
|
|
|
}
|
|
|
if(w0!=1&&w2==1)
|
|
|
save(n);
|
|
|
}while(w0==1);
|
|
|
menu();
|
|
|
}
|
|
|
search() //查询模块
|
|
|
{ int c,w1;
|
|
|
do
|
|
|
{puts("\nsearch by=>\n\n1).工号 2}.学历 3).取消并返回");
|
|
|
printf("Which you needed?:[ ]\b\b");
|
|
|
scanf("%d",&c);
|
|
|
if(c>3||c<1)
|
|
|
{ puts("\nchoice error!please again!");
|
|
|
getchar();
|
|
|
}
|
|
|
}
|
|
|
while(c>3||c<1);
|
|
|
{switch(c)
|
|
|
{case 1:search1();break;
|
|
|
case 2:search2();break;
|
|
|
case 3:menu();break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
search1() //按工号查询模块
|
|
|
{int i,n,k,w1=1,w2,w3,w4;
|
|
|
struct worker s;
|
|
|
n=load();
|
|
|
do
|
|
|
{
|
|
|
do
|
|
|
{k=-1;
|
|
|
printf("\n\n输入要查询的工号! id:");
|
|
|
scanf("%s",s.id);
|
|
|
printf_face();
|
|
|
for(i=0;i<n;i++)
|
|
|
if(strcmp(s.id,worker[i].id)==0)
|
|
|
{k=i;
|
|
|
printf_one(k);break;
|
|
|
}
|
|
|
if(k==-1)
|
|
|
{printf("\n\nNo exit!please");
|
|
|
printf("\n\n继续查找?\n\t1).是 2).否,返回 [ ]\b\b");
|
|
|
scanf("%d",&w1);
|
|
|
if(w1==2) menu();
|
|
|
}
|
|
|
}
|
|
|
while(k==-1&&w1==1);
|
|
|
w4=0;w3=0;
|
|
|
if(k!=-1)
|
|
|
{printf("\n\n接下来干什么?\n\t1).查找另一个 2).修改 3).返回菜单 [ ]\b\b");
|
|
|
scanf("%d",&w2);
|
|
|
switch(w2)
|
|
|
{
|
|
|
case 1:search1();break;
|
|
|
case 2:w3=modify_data(k,n);break;
|
|
|
case 3:{menu();break;}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
while(w2==1);
|
|
|
menu();
|
|
|
}
|
|
|
|
|
|
search2() //按学历查询模块
|
|
|
{
|
|
|
int i,n,k,w1=1,w2,w3,w4;
|
|
|
struct worker s;
|
|
|
n=load();
|
|
|
do
|
|
|
{
|
|
|
do
|
|
|
{k=-1;
|
|
|
printf("\n\n请输入你要查询的学历:\n");
|
|
|
scanf("%s",s.edu);
|
|
|
printf_face();
|
|
|
for(i=0;i<n;i++)
|
|
|
if(strcmp(s.edu,worker[i].edu)==0) //运用strcmp函数来比较学历,从而找到学历所属职工
|
|
|
{k=i;
|
|
|
printf_one(k);
|
|
|
}
|
|
|
if(k==-1)
|
|
|
{printf("\n\nNo exit!please");
|
|
|
printf("\n\n继续查找?\n\t1).是 2).否,返回 [ ]\b\b");
|
|
|
scanf("%d",&w1);
|
|
|
if(w1==2) menu();
|
|
|
}
|
|
|
}
|
|
|
while(k==-1&&w1==1);
|
|
|
w4=0;w3=0;
|
|
|
if(k!=-1)
|
|
|
{printf("\n\n接下来干什么?\n\t1).查找另一个 2).修改 3).返回菜单 [ ]\b\b");
|
|
|
scanf("%d",&w2);
|
|
|
switch(w2)
|
|
|
{case 1:search2();break;
|
|
|
case 2:w3=modify_data(k,n);break;
|
|
|
case 3:{menu();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
while(w2==1);
|
|
|
menu();
|
|
|
}
|
|
|
|
|
|
menu()
|
|
|
{
|
|
|
int n,w1;
|
|
|
do
|
|
|
{
|
|
|
puts("\t\t*******************菜单*******************\n\n");
|
|
|
puts("\t\t\t\t1.录入职工信息");
|
|
|
puts("\t\t\t\t2.浏览职工信息");
|
|
|
puts("\t\t\t\t3.查询职工信息");
|
|
|
puts("\t\t\t\t4.修改职工信息");
|
|
|
puts("\t\t\t\t5.退出");
|
|
|
puts("\t\t****************************************\n\n");
|
|
|
printf("请选择(1~5): [ ]\b\b");
|
|
|
scanf("%d",&n);
|
|
|
if(n<1||n>5) //对选择的数字作判断
|
|
|
{w1=1;getchar();}
|
|
|
else w1=0;
|
|
|
} while(w1==1);
|
|
|
switch(n)
|
|
|
{case 1:enter();break; //录入
|
|
|
case 2:browse();break; //浏览
|
|
|
case 3:search();break; //查询
|
|
|
case 4:modify();break; //修改
|
|
|
case 5:exit(0); //退出
|
|
|
}
|
|
|
}
|
|
|
main()
|
|
|
{
|
|
|
menu();
|
|
|
}
|