关于c语言程序
#include <stdio.h>
char c;
void main()
{
printf("Please type 1 for smoking,and type 2 for nonsmoking:");
{
do choose();
while(a[0]+a[1]+a[2]+a[3]+a[4]+b[0]+b[1]+b[2]+b[3]+b[4]!=55);
}
printf("Sorry!All the tickets are sold out.Next flight leaves in 3 hours");
}
void choose()
{
scanf("%c",&c);
switch(c)
{
case '1':{ smoking();}break;
case '2':{ nonsmoking();}break;
default: printf("error!");
}
}
void smoking()
{
static int a[5]={0};
int i,j;
char w,w2;
for(i=0;i<5;i++)
{
printf("please input the number of a seat that you want(1-5):");
scanf("%d",&a[i]);
if({for (j=0;j<i;j++)
a[i]!=a[j];})
{
printf("\
the seat you choose is number%d seat",a[i]);
if(a[0]+a[1]+a[2]+a[3]+a[4]!=15)
{
printf("would you like to countinue to book more tickets?(y/n)");
scanf("%c,&w);
if(w=='n') break;
else if(w!='y')
{printf("error");break;}
}
else {printf("sorry!the tickets for nonsmoking seats are sold out!\
);
printf("would you like to book a smoking seat?(y/n);
scanf(%c,&w2);
if(w=='n')
break;
else if(w!='y')
{printf("error!"); break;}
else
smoking(); }
}
else printf("sorry!the seat is already booked!");smoking();
}
}
void nonsmoking()
{
static int b[5]={0};
int i,j;
char w,w2
for(i=5;i<10;i++)
{
printf("please input the number of a seat that you want(6-10):");
scanf("%d",&b[i]);
if({for(j=5;j<i;j++)
b[i]!=b[j];})
{
printf("\
the seat you choose is %d",b[i]);
if(b[0]+b[1]+b[2]+b[3]+b[4]!=40)
{
printf("would you like to countinue to book more tickets?(y/n)");
scanf("%c,&w);
if(w=='n') break;
else if(w!=y)
{printf("error!");break;}
}
else
{printf("sorry!the tickets for nonsmoking seats are sold out!\
);
printf("would you like to book a smoking seat?(y/n);
scanf(%c,&w2);
if(w=='n')
break;
else if(w!='y')
{printf("error!"); break;}
else
smoking();}
}
else printf("sorry!the seat is already booked!");nonsmoking();
}
}
那位强人帮忙看一下以上程序出了那些错误,应该怎样改正,谢谢!
答案:#include <stdio.h>
char c;
void main()
{
printf("Please type 1 for smoking,and type 2 for nonsmoking:");
{ void choose();int a[5],b[5];
do choose();
while(a[0]+a[1]+a[2]+a[3]+a[4]+b[0]+b[1]+b[2]+b[3]+b[4]!=55);
}
printf("Sorry!All the tickets are sold out.Next flight leaves in 3 hours");
}
void choose()
{ scanf("%c",&c);
switch(c)
{ void smoking();void nonsmoking();
case '1':{ smoking();}break;
case '2':{ nonsmoking();}break;
default: printf("error!");
}
}
void smoking()
{ static int a[5]={0};
int i,j;
char w,w2;
for(i=0;i<5;i++)
{
printf("please input the number of a seat that you want(1-5):");
scanf("%d",&a[i]); for(j=5;j<i;j++)
if (a[i]!=a[j]) continue; if(j==i)
{
printf("\
the seat you choose is number%d seat",a[i]);
if(a[0]+a[1]+a[2]+a[3]+a[4]!=15)
{
printf("would you like to countinue to book more tickets?(y/n)");
scanf("%c",&w);
if(w=='n') break;
else if(w!='y')
{printf("error");break;}
}
else
{printf("sorry!the tickets for nonsmoking seats are sold out!\
");
printf("would you like to book a smoking seat?(y/n)");
scanf("%c",&w2);
if(w=='n') break;
else if(w!='y') { printf("error!"); break;}
else smoking(); }
}
else printf("sorry!the seat is already booked!");smoking();
}
}
void nonsmoking()
{ static int b[5]={0};
int i,j;
char w,w2;
for(i=5;i<10;i++)
{
printf("please input the number of a seat that you want(6-10):");
scanf("%d",&b[i]);
for(j=5;j<i;j++)
if(b[i]!=b[j]) continue;if(j==i)
{
printf("\
the seat you choose is %d",b[i]);
if(b[0]+b[1]+b[2]+b[3]+b[4]!=40)
{
printf("would you like to countinue to book more tickets?(y/n)");
scanf("%c",&w);
if (w=='n') break;
else if(w!='y')
{printf("error!");break;}
}
else
{printf("sorry!the tickets for nonsmoking seats are sold out!\
");
printf("would you like to book a smoking seat?(y/n)");
scanf("%c",&w2);
if(w=='n') break;
else if(w!='y') {printf("error!"); break;}
else smoking();}
}
else printf("sorry!the seat is already booked!");nonsmoking();
}
}
天啊
你这个程序也太难改了 ,我花了3个小时,2点才大致改出来 ,由于我还是有点不知到你这个程序的意图 ,所以只能给你改正某些大错误和语法错误;要指出的是 ,这个程序有很多语法问题,比如scanf函数语句少了分号,某些地方少了括号,最大的错误是if语句中,这种用法是绝对错误的if({for(j=5;j<i;j++)
b[i]!=b[j];}) if后的括号里只能有变量和表达式,不能有语句(带分号就代表是一个语句),但有不能改变你原有的意思,所以我就那样改了一下,for(j=5;j<i;j++)
if(b[i]!=b[j]) continue;if(j==i)我问国别人,这样改跟你原意一点没变。
最后,我想说的是,在记住书本知识后,c语言是一个不断上机和思考的学科,你上机思考的愈多,进步就越大(编译后按f4可看错误地点和提示,这你知道吧)
祝你C语言大有所成!!!
上一个:C语言 游戏
下一个:怎样学好C 语言