职工信息统计系统
#include<stdio.h> #include<stdlib.h>
#include<string.h>
typedef struct
{
char num;
char name;
char sex;
char year;
char stud;
char job;
char pay;
char adds;
char call;
}mem;
mem peo[100];
int menu_select()
{
char c;
do{
system("cls");
printf("\t\t *****职工信息管理系统***** \n");
printf("\t\t┌───────┐\n");
printf("\t\t│ 1. 录入信息 │\n");
printf("\t\t│ 2. 显示信息 │\n");
printf("\t\t│ 3. 删除信息 │\n");
printf("\t\t│ 4. 查询信息 │\n");
printf("\t\t│ 5. 修改信息 │\n");
printf("\t\t│ 6. 保存信息 │\n");
printf("\t\t│ 0. 退出程序 │\n");
printf("\t\t└───────┘\n");
printf("\t\t请您选择(0-6):");
c=getchar();
}while(c<'0'||c>'6');
return(c-'0');
}
int Input(mem peo[],int n)
{
int i=0;
char sign,x[10];
while(sign!='n'&&sign!='N')
{
printf("number:");
scanf("%d",&peo[n+i].num);
printf("name:");
scanf("%s",peo[n+i].name);
printf("sex:");
scanf("%s",peo[n+i].sex);
printf("year:");
scanf("%s",peo[n+i].year);
printf("acad:");
scanf("%s",peo[n+i].stud);
printf("post:");
scanf("%s",peo[n+i].job);
printf("pay:");
scanf("%s",peo[n+i].pay);
printf("address:");
scanf("%s",peo[n+i].adds);
printf("phone:");
scanf("%s",peo[n+i].call);
gets(x);
printf("\n是否继续添加?(Y/N)");
scanf("%c",&sign);
i++;
}
return(n+i);
}
void Display(mem peo[],int n)
{
int i;
printf("----------------------------------------------------------------------\n"); /*格式*/
printf("职工号 姓名 性别 出生年月 学历 职务 工资 住址 电话\n");
printf("----------------------------------------------------------------------\n");
for(i=1;i<n+1;i++)
{
printf("%-5d%-8s%-6s%-13s%-15s%-15s%-15s%-15s%-15s%\n",peo[i-1].num,peo[i-1].name,peo[i-1].sex,peo[i-1].stud,peo[i-1].job,peo[i-1].pay,peo[n+i].adds,peo[n+i].call);
if(i>1&&i%10==0)
{
printf("\t-----------------------------------\n");
printf("\t");
system("pause");
printf("\t-----------------------------------\n");
}
}
printf("----------------------------------------------------------------------\n");
system("pause");
}
int Delete_a_record(mem peo[],int n)
{
char s[20];
int i=0,j;
printf("\t请输入想删除记录中的名字:");
scanf("%s",s);
while(strcmp(peo[i].name,s)!=0&&i<n) i++;
if(i==n)
{
printf("\t通讯录中没有此人!\n");
return(n);
}
for(j=i;j<n-1;j++)
{
strcpy(peo[j].num,peo[j+1].num);
strcpy(peo[j].name,peo[j+1].name);
strcpy(peo[j].sex,peo[j+1].sex);
strcpy(peo[j].year,peo[j+1].year);
strcpy(peo[j].stud,peo[j+1].stud);
strcpy(peo[j].job,peo[j+1].job);
strcpy(peo[j].pay,peo[j+1].pay);
strcpy(peo[j].adds,peo[j+1].adds);
strcpy(peo[j].call,peo[j+1].call);
per[j].score=per[j+1].score;
}
printf("\t\t\t已经成功删除!\n");
return(n-1);
} void Query_a_record(mem peo[],int n)
{
int m;
printf("\t\n请选择查询方式:\n");
printf("\t┌──────┐\n");
printf("\t│1------姓名 │\n");
printf("\t│2------电话 │\n");
printf("\t│3------地址 │\n");
printf("\t│4------返回 │\n");
printf("\t└──────┘\n");
printf("请选择:");
scanf("%d",&m);
while(m!=1&&m!=2&&m!=3&&m!=4)
{
printf("输入错误,请重新选择:");
scanf("%d",&m);
}
if(m==1)
{
char s[20];
int i=0;
printf("\t请输入想查询的姓名:");
scanf("\t%s",s);
while(strcmp(peo[i].name,s)!=0&&i<n) i++;
if(i==n)
{
printf("\t记录中没有此人!\n");
return;
}
printf("\t此职工的性别为: %s\n",,peo[j+1].sex);
printf("\t此职工的出生年月日为: %s\n", peo[j+1].year);
printf("\t此职工的学历为: %s\n",peo[j+1].stud);
printf("\t此职工的职务为: %s\n",peo[j+1].job);
printf("\t此职工的工资为: %s\n",peo[j+1].pay);
printf("\t此职工的地址为: %s\n",peo[j+1].adds);
printf("\t此职工的电话为: %s\n",peo[j+1].call);
} ;
if(m==2)
{
char s[20];
int i=0;
printf("\t请输入想查询的电话:");
scanf("\t%s",s);
while(strcmp(peo[i].num,s)!=0&&i<n) i++;
if(i==n)
{
printf("\t记录中没有此人!\n");
return;
}
printf("\t 此职工的性别为: %s\n",,peo[j+1].sex);
printf("\t此职工的出生年月日为: %s\n", peo[j+1].year);
printf("\t此职工的学历为: %s\n",peo[j+1].stud);
printf("\t此职工的职务为: %s\n",peo[j+1].job);
printf("\t此职工的工资为: %s\n",peo[j+1].pay);
printf("\t此职工的地址为: %s\n",peo[j+1].adds);
} ;
if(m==3)
{
char s[20];
int i=0;
printf("\t请输入想查询的地址:");
scanf("\t%s",s);
while(strcmp(peo[i].adds,s)!=0&&i<n) i++;
if(i==n)
{
printf("\t记录中没有此人!\n");
return;
}
printf("\t 此职工的性别为: %s\n",,peo[j+1].sex);
printf("\t此职工的出生年月日为: %s\n", peo[j+1].year);
printf("\t此职工的学历为: %s\n",peo[j+1].stud);
printf("\t此职工的职务为: %s\n",peo[j+1].job);
printf("\t此职工的工资为: %s\n",peo[j+1].pay);
printf("\t此职工的电话为: %s\n",peo[j+1].call);
} ;
}
void Change(Person peo[],int n)
{
char s[20];
int i=0;
printf("\t请输入想修改的记录中的名字:");
scanf("%s",s);
while(strcmp(peo[i].name,s)!=0&&i<n) i++;
if(i==n)
{
printf("\t记录中没有此人!\n");
return;
}
printf("\t职工号:");
scanf("\t%d",&peo[n+i].num);
printf("\t姓名:");
scanf("\t%s",peo[n+i].name);
printf("\t性别:");
scanf("\t%s",peo[n+i].sex);
printf("\t出生年月:");
scanf("\t%s",peo[n+i].year);
printf("\t学历:");
scanf("\t%s",peo[n+i].stud);
printf("\t职务:");
scanf("\t%s",peo[n+i].job);
printf("\t工资:");
scanf("\t%s",peo[n+i].pay);
printf("\t住址:");
scanf("\t%s",peo[n+i].adds);
printf("\t电话:");
scanf("\t%s",peo[n+i].call);
printf("\t修改成功!");
}
void WritetoText(mem peo[],int n)
{
int i=0;
FILE *fp; /*定义文件指针*/
char filename[20]; /*定义文件名*/
printf("\t保存到文件\n"); /*输入文件名*/
printf("\t请输入所保存的文件名:");
scanf("\t%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\t无法打开文件\n");
system("pause");
return;
}
fprintf(fp,"********************************职工信息管理系统*************************************\n");
fprintf(fp,"职工号 姓名 性别 出生年月 学历 职务 工资 住址 电话\n");
fprintf(fp,"------------------------------------------------------------------------------------------\n");
while(i<n)
{
fprintf(fp,"%-3d\t%-6s\t"%-5d%-8s%-6s%-13s%-15s%-15s%-15s%-15s%-15s%\n",peo[i-1].num,peo[i-1].name,peo[i-1].sex,peo[i-1].stud,peo[i-1].job,peo[i-1].pay,peo[n+i].adds,peo[n+i].call);
i++;
}
fprintf(fp,"------------------------------------------------------------------------------------------\n");
fprintf(fp,"***************************************共有%d条记录****************************************\n",n);
fclose(fp); /*关闭文件*/
printf("保存成功!\n");
}
void main() /*主函数*/
{
int n=0;
for(;;)
{
switch(menu_select())
{
case 1:
printf("\n\t添加记录到职工信息记录中\n"); /*添加记录*/
n=Input(pe,n);
break;
case 2:
printf("\n\t\t\t 职工信息记录表\n"); /*显示记录*/
Display(pe,n);
break;
case 3:
printf("\n\t从职工信息统计表中删除记录\n");
n=Delete_a_record(pe,n); /*删除记录*/
printf("\t");
system("pause");
break;
case 4:
printf("\n\t在职工信息表中查找记录\n");
Query_a_record(pe,n); /*查找记录*/
printf("\t");
system("pause");
break;
case 5:
printf("\n\t修改职工信息表中的记录\n");
Change(pe,n); /*修改数据*/
printf("\t");
system("pause");
break;
case 6:
printf("\n\t保存功能\n");
WritetoText(pe,n); /*保存数据*/
printf("\t");
system("pause");
break;
case 0:
printf("\n\t\t谢谢使用,再见!\n"); /*结束程序*/
printf("\n\t\t");
//system("pause");
exit(0);
}
}
}