#include<stdio.h>
#include<malloc.h>
#include<string.h>
void print_1();
void print_2();
void print_3();
void setsign();
void search();
void search_num();
void search_day();
void up();
void down();
void chushihua();
void pr();
//struct use *creat();
typedef struct computer //定义计算机结构体
{
char use_name[20];
int day;
int num;
char use_num[20];
int sign;
int time_shi;
int time_fen;
}
COM;
void main() //主函数
{
chushihua(); //初始化计算机
// pr();
int flag;
for(;;) //使程序循环执行
{
print_1(); //输出主菜单
printf("\t请选择:");
scanf("%d",&flag);
if(flag==1)setsign(); //进行登记
if(flag==2)search(); //进行查找
if(flag==0)break; //退出
}
}
void setsign() //定义登记函数
{
int flag;
for(;;)
{
print_2(); //输出登记菜单
printf("\t请选择:");
scanf("%d",&flag);
if(flag==1)up(); //上机登记
if(flag==2)down();//下机登记
if(flag==0)return;//返回上一级
}
}
void search() //定义查找函数
{
int flag;
for(;;)
{
print_3();
printf("\t请选择:");
scanf("%d",&flag);
if(flag==1)search_num();//进行按序号查找
if(flag==2)search_day();//进行按星期查找
if(flag==0)return; //返回上一级
}
}
void up() //定义上机函数
{
COM cpu;
COM cp;
int i=1;
FILE *fp;
cpu.sign=1;
if((fp=fopen("11111","rb"))==NULL)//打开文件
{
printf("fail\n");
return;
}
printf("以下为您可使用的计算机:\n");
for(int k=1001;k<=1100;k++) //查找未在使用计算机
{
do
{
fseek(fp,-i*sizeof(COM),2);
if(fread(&cp,sizeof(COM),1,fp)!=1&&feof(fp)==0)
{
printf("error\n");
break;
}
if(feof(fp)==1)
{printf("\t\t未找到");return;}
i++;
}
while(k!=cp.num);
i=1;
if(cp.sign==0)
printf("%d ",k);
}
printf("\n\t请输入计算机编号(1001-1100),输入0退出:"); //用户选择
scanf("%d",&cpu.num);
if(cpu.num==0)
{
return;
}
// scanf("%d",&cpu.num);
if(cpu.num<1001||cpu.num>1100)
{
printf("\t输入错误,请重新输入\n");
up();
return;
}
i=1;
do
{
fseek(fp,-i*sizeof(COM),2);
if(fread(&cp,sizeof(COM),1,fp)!=1&&feof(fp)==0)
{
printf("error\n");
return;
}
if(feof(fp)==1)
{printf("\t\t未找到");return;}
i++;
}
while(cpu.num!=cp.num);
fclose(fp);
if(cp.sign==1)
{
printf("\t该计算机已在使用,请重新输入\n");
up();
return;
}
fp=fopen("11111","ab");
printf("\t请输入你的姓名,输入0退出:"); //用户输入信息
scanf("%s",cpu.use_name);
if(strcmp(cpu.use_name,"0")==0)
{
return;
}
printf("\t请输入你的学号,输入0退出:");
scanf("%s",cpu.use_num);
if(strcmp(cpu.use_num,"0")==0)
{
return;
}
printf("\t请输入今日星期(1-7),输入0退出:");
loop:scanf("%d",&cpu.day);
if(cpu.day==0)
{
return;
}
if(cpu.day<1||cpu.day>7)
{
printf("\t输入错误,请重新输入\n\t");
goto loop;
}
printf("\t请输入上机时间(0:00)");
aaa:scanf("%d:%d",&cpu.time_shi,&cpu.time_fen);
if(cpu.time_shi>23||cpu.time_shi<0||cpu.time_fen>59||cpu.time_fen<0||cpu.time_shi<cp.time_shi||(cpu.time_shi==cp.time_shi&&cp.time_fen>cpu.time_fen))
{
printf("\t输入错误,请重新输入\n\t");
goto aaa;
}
fwrite(&cpu,sizeof(COM),1,fp); //将信息写入文件
printf("\t登记完成\n");
fclose(fp);
}
void down() //定义下机函数
{
COM cpu,cp;
FILE *fp;
int i=1;
printf("\t请输入你的姓名,输入0退出:");//输入用户信息
scanf("%s",cpu.use_name);
if(strcmp(cpu.use_name,"0")==0)
return;
printf("\t请输入计算机序号,输入0退出::");
scanf("%d",&cpu.num);
if(cpu.num==0)
return;
if(cpu.num<1001||cpu.num>1100)
{
printf("\t输入错误,请重新输入\n");
up();
return;
}
if((fp=fopen("11111","rb"))==0)
{
printf("\t不能写入");
return;
}
do //查找用户是否为在线用户
{
fseek(fp,-i*sizeof(COM),2);
i++;
if(fread(&cp,sizeof(COM),1,fp)!=1)
{
printf("\terror\n");
return;
}
}
while(cpu.num!=cp.num);
printf("\t请输入下机时间(0:0)");
aaa:scanf("%d:%d",&cpu.time_shi,&cpu.time_fen);
if(cpu.time_shi>23||cpu.time_shi<0||cpu.time_fen>59||cpu.time_fen<0)
{
printf("\t输入错误,请重新输入\n\t");
goto aaa;
}
fclose(fp);
if(strcmp(cpu.use_name,cp.use_name)!=0||cpu.num!=cp.num||cp.sign!=1||cpu.time_shi<cp.time_shi||(cpu.time_shi==cp.time_shi&&cpu.time_fen<cp.time_fen))
{
printf("\t信息错误,请重新输入");
down();
return;
}
else
{
cpu.sign=0;
cpu.day=cp.day;
strcpy(cpu.use_num,cp.use_num);
fp=fopen("11111","ab");
// fseek(fp,-(i-1)*sizeof(COM),2);
fwrite(&cpu,sizeof(COM),1,fp); //将下机信息记入文件
fclose(fp);
printf("\t成功下机\n");
}
}
void search_num() //定义按序号查找函数
{
COM cp;
int a;
int i=1;
int j,k;
FILE *fp;
fp=fopen("11111","rb");
printf("\t请输入序号(1001-1100):");
scanf("%d",&a);
if(a<1001||a>1100)
{
printf("输入错误,请重新输入\n");
search_num();
return;
}
printf("\n\t************************************************************\n");
printf(" 电脑编号 使用者姓名 学号 星期 时间 上机/下机\n");
printf("\n\t------------------------------------------------------------\n");
do
{
// fseek(fp,-i*sizeof(COM),2);
fread(&cp,sizeof(COM),1,fp);
if(a==cp.num&&cp.day!=0)
{
j=(cp.num-1001)/5+1;
k=(cp.num-1001)%5+1;
if(cp.sign==1)
printf(" %-10d %d排%d号 %-10s %-10s %-10d %d:%d 上机\n",cp.num,j,k,cp.use_name,cp.use_num,cp.day,cp.time_shi,cp.time_fen);
if(cp.sign==0)
printf(" %-10d %d排%d号 %-10s %-10s %-10d %d:%d 下机\n",cp.num,j,k,cp.use_name,cp.use_num,cp.day,cp.time_shi,cp.time_fen);
}
// i++;
}
while(feof(fp)==0);
printf("\n\t------------------------------------------------------------\n");
fclose(fp);
}
void search_day() //定义按星期查找函数
{
COM cp;
int a;
int i=1;
int j,k;
FILE *fp;
fp=fopen("11111","rb");
printf("\t请输入时间(1-7):");
bbb:scanf("%d",&a);
if(a<1||a>7)
{
printf("\t输入错误,请重新输入\n\t");
goto bbb;
}
printf("\n\t************************************************************\n");
printf(" 电脑编号 电脑位置 使用者姓名 学号 星期 时间 上机/下机\n");
printf("\n\t------------------------------------------------------------\n");
do
{
// fseek(fp,-i*sizeof(COM),0);
if((fread(&cp,sizeof(COM),1,fp))!=1)break;
if(a==cp.day)
{
j=(cp.num-1001)/5+1;
k=(cp.num-1001)%5+1;
if(cp.sign==1)
printf(" %-10d %d排%d号 %-10s %-10s %-5d %d:%d 上机\n",cp.num,j,k,cp.use_name,cp.use_num,cp.day,cp.time_shi,cp.time_fen);
if(cp.sign==0)
printf(" %-10d %d排%d号 %-10s %-10s %-5d %d:%d 下机\n",cp.num,j,k,cp.use_name,cp.use_num,cp.day,cp.time_shi,cp.time_fen);
}
// i++;
}
while(feof(fp)==0);
printf("\n\t------------------------------------------------------------\n");
fclose(fp);
}
void print_1() //主菜单
{
printf("\n\t************************************************************\n");
printf("\n\t------------------------------------------------------------\n");
printf("\n\t 1.登记\n");
printf("\n\t 2.查询\n");
printf("\n\t 0.退出\n");
printf("\n\t------------------------------------------------------------\n");
printf("\n\t************************************************************\n");
}
void print_2() //登记菜单
{
printf("\n\t************************************************************\n");
printf("\n\t------------------------------------------------------------\n");
printf("\n\t 1.上机\n");
printf("\n\t 2.下机\n");
printf("\n\t 0.退出\n");
printf("\n\t---------------------------------------
评论0