📄 workin.c
字号:
case 6: printf("\nduty:"); gets(info.duty); getchar(); break;
case 7: printf("\nwage:"); scanf("%lf",info.wage); getchar(); break;
case 8: printf("\naddress:"); gets(info.addr); getchar(); break;
case 9: printf("\nphone:"); gets(info.phone); getchar(); break;
case 10:printf("\n\tnum:");gets(info.num); getchar();
printf("\n\tname:");gets(info.name); getchar();
printf("\n\tsex:");gets(temp); getchar();
if(!strcmp(temp,"female")) info.sex=female;
else info.sex=male;
printf("\n\tbirthday(yyyy/mm/dd):");scanf("%d/%d/%d",&info.birthday.year,&info.birthday.month,&info.birthday.day);
printf("\n\teducation:");gets(temp); getchar();
if(!strcmp(temp,"doctor")) info.education=doctor;
else if(!strcmp(temp,"master")) info.education=master;
else if(!strcmp(temp,"college")) info.education=college;
else if(!strcmp(temp,"junior")) info.education=junior;
else info.education=high;
printf("\n\tduty:");gets(info.duty); getchar();
printf("\n\twage:");gets(temp); info.wage=atof(temp); getchar();
printf("\n\taddress:");gets(info.addr); getchar();
printf("\n\tnum:");gets(info.phone); getchar();
break;
case 11:
default:printf("\n\n");
menu();
}
printf("\nAre you sure?\n\n\t1).Sure 2).Back without save [ ]\b\b"); /*是否确定*/
scanf("%d",&Yes);
getchar();
if(Yes==1) /* 将更改后的数据写回到文件中去 */
{
if((fwrite(&info, sizeof(struct Info), 1, fp))==1)
printf("\nthe modification is successful ^_^");
else
printf("\noh,the modification has not done ! ");
}
else
{
printf("\n%d\n",Yes);
getch();
}
fclose(fp);
}
else
{
printf("\n\nThe record you enter is not found!\tany key to continue...");
getch();
}
printf("\n\nDo you want to modify more records?:\t\t1).Yes\t2).No [ ]\b\b"); /* 是否继续修改其他记录 */
scanf("%d",&flag);
getchar();
}while(flag == 1);
menu();
}
void delete() /* 信息删除函数 */
{
struct Node
{
char num[10];
struct Node * next;
};
int amount; /* 信息文件中的记录总数 */
int con=0; /* 已处理的文件记录总数 */
struct Node node;
struct Node * head=NULL, * pf=NULL, * pb=NULL;
struct Info info, info_temp;
FILE * fp, *fp1; /* fp用于指示信息文件inform,而fp1用于指示临时文件 */
int total=0;
int offset=0; /* 文件位置指针偏移量 */
char temp[10], temp1[10];
int flag=0; /* 指示是否继续进行修改,为1时继续 */
char ID[10]; /* 待修改的记录的职工号 */
int ord; /* 待修改的项目指示 */
int repeat; /* 在修改中用于重复的输入 */
int Yes=0; /* 指示是否保存修改 */
amount=load("inform.txt");
if((fp=fopen("inform.txt","rb")) == NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
printf("%-5s%-8s%-8s%-11s%-10s%-8s%-8s%-12s%-8s\n","nun","name","sex ","birthday","education","duty","wage","address","phone");
for(con=0; con<amount; con++)
{
if(fread(&info, sizeof(info), 1, fp) != 1)
{
printf("\nfile read wrong!\n");
getch();
exit(1);
}
pb=(struct Node *)malloc(sizeof(struct Node)); /* 创建包含num信息的链表,用于后续的查找 */
strcpy(pb->num,info.num);
if(total==0)
head=pf=pb;
else
pf->next=pb;
pb->next=NULL;
pf=pb;
total++;
if(info.sex==male)
strcpy(temp,"male");
else
strcpy(temp,"female");
if(info.education==doctor) strcpy(temp1,"doctor");
else if(info.education==master) strcpy(temp1,"master");
else if(info.education==college) strcpy(temp1,"college");
else if(info.education==junior) strcpy(temp1,"junior");
else strcpy(temp1,"high");
printf("%-5s%-8s%-8s%-4d/%-2d/%-2d%-10s%-8s%-8.2lf%-12s%-8s\n",info.num,info.name,temp,info.birthday.year,info.birthday.month,info.birthday.day,temp1,info.duty,info.wage,info.addr,info.phone);
if((total != 0) && (total%10 ==0))
{
printf("\n\n\tPress any key to continue......");
getch();
puts("\n\n");
}
}
fclose(fp);
do
{
printf("Please enter the num that you want to delete:");
gets(ID);
pf=head;
while(strcmp(pf->num,ID) && pf->next!=NULL)
{
pf=pf->next;
offset++;
}
if(!strcmp(pf->num,ID)) /* 待删除的记录存在 */
{
printf("\nAre you sure to delete the record?:\t\t1).Yes\t2).No [ ]\b\b");
scanf("%d",&Yes);
if(Yes==1) /* 如果确定要删除该记录 */
{
if((fp=fopen("inform.txt","rb"))==NULL)
{
printf("\n\nfile open wrong!");
getch();
exit(1);
}
if((fp1=fopen("info_temp.txt","wb"))==NULL)
{
printf("\n\nfile open wrong!");
getch();
exit(1);
}
total=0;
amount=load("inform.txt");
for(con=0; con<amount; con++)
{
if(total==offset-1)
fseek(fp,(long)sizeof(struct Info),1);
fread(&info, sizeof(struct Info),1,fp);
fwrite(&info, sizeof(struct Info), 1, fp1);
}
fclose(fp1);
fclose(fp);
if((fp1=fopen("info_temp.txt","rb"))==NULL)
{
printf("\n\nfile open wrong!");
getch();
exit(1);
}
if((fp=fopen("inform.txt","wb"))==NULL)
{
printf("\n\nfile open wrong!");
getch();
exit(1);
}
amount=load("info_temp.txt");
for(con=0; con<amount; con++)
{
fread(&info, sizeof(struct Info),1,fp1);
fwrite(&info, sizeof(struct Info), 1, fp);
}
fclose(fp);
fclose(fp1);
}
}
else /* 待删除的记录不存在 */
{
printf("\n\nThe record you enter is not found!\tany key to continue...");
getch();
}
printf("\n\nDo you want to continue?:\t\t1).Yes\t2).No [ ]\b\b");
scanf("%d",&flag); getchar();
}while(flag == 1);
menu();
}
void menu() /*菜单选择界面*/
{
int n, w1; /* n记录选择的菜单, w1记录选择是否合法 */
do
{
clrscr(); /*清屏*/
puts("\t\t\t Welcome to employee mangement system ");
puts("\t\t*********************MENU*********************\n\n");
puts("\t\t\t\t1.Append inform");
puts("\t\t\t\t2.Display inform");
puts("\t\t\t\t3.Search inform");
puts("\t\t\t\t4.Modify inform");
puts("\t\t\t\t5.Delete inform");
puts("\t\t\t\t6.Exit");
puts("\n\n\t\t**********************************************\n");
printf("Choice your number(1-6): [ ]\b\b");
scanf("%d",&n);
getchar();
if(n<1||n>6) /*对选择的数字作判断*/
{
w1=1;
getchar();
}
else
w1=0;
} while(w1==1);
switch(n) /*菜单选择功能*/
{
case 1: append(); break; /*信息添加模块*/
case 2: display(); break; /*信息显示模块*/
case 3:search(); break; /*信息查询模块*/
case 4:modify(); break; /*信息修改模块*/
case 5:delete(); break; /*信息删除模块*/
case 6:exit(0); /*退出*/
}
}
main()
{
menu();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -