我正在编写一个程序,将学生ID存储到5个不同实验室的20台PC中。我几乎得到它,就在程序检测到已经使用过的ID时,它应该打印一个语句并重新启动程序,要求提供学生ID。但是,当我的程序检测到已使用的ID时,它会继续询问实验室然后打印语句,然后继续要求PC预订。
结帐(循环)
for(r=0;r<20;r++)
{
if(sId!=l1->pc[r] || sId!=l2->pc[r] || sId!=l3->pc[r] || sId!=l4->pc[r] || sId!=l5->pc[r])
{
printf("The Student ID '%i' has not made any booking\n", sId);
return;
}
}
结构代码:
typedef struct
{
int lId,pc[20],vunit;
}Openlab;
主要
int main()
{
Openlab lab1;
Openlab lab2;
Openlab lab3;
Openlab lab4;
Openlab lab5;
int option;
lab1.lId = 101;
lab1.vunit = 20;
lab1.pc[20];
lab2.lId = 201;
lab2.vunit = 20;
lab2.pc[20];
lab3.lId = 301;
lab3.vunit = 20;
lab3.pc[20];
lab4.lId = 401;
lab4.vunit = 20;
lab4.pc[20];
lab5.lId = 501;
lab5.vunit = 20;
lab5.pc[20];
printf("***Welcome to OpenLab Booking Service***");
while(option>3)
{
printf("\n\nPlease chose an option:");
printf("\n[1]Check In");
printf("\n[2]Check Out");
printf("\n[0]Quit");
printf("\n\nOption: ");
scanf("%i", &option);
if(option == 1)
{
check_in(&lab1,&lab2,&lab3,&lab4,&lab5);
option = 4;
printf("\nUnits Vacant Left in Lab 101: %i\n", lab1.vunit);
printf("Units Vacant Left in Lab 201: %i\n", lab2.vunit);
printf("Units Vacant Left in Lab 301: %i\n", lab3.vunit);
printf("Units Vacant Left in Lab 401: %i\n", lab4.vunit);
printf("Units Vacant Left in Lab 501: %i\n", lab5.vunit);
}
else if(option == 2)
{
check_out(&lab1,&lab2,&lab3,&lab4,&lab5);
option = 4;
printf("\nUnits Vacant Left in Lab 101: %i\n", lab1.vunit);
printf("Units Vacant Left in Lab 201: %i\n", lab2.vunit);
printf("Units Vacant Left in Lab 301: %i\n", lab3.vunit);
printf("Units Vacant Left in Lab 401: %i\n", lab4.vunit);
printf("Units Vacant Left in Lab 501: %i\n", lab5.vunit);
}
else if(option == 0)
{
printf("\n\nThanks for using the OpenLab Booking Service! See you again!\n\n");
}
}
return 0;
}
功能:
void check_in(Openlab *l1, Openlab *l2, Openlab *l3, Openlab *l4, Openlab *l5)
{
int sId,r,lab,comp;
printf("Please enter your Student ID: ");
scanf("%i", &sId);
for(r=0;r<20;r++)
{
if(sId!=l1->pc[r] && sId!=l2->pc[r] && sId!=l3->pc[r] && sId!=l4->pc[r] && sId!=l5->pc[r])
{
while(lab!=l1->lId && lab!=l2->lId && lab!=l3->lId && lab!=l4->lId && lab!=l5->lId)
{
printf("Choose a Open Lab [101,201,301,401,501]: ");
scanf("%i", &lab);
}
}
else
{
printf("The Student ID '%i' has already been used to book a PC!\n", sId);
break;
}
}
if(l1->vunit!=0 && l2->vunit!=0 && l3->vunit!=0 && l4->vunit!=0 && l5->vunit!=0)
{
while(comp>19)
{
printf("Choose a PC [0-19]: ");
scanf("%i", &comp);
}
}
else
{
printf("\nNo vacant PC left Open Lab %i!\n\n", lab);
}
if(lab==l1->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
if(l1->vunit!=0)
{
l1->vunit--;
l1->pc[comp] = sId;
}
}
else if(lab==l2->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l2->vunit--;
l2->pc[comp] = sId;
}
else if(lab==l3->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l3->vunit--;
l3->pc[comp] = sId;
}
else if(lab==l4->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l4->vunit--;
l4->pc[comp] = sId;
}
else if(lab==l5->lId)
{
//printf("Booking for %i - Lab %i | PC %i ",sId,lab,comp);
l5->vunit--;
l5->pc[comp] = sId;
}
}
退房:
void check_out(Openlab *l1, Openlab *l2, Openlab *l3, Openlab *l4, Openlab *l5)
{
int sId,r;
printf("Please Enter your Student ID: ");
scanf("%i", &sId);
for(r=0;r<20;r++)
{
if(sId==l1->pc[r])
{
l1->pc[r] = 0;
l1->vunit++;
printf("Removed");
//return;
}
else if(sId==l2->pc[r])
{
l2->pc[r] = 0;
l2->vunit++;
printf("Removed");
//return;
}
else if(sId==l3->pc[r])
{
l3->pc[r] = 0;
l3->vunit++;
printf("Removed");
//return;
}
else if(sId==l4->pc[r])
{
l4->pc[r] = 0;
l4->vunit++;
printf("Removed");
//return;
}
else if(sId==l5->pc[r])
{
l5->pc[r] = 0;
l5->vunit++;
printf("Removed");
//return;
}
}
}
答案 0 :(得分:1)
你的逻辑全都倒退了。您目前获得它的方式是,它循环遍历所有20个插槽并检查所有5个实验室,以查看是否已将学生分配给其中一个。如果它不是,那么你问他们在哪个实验室并继续检查。在询问实验室之前,您应该等到循环结束。
而不是您所拥有的,您应该让它看看是否已分配学生,如果是,请打印出错误消息并从该功能return
打印出来,因为那里有没有必要继续。
for(r=0;r<20;r++)
{
if(sId==l1->pc[r] || sId==l2->pc[r] || sId==l3->pc[r] || sId==l4->pc[r] || sId==l5->pc[r])
{
printf("The Student ID '%i' has already been used to book a PC!\n", sId);
return;
}
}
如果循环完成,那么您可以询问分配它们的实验室。值得注意的是,您应该在使用之前为lab
分配一个值,因为它可以包含任何值,包括有效的实验室ID,这会导致它跳过while
循环。或者替代方案,您可以将其设为do...while()
循环,因为您始终要至少提出一次问题。
do
{
printf("Choose a Open Lab [101,201,301,401,501]:\n");
scanf("%i", &lab);
}
while(lab!=l1->lId && lab!=l2->lId && lab!=l3->lId && lab!=l4->lId && lab!=l5->lId);