📄 workin.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum Sex{ male, female }; /* 性别 */
enum Education{ high, junior, college, master, doctor}; /* 学历 */
struct Date /* 日期 */
{
int year;
int month;
int day;
};
struct Info
{
char num[5]; /* 职工号 */
char name[8]; /* 姓名 */
enum Sex sex; /* 性别 */
struct Date birthday; /* 出生年月*/
enum Education education; /* 学历 */
char duty[8]; /* 职务 */
double wage; /* 工资 */
char addr[12]; /* 地址 */
char phone[8]; /* 电话 */
};
void menu();
int load(char * a) /*计算参数所指定的文件中的记录的个数的函数*/
{
struct Info info;
FILE *fp;
int i;
if((fp=fopen(a,"rb"))==NULL)
{
printf("\nCannot open file\n");
getch();
return 0;
}
for(i=0;!feof(fp);i++)
fread(&info,sizeof(struct Info),1,fp);
fclose(fp);
return(i-1); /*返回记录个数*/
}
void append() /* 信息添加函数 */
{
struct Info info;
FILE * fp;
int flag = 1;
char temp[10];
if((fp=fopen("inform.txt","ab")) == NULL)
{
if((fp=fopen("inform.txt","wb"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
}
do
{
printf("\tnum:");
gets(info.num);
printf("\tname:");
gets(info.name);
printf("\tsex:");
gets(temp);
if(!strcmp(temp,"female"))
info.sex=female;
else
info.sex=male;
printf("\tbirthday(yyyy/mm/dd):");
scanf("%d/%d/%d",&info.birthday.year,&info.birthday.month,&info.birthday.day);
getchar();
printf("\teducation:");
gets(temp);
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("\tduty:");
gets(info.duty);
printf("\twage:");
gets(temp);
info.wage=atof(temp);
printf("\taddress:");
gets(info.addr);
printf("\tphone:");
gets(info.phone);
if(fwrite(&info, sizeof(info), 1, fp) != 1)
{
printf("\nfile input wrong!\n");
getch();
exit(1);
}
printf("Any more ? \t1).Yes\t2).No: [ ]\b\b");
scanf("%d",&flag);
getchar();
}while(flag == 1);
fclose(fp);
menu();
}
void display() /* 职工信息显示函数 */
{
int amount; /* 信息文件中的记录总数 */
struct Info info;
FILE * fp;
int total = 0;
char temp[10], temp1[10];
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( ;total<amount; )
{
if(fread(&info, sizeof(info), 1, fp) != 1)
{
printf("\nfile read wrong!\n");
getch();
exit(1);
}
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");
printf("%-5s%-8s%-8s%-11s%-10s%-8s%-8s%-12s%-8s\n","nun","name","sex ","birthday","education","duty","wage","address","phone");
}
}
fclose(fp);
printf("\n\n\tThere are %d record in all!",total);
getch();
menu();
}
void search() /* 信息查询函数 */
{
int amount; /* 信息文件中的记录总数 */
int con=0; /* 已处理的文件记录总数 */
struct Info info;
FILE * fp;
int flag=-1, flag1=-1; /* flag为1按工资查询,flag为2按学历查询;flag1为0表等于,为1表大于,为2表小于 */
int total=0; /*记录符合条件的记录的个数 */
char temp[10], temp1[10], degree[10], express[10];
char operator;
double value;
enum Education grade1;
int i=0; /* 读入表达式时用于字符串到数值的转换 及 控制学历的合法输入 */
amount=load("inform.txt");
do
{
printf("\n\nChoice your item that to search by: \t1).wage \t2).education: [ ]\b\b");
scanf("%d",&flag);
getchar();
}while(!(flag==1 ||flag==2));
if(flag==1) /* 按工资进行查询 */
{
flag1=-1;
do
{
printf("Please input the relative expression(>value or <value or =value):");
gets(express);
i=1;
do
{
temp[i-1]=express[i];
i++;
}while(express[i-1] != '\0');
operator = express[0];
value = atof(temp);
if(operator=='>') flag1=1;
else if(operator=='<') flag1=2;
else if(operator=='=') flag1=0;
else printf("\nthe express format must be >value or <value or =value, please input again...\n");
}while(!(flag1==1 || flag1==2 || flag1==0));
if((fp=fopen("inform.txt","rb")) == NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
total=0; /* 符合条件的记录数 */
for(con=0; con<amount; con++)
{
if(fread(&info, sizeof(info), 1, fp) != 1)
{
printf("\nfile read wrong!\n");
getch();
exit(1);
}
if(flag1==0 && info.wage==value)
{
total++;
if(total == 1) printf("%-5s%-8s%-8s%-11s%-10s%-8s%-8s%-12s%-8s\n","nun","name","sex ","birthday","education","duty","wage","address","phone");
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");
printf("%-5s%-8s%-8s%-11s%-10s%-8s%-8s%-12s%-8s\n","nun","name","sex ","birthday","education","duty","wage","address","phone");
}
}
else if(flag1==1 && info.wage>value)
{
total++;
if(total == 1)
printf("%-5s%-8s%-8s%-11s%-10s%-8s%-8s%-12s%-8s\n","nun","name","sex ","birthday","education","duty","wage","address","phone");
if(info.sex==male)
strcpy(temp,"male");
else
strcpy(temp,"female");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -