2 mgxy634 mgxy634 于 2014.06.06 21:32 提问

机房管理系统 不懂源代码中的一个问题

// 当选择等待时,输入的数字和电话号码为什么没有,只是把一次的复制了一遍?

#include

#include

#include

#define N 20

#define T (time-8)/2

int sum[6]={0};

int computer[6][20]={{1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1}, //0表示是空机位,从0开始表示时间段

{1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1},

{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},

{1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,0,0,1,1,1},

{1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0},

{1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1}}; //该数组表示假设某天某人查询时机房各个时间段机位数情况

void Menu();

void Situation();

void Book();

void Cancel();

void SearchWaiting();

void Exit();

void detail(int time);

void printf_one(int i);

struct waitlist
{

char number[3];
char telephonenumber[12];
}
wait[N];

/*主函数*/
void main()
{
Menu(); //当前状态函数

}

void Menu() //主界面

{

int n,w;
do
{
puts("\t\t****************COMPUTER BOOK SYSTERM*******************\n");
puts("\t\t*************************MENU***************************\n");
puts("\t\t\t 1.Search the current situation"); //查询某时间段机位位状态
puts("\t\t\t 2.Book the vacant computer"); //预定空机
puts("\t\t\t 3.Cancel booking"); //取消预订
puts("\t\t\t 4.Search waiting"); //查询等待信息
puts("\t\t\t 5.Exit"); //退出
puts("\t\t********************************************************\n");
printf("Choose your number(1-5): [ ]\b\b"); // \b表示退一格
scanf("%d",&n);
if(n5)

{
w=1;
getchar();
}
else
w=0;

}
while(w==1);
switch(n)
{
case 1:Situation();break;
case 2:Book();break;
case 3:Cancel();break;

case 4:SearchWaiting();break;
case 5:exit(0);break;
}
}

/*查询模块*/

/*查询当前状态*/
void Situation()

{

int time;

printf("Input time (8<=time scanf("%d",&time);
if(time=20)

{
printf("\tWrong time!\n");

printf("Please input time (8<=time<20):");
scanf("%d",&time);
}

detail(time);
getchar(); 
Menu(); 

}

/*预定模块*/
void Book()

{
int time,i=0,x,y;
FILE *fp;
char c;

printf("Input time you want to book (8<=time scanf("%d",&time);
if(time=20)
{

printf("\tWrong time!\n");

printf("\tInput time you want to book (8<=time scanf("%d",&time);
}
detail(time); //函数调用
if(sum[T]>0)

{
if((fp=fopen("waitlist.txt","a"))==NULL)
{
printf("\nCannot open file!\n");
exit(0);
}
printf("\n\tPlease input the number you want to book and leave your telephonenumber!:\n");
scanf("%s %s",wait[i].number,wait[i].telephonenumber);
for(i=0;i fwrite(&wait[i],sizeof(struct waitlist),1,fp); //从fp所指的文件读入1个 struct waitlist的数据存入数组 wait中
fclose(fp); //向文本文档添加一个结构体单元
printf("\tInput the number you want to book again!\n");
scanf("%d",&x);
sum[T]--; //预定后该时间段空机位数减1
computer[T][x]=1; //预订后该时间段机位变为1
getchar();
Menu();
}
else
{
printf("There is no vacant computer at that time!\n");
for(i=T+1;i {
time=9+2*i;
detail(time); //函数调用
if(sum[T]>0)
printf("\tThe latest free time is %d,%d\n",2*i+8,2*i+10);break;
}

printf("\tDo you want to wait?:y/n?");
scanf("%s",&c);
getchar();
if(c=='Y'||c=='y') // 若等待,就用文件的方式列出当时的等待列表并提示输入你的等待序号和联系方式
{
if((fp=fopen("waitlist.txt","r"))==NULL)

{

printf("\nCannot open file!\n");
exit(0);
}
else
{
printf("waitlist:number telephonenumber\n");
for(i=0;!feof(fp);i++) //!feof(fp)文件是否读完
{
fscanf(fp,"%s %s",&wait[i].number,&wait[i].telephonenumber);
}

fclose(fp); //关闭文件
}
for(i=0;i<10;i++) //至多预订10次
{
printf_one(i); //调用函数
}
printf("\n\tPlease Input the number you want to book and leave your telephonenumber!:\n");
if((fp=fopen("waitlist.txt","a"))==NULL) //追加
{
printf("\nCannot open file!\n");
exit(0);
}
scanf("%s %s",wait[i].number,wait[i].telephonenumber);
for(i=0;i<1;i++)

fwrite(&wait[i],sizeof(struct waitlist),1,fp);

fclose(fp);

printf("Input the number of the computer again!\n");
scanf("%d",&y);
sum[T]--;
computer[T][y]=1;
printf("\tOk,please wait our phone!\n");
Menu();
}

else Menu();
}
}

/*取消预订模块*/
void Cancel()
{
int time,number;
printf("Input time you have booked (8<=time scanf("%d",&time);
if(time=20)
{
printf("\tWrong time!\n");
printf("\tInput time you have booked (8<=time<20):");
scanf("%d",&time);
}
else
{
printf("\tInput number you have booked :");
scanf("%d",&number);
}
detail(time);
sum[T]++; //预定后该时间段空机位数加1
computer[T][number]=0; //预定后该机状态变为0
printf("\tCancel succeed!\n");
getchar();
Menu();
}

/*查询等待信息模块*/
void SearchWaiting()
{
FILE *fp;
int time,i;
printf("\tInput time you want to search (8<=time scanf("%d",&time);
if(time=20)
{
printf("\tWrong time!\n");
printf("\tInput time you want to search (8<=time<20):");
scanf("%d",&time);
}
else ;
if((fp=fopen("waitlist.txt","r"))==NULL) //只读方式打开文件
{
printf("\nCannot open file!\n");
exit(0);
}
else
{
printf("waitlis:number telephonenumber\n");
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s %s",&wait[i].number,&wait[i].telephonenumber);
}
fclose(fp);
}
for(i=0;i<10;i++)
{
printf_one(i);
}
Menu();
}

void detail(int time) //此函数功能是实现查找每个时间段空机位总数和空机位号
{
int i,j=0,k,s=0;
int v[30];
for(i=0;i<20;i++)
{
if(computer[T][i]==0)
{
s++;
v[j++]=i; //
}
}
sum[T]=s;
printf("\tCurrent vacant computers:%d\n",sum[T]);
for(k=0;k<j;k++)
printf("\tThe vacant computers are:%d\n",v[k]);
}

void printf_one(int i) //此函数功能是实现输出等待列表到屏幕
{
printf("\t\t%s %s\n",wait[i].number,wait[i].telephonenumber);
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!