显示数据的时候做了一下优化,入校/出校不是显示的0/1,而是直接用中文显示的,看着更直观。
运行结果:
(1)
(2)
(3)
(4)
(5)
(6)
(7)
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OUT_SCHOOL (int)0
#define IN_SCHOOL (int)1
enum eType
{
e_student=1,
e_teacher=2,
e_other
};
typedef struct _date
{
int y,m,d;
}Date;
typedef struct _person
{
eType type;
char name[20];
char id[10];
char phone[12];
char sfz[20];
int op;
Date time;
}Person;
void save(Person ps[],int n);
int menu()
{
int op;
system("cls");
printf("************欢迎使用校园出入管理系统**************\n");
printf("--------------------------------------------------\n");
printf("---------1.录入出入校园人员信息-------------------\n");
printf("---------2.保存出入校园人员信息-------------------\n");
printf("---------3.浏览出入校园人员信息-------------------\n");
printf("---------4.查询出入校园人员信息-------------------\n");
printf("---------5.增加出入校园人员信息-------------------\n");
printf("---------6.删除出入校园人员信息-------------------\n");
printf("---------7.修改出入校园人员信息-------------------\n");
printf("---------8.统计出入校园人员信息-------------------\n");
printf("---------9.对出入校园人员信息进行排序-------------\n");
printf("---------10.退出----------------------------------\n");
printf("--------------------------------------------------\n");
printf("*****************谢谢使用*************************\n");
printf("请选择(1-10): ");
scanf("%d",&op);
return op;
}
int input(Person ps[],int n)
{
int flag = 0;
int t;
while(1)
{
system("cls");
while(1)
{
printf("请输入人员身份(1.学生 2.教职工 3.其他):");
scanf("%d",&t);
if(t == 1)
{
ps[n].type = e_student;
printf("请输入学号:");
scanf("%s",ps[n].id);
break;
}else if(t == 2)
{
ps[n].type = e_teacher;
printf("请输入教职工号:");
scanf("%s",ps[n].id);
break;
}else if(t == 3)
{
ps[n].type = e_other;
ps[n].id[0] = 0;
break;
}else
printf("输入错误!");
}
printf("请输入姓名:");
scanf("%s",ps[n].name);
printf("请输入联系方式:");
scanf("%s",ps[n].phone);
printf("请输入身份证号:");
scanf("%s",ps[n].sfz);
printf("0.出校 1.入校:");
scanf("%d",&ps[n].op);
if(ps[n].op == OUT_SCHOOL)
printf("请输入离校时间(YYYY/MM/DD):");
else
printf("请输入入校时间(YYYY/MM/DD):");
scanf("%d/%d/%d",&ps[n].time.y,&ps[n].time.m,&ps[n].time.d);
printf("录入成功!\n");
n++;
while(1)
{
printf("是否继续录入(1.是 0.否):");
scanf("%d",&flag);
if(flag == 0 || flag == 1)
break;
}
if(flag == 0)
break;
}
save(ps,n);
return n;
}
void save(Person ps[],int n)
{
FILE* fp = fopen("data.txt","w");
for(int i = 0;i<n;i++)
{
if(ps[i].type == e_student)
fprintf(fp,"1 %s %s %s %s %d %d %d %d\n",ps[i].name,ps[i].id,ps[i].phone,ps[i].sfz,ps[i].op,ps[i].time.y,ps[i].time.m,ps[i].time.d);
else if(ps[i].type == e_teacher)
fprintf(fp,"2 %s %s %s %s %d %d %d %d\n",ps[i].name,ps[i].id,ps[i].phone,ps[i].sfz,ps[i].op,ps[i].time.y,ps[i].time.m,ps[i].time.d);
else
fprintf(fp,"1 %s %s %s %s %d %d %d %d\n",ps[i].name,"无",ps[i].phone,ps[i].sfz,ps[i].op,ps[i].time.y,ps[i].time.m,ps[i].time.d);
}
fclose(fp);
printf("数据已保存到data.txt中!\n");
system("pause");
}
void showSingle(Person ps)
{
if(ps.type == e_student)
{
if(ps.op == OUT_SCHOOL)
printf("%-6s %-10s %-9s %s %s %s %d/%d/%d\n","学生",ps.name,ps.id,ps.phone,ps.sfz,"出校",ps.time.y,ps.time.m,ps.time.d);
else
printf("%-6s %-10s %-9s %s %s %s %d/%d/%d\n","学生",ps.name,ps.id,ps.phone,ps.sfz,"入校",ps.time.y,ps.time.m,ps.time.d);
}
else if(ps.type == e_teacher)
{
if(ps.op == OUT_SCHOOL)
printf("%-6s %-10s %-9s %s %s %s %d/%d/%d\n","教职工",ps.name,ps.id,ps.phone,ps.sfz,"出校",ps.time.y,ps.time.m,ps.time.d);
else
printf("%-6s %-10s %-9s %s %s %s %d/%d/%d\n","教职工",ps.name,ps.id,ps.phone,ps.sfz,"入校",ps.time.y,ps.time.m,ps.time.d);
}else
{
if(ps.op == OUT_SCHOOL)
printf("%-6s %-10s %-9s %s %s %s %d/%d/%d\n","其他",ps.name,"无",ps.phone,ps.sfz,"出校",ps.time.y,ps.time.m,ps.time.d);
else
printf("%-6s %-10s %-9s %s %s %s %d/%d/%d\n","其他",ps.name,"无",ps.phone,ps.sfz,"入校",ps.time.y,ps.time.m,ps.time.d);
}
}
void show(Person ps[],int n)
{
system("cls");
for(int i = 0;i<n;i++)
{
showSingle(ps[i]);
}
system("pause");
}
void search(Person ps[],int n)
{
int i,t;
int op;
char sel;
char tmp[20];
int flag = 0;
while(1)
{
system("cls");
flag = 0;
rewind(stdin);
printf("A.按人员身份查询\n");
printf("B.按姓名查询\n");
printf("C.按出/入校查询\n");
printf("D.退出\n");
printf("请选择(A-D):");
sel = getchar();
getchar();
switch(sel)
{
case 'A':
printf("请输入人员身份(1.学生 2.教职工 3.其他):");
scanf("%d",&t);
for(i=0;i<n;i++)
{
if(ps[i].type == t)
{
flag = 1;
showSingle(ps[i]);
}
}
if(flag == 0)
printf("未找到符合条件的人员信息\n");
break;
case 'B':
printf("请输入人员姓名:");
scanf("%s",tmp);
for(i=0;i<n;i++)
{
if(strcmp(ps[i].name,tmp) == 0)
{
flag = 1;
showSingle(ps[i]);
}
}
if(flag == 0)
printf("未找到符合条件的人员信息\n");
break;
case 'C':
printf("1.出校 2.入校 \n请选择:");
scanf("%d",&op);
for(i=0;i<n;i++)
{
if(ps[i].op == op)
{
flag = 1;
showSingle(ps[i]);
}
}
if(flag == 0)
printf("未找到符合条件的人员信息\n");
break;
case 'D':
return ;
}
system("pause");
}
}
int add(Person ps[],int n)
{
return input(ps,n);
}
int del(Person ps[],int n)
{
int i,j;
char name[20],sfz[20];
int op;
int y,m,d;
char ch;
system("cls");
rewind(stdin);
printf("输入姓名、身份证号、出/入校、出/入校时间: ");
scanf("%s %s %d %d/%d/%d",name,sfz,&op,&y,&m,&d);
for(i=0;i<n;i++)
{
if(strcmp(ps[i].name,name)==0 && strcmp(ps[i].sfz,sfz)==0 && ps[i].op==op && ps[i].time.y ==y && ps[i].time.m ==m && ps[i].time.d ==d)
{
showSingle(ps[i]);
break;
}
}
if(i==n)
{
printf("没有该人员信息\n");
system("pause");
}
else
{
while(1)
{
printf("确认删除(y/n)? ");
rewind(stdin);
ch = getchar();
if(ch =='y' || ch=='Y')
{
for(j=i;j<n-1;j++)
ps[j] = ps[j+1];
printf("删除成功!\n");
n--;
break;
}
}
save(ps,n);
}
return n;
}
void change(Person ps[],int n)
{
int i,j,t;
char name[20],sfz[20];
int op;
int y,m,d;
char ch;
system("cls");
rewind(stdin);
printf("输入姓名、身份证号、出/入校、出/入校时间: ");
scanf("%s %s %d %d/%d/%d",name,sfz,&op,&y,&m,&d);
for(i=0;i<n;i++)
{
if(strcmp(ps[i].name,name)==0 && strcmp(ps[i].sfz,sfz)==0 && ps[i].op==op && ps[i].time.y ==y && ps[i].time.m ==m && ps[i].time.d ==d)
{
showSingle(ps[i]);
break;
}
}
if(i==n)
{
printf("没有该人员信息\n");
system("pause");
}
else
{
rewind(stdin);
while(1)
{
printf("是否修改身份类型(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入人员身份(1.学生 2.教职工 3.其他):");
scanf("%d",&t);
if(t==1)
ps[i].type = e_student;
else if(t==2)
ps[i].type = e_teacher;
else if(t == 3)
ps[i].type = e_other;
break;
}
}
rewind(stdin);
printf("是否修改姓名(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入姓名:");
scanf("%s",ps[i].name);
}
rewind(stdin);
if(ps[i].type == e_student)
{
printf("是否修改学号(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入学号:");
scanf("%s",ps[i].id);
}
}else if(ps[i].type == e_teacher)
{
printf("是否修改教职工编号(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入教职工编号:");
scanf("%s",ps[i].id);
}
}
rewind(stdin);
printf("是否修改联系方式(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入联系方式:");
scanf("%s",ps[i].phone);
}
rewind(stdin);
printf("是否修改身份证号(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入身份证号:");
scanf("%s",ps[i].sfz);
}
rewind(stdin);
printf("是否修改出/入校(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入0.出校 / 1.入校:");
scanf("%d",&ps[i].op);
}
rewind(stdin);
printf("是否修改出/入校时间(y/n):");
ch = getchar(); getchar();
if(ch =='y' || ch=='Y')
{
printf("请输入出入校时间(YYYY/MM/DD):");
scanf("%d/%d/%d",&ps[i].time.y,&ps[i].time.m,&ps[i].time.d);
}
printf("修改成功!\n");
save(ps,n);
}
}
void tongji(Person ps[],int n)
{
int i;
int a[3]={0};
int op;
system("cls");
printf("输入0.出校 1.入校:");
scanf("%d",&op);
for(i=0;i<n;i++)
{
if(ps[i].op == op)
{
if(ps[i].type == e_student)
a[0]++;
else if(ps[i].type == e_teacher)
a[1]++;
else if(ps[i].type == e_other)
a[2]++;
}
}
if(op == OUT_SCHOOL)
printf("出校统计结果: ");
else
printf("入校统计结果: ");
printf("学生%d人,教职工%d人,其他人%d人\n",a[0],a[1],a[2]);
for(i=0;i<n;i++)
{
if(ps[i].op == op)
{
showSingle(ps[i]);
}
}
system("pause");
}
int cmp(Date d1,Date d2)
{
if(d1.y < d2.y)
return -1;
else if(d1.y > d2.y)
return 1;
else
{
if(d1.m < d2.m)
return -1;
else if(d1.m > d2.m)
return 1;
else
{
if(d1.d < d2.d)
return -1;
else if(d1.d > d2.d)
return 1;
else
return 0;
}
}
}
void sortByName(Person ps[],int n)
{
int i,j;
Person t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(strcmp(ps[j].name,ps[j+1].name)> 0)
{
t = ps[j];
ps[j] = ps[j+1];
ps[j+1] = t;
}else if(strcmp(ps[j].name,ps[j+1].name)== 0)
{
if(ps[j].op == IN_SCHOOL && ps[j+1].op == IN_SCHOOL)
{
if(cmp(ps[j].time,ps[j+1].time) > 0)
{
t = ps[j];
ps[j] = ps[j+1];
ps[j+1] = t;
}
}
}
}
}
printf("排序后:\n");
show(ps,n);
}
int main()
{
int op;
Person ps[500];
int n = 0;
while(1)
{
op = menu();
switch(op)
{
case 1: n = input(ps,n);break;
case 2: save(ps,n); break;
case 3: show(ps,n);break;
case 4: search(ps,n);break;
case 5: n = add(ps,n);break;
case 6: n = del(ps,n);break;
case 7: change(ps,n);break;
case 8: tongji(ps,n);break;
case 9:sortByName(ps,n);break;
case 10:return 0;
}
}
return 0;
}