You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
6.2 KiB
274 lines
6.2 KiB
#include<stdio.h>
|
|
#include<string.h>
|
|
#include<stdlib.h>
|
|
struct stu /*定义结构体数组*/
|
|
{
|
|
char num[15];
|
|
char banji[10];
|
|
char name[25];
|
|
int hour1;
|
|
int hour2;
|
|
int min1;
|
|
int min2;
|
|
}st[99];
|
|
void menu() /*菜单*/
|
|
{
|
|
printf("\t**************************\n");
|
|
printf("\t****1---输入数据**********\n");
|
|
printf("\t****2---计算费用**********\n");
|
|
printf("\t****3---删除数据**********\n");
|
|
printf("\t****4---查询数据**********\n");
|
|
printf("\t**************************\n");
|
|
printf("\t输入相应数字选择以回车结束\n");
|
|
}
|
|
void new_students(int *p);
|
|
void calculate(struct stu st[],int *q);
|
|
void del(int *p);
|
|
void search(int *p);
|
|
void main() /*主函数*/
|
|
{
|
|
int b=0;
|
|
int a,*p;
|
|
FILE *fpp; /*定义文件指针*/
|
|
p=&b;
|
|
if((fpp=fopen("stu_num.txt","r"))==NULL)
|
|
{
|
|
printf("无历史记录");
|
|
system("pause");
|
|
}
|
|
else
|
|
{
|
|
fread(p,4,1,fpp);
|
|
fclose(fpp);
|
|
}
|
|
while(1)
|
|
{
|
|
system("cls");
|
|
menu();
|
|
scanf("%d",&a);
|
|
switch(a)
|
|
{
|
|
case 1:
|
|
new_students(p); break;
|
|
case 2:
|
|
calculate(st,p); break;
|
|
case 3:
|
|
del(p); break;
|
|
case 4:
|
|
search(p); break;
|
|
case 5:
|
|
{
|
|
system("pause");
|
|
exit(0);
|
|
}
|
|
default :
|
|
printf("continue\n");
|
|
}
|
|
system("pause");
|
|
}
|
|
}
|
|
void new_students(int *p) /*输入函数*/
|
|
{
|
|
int i;
|
|
FILE *fp;
|
|
FILE *fpp;
|
|
for(i=*p;i<*p+1;i++)
|
|
{
|
|
printf("请输入信息!\n");
|
|
printf("学号为:");
|
|
scanf("%s",&st[i].num);
|
|
fflush(stdin);
|
|
printf("班级为:");
|
|
scanf("%s",&st[i].banji);
|
|
printf("姓名为:");
|
|
scanf("%s",&st[i].name);
|
|
printf("上机小时为:");
|
|
scanf("%d",&st[i].hour1);
|
|
printf("上机分钟为:");
|
|
scanf("%d",&st[i].min1);
|
|
printf("下机小时为:");
|
|
scanf("%d",&st[i].hour2);
|
|
printf("下机机分钟为:");
|
|
scanf("%d",&st[i].min2);
|
|
}
|
|
if(*p==0)
|
|
{
|
|
if((fp=fopen("stu_menu.txt","w"))==NULL)
|
|
printf("cannot open file");
|
|
}
|
|
else
|
|
{
|
|
fp=fopen("stu_menu.txt","r+");
|
|
for(i=0;i<*p;i++)
|
|
fread(&st[i],sizeof(struct stu),1,fp); /*写入文件*/
|
|
}
|
|
for(i=0;i<*p+1;i++)
|
|
if(fwrite(&st[i],sizeof(struct stu),1,fp)!=1)
|
|
fclose(fp);
|
|
*p=*p+1;
|
|
if((fpp=fopen("stu_num.txt","w"))==NULL)
|
|
{
|
|
printf("cannot open file");
|
|
return;
|
|
}
|
|
if(fwrite(p,4,1,fpp)!=1)
|
|
printf("file write error");
|
|
fclose(fpp);
|
|
}
|
|
void calculate(struct stu st[],int *q)
|
|
{
|
|
int i,j;
|
|
FILE *fp;
|
|
char a;
|
|
fp=fopen("stu_menu.txt","r"); /*打开文件*/
|
|
for(i=0;i<*q;i++)
|
|
{
|
|
fread(&st[i],sizeof(struct stu),1,fp);
|
|
system("cls");
|
|
printf("学号为:%s\t 班级为:%s\t 姓名为:%s\t",st[i].num,st[i].banji,st[i].name);
|
|
printf("\t上机时间为:%d时%d分",st[i].hour1,st[i].min1);
|
|
printf("\t下机时间为:%d时%d分",st[i].hour2,st[i].min2);
|
|
if(st[i].min1<=st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1)*60+(st[i].min2-st[i].min1);
|
|
else if(st[i].min1>st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1-1)*60+(60-st[i].min1+st[i].min2);
|
|
else if(st[i].min1==0&&st[i].min2==0)
|
|
j=(st[i].hour2-st[i].hour1)*60;
|
|
printf("费用为:%d元",j);
|
|
}
|
|
fflush(stdin);
|
|
a=getchar();
|
|
system("cls");
|
|
}
|
|
void del(int *p) /*删除函数*/
|
|
{
|
|
int i,j;
|
|
char a[9];
|
|
FILE *fp;
|
|
FILE *fpp;
|
|
fp=fopen("stu_menu.txt","r+");
|
|
for(i=0;i<*p;i++)
|
|
fread(&st[i],sizeof(struct stu),1,fp);
|
|
printf("请输入要删除的名字");
|
|
scanf("%s",a);
|
|
for(i=0;i<*p;i++)
|
|
{
|
|
if(strcmp(a,st[i].name)==0)
|
|
{
|
|
for(j=i;j<*p-1;j++)
|
|
{
|
|
strcpy(st[j].num,st[j+1].num);
|
|
strcpy(st[j].banji,st[j+1].banji);
|
|
st[j].hour1=st[j+1].hour1;
|
|
st[j].min1=st[j+1].min1;
|
|
st[j].hour2=st[j+1].hour2;
|
|
st[j].min2=st[j+1].min2;
|
|
strcpy(st[j].name,st[j+1].name);
|
|
}
|
|
*p=*p-1;
|
|
}
|
|
}
|
|
for(i=0;i<*p;i++)
|
|
fwrite(&st[i],sizeof(struct stu),1,fp);
|
|
fclose(fp);
|
|
if((fpp=fopen("stu_num.txt","w"))==NULL)
|
|
{
|
|
printf("cann't open file");
|
|
return;
|
|
}
|
|
if(fwrite(p,4,1,fp)!=1)
|
|
printf("file write error");
|
|
fclose(fpp);
|
|
printf("该生信息已经不存在");
|
|
}
|
|
void search(int *p) /*查询数据*/
|
|
{
|
|
int a,i,c=1,j;
|
|
char b[9];
|
|
FILE *fp;
|
|
fp=fopen("stu_menu.txt","r");
|
|
for(i=0;i<*p;i++)
|
|
fread(&st[i],sizeof(struct stu),1,fp);
|
|
fclose(fp);
|
|
printf("**请输入相应数字选择查询的条件**\n");
|
|
printf("1:学号 2:姓名 3:班级\n");
|
|
scanf("%d",&a);
|
|
switch(a) /*选择查找方式*/
|
|
{
|
|
case 1:
|
|
{
|
|
printf("学号");
|
|
scanf("%s",b);
|
|
for(i=0;i<*p;i++)
|
|
if(strcmp(b,st[i].num)==0)
|
|
{
|
|
printf("学号为:%s 班级为:%s 姓名为:%s",st[i].num,st[i].banji,st[i].name);
|
|
printf("\t上机时间为:%d时%d分",st[i].hour1,st[i].min1);
|
|
printf("\t下机时间为:%d时%d分",st[i].hour2,st[i].min2);
|
|
if(st[i].min1<=st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1)*60+(st[i].hour2-st[i].hour1);
|
|
else if(st[i].min1>st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1-1)*60+(60-st[i].min1+st[i].min2);
|
|
else if(st[i].min1==0&&st[i].min2==0)
|
|
j=st[i].hour2-st[i].hour1;
|
|
printf("费用为:%d元",j);
|
|
c=0;
|
|
}
|
|
if(c==1)
|
|
{
|
|
printf("没有相关消息");
|
|
}
|
|
} break;
|
|
case 2:
|
|
{
|
|
printf("姓名");
|
|
scanf("%s",b);
|
|
for(i=0;i<*p;i++)
|
|
if(strcmp(b,st[i].name)==0)
|
|
{
|
|
printf("学号为:%s 班级为:%s 姓名为:%s",st[i].num,st[i].banji,st[i].name);
|
|
printf("\t上机时间为:%d时%d分",st[i].hour1,st[i].min1);
|
|
printf("\t下机时间为:%d时%d分",st[i].hour2,st[i].min2);
|
|
if(st[i].min1<=st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1)*60+(st[i].min2-st[i].min1);
|
|
else if(st[i].min1>st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1-1)*60+(60-st[i].min1+st[i].min2);
|
|
else if(st[i].min1==0&&st[i].min2==0)
|
|
j=st[i].hour2-st[i].hour1;
|
|
printf("费用为:%d元",j);
|
|
c=0;
|
|
}
|
|
if(c==1)
|
|
{
|
|
printf("没有相关消息");
|
|
}
|
|
} break;
|
|
case 3:
|
|
{
|
|
printf("班级");
|
|
scanf("%s",b);
|
|
for(i=0;i<*p;i++)
|
|
if(strcmp(b,st[i].banji)==0)
|
|
{
|
|
printf("学号为:%s 班级为:%s 姓名为:%s",st[i].num,st[i].banji,st[i].name);
|
|
printf("\t上机时间为:%d时%d分",st[i].hour1,st[i].min1);
|
|
printf("\t下机时间为:%d时%d分",st[i].hour2,st[i].min2);
|
|
if(st[i].min1<=st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1)*60+(st[i].min2-st[i].min1);
|
|
else if(st[i].min1>st[i].min2)
|
|
j=(st[i].hour2-st[i].hour1-1)*60+(60-st[i].min1+st[i].min2);
|
|
else if(st[i].min1==0&&st[i].min2==0)
|
|
j=(st[i].hour2-st[i].hour1)*60;
|
|
printf("费用为:%d元",j);
|
|
c=0;
|
|
}
|
|
if(c==1)
|
|
{
|
|
printf("没有相关消息");
|
|
}
|
|
} break;
|
|
default :
|
|
printf("没有相关信息");
|
|
}
|
|
}
|