C语言程序问题!!!!!求助
#include <stdio.h>#define SIZE 12
void print ();
void show_nullnum (struct plane *a);
void show_listnum (struct plane *b);
void show_alphabeticallist (struct plane *c);
void assign_num (struct plane *d);
void delete_num (struct plane *e);
struct plane {
int num;
int sf;
char name;
};
int main (void)
{
char q;
struct plane planes =
{
{0, 1, " "},
{1, 1, " "},
{2, 1, " "},
{3, 1, " "},
{4, 1, " "},
{5, 1, " "},
{6, 1, " "},
{7, 1, " "},
{8, 1, " "},
{9, 1, " "},
{10, 1, " "},
{11, 1, " "},
};
print ();
scanf ("%c", &q);
while (q != 'f')
{
while ( q < 'a' && q > 'f')
{
printf ("输入错误\n");
scanf ("%c", &q);
}
switch (q)
{
case 'a':
show_nullnum (planes);
break;
case 'b':
show_listnum (planes);
break;
case 'c':
show_alphabeticallist (planes);
break;
case 'd':
assign_num (planes);
break;
case 'e':
delete_num (planes);
break;
case 'f':
break;
}
if (q != 'f')
{
print ();
scanf ("%c", &q);
}
}
return 0;
}
void print ()
{
printf ("选择一个函数,输入字母标签\n");
printf ("a) 显示的空座位号码\n");
printf ("b) 显示的空座位表\n");
printf ("c) 显示的席位按字母顺序排列的列表\n");
printf ("d) 分配一个客户一个座位\n");
printf ("e) 删除一个座位\n");
printf ("f) 退出\n");
}
void show_nullnum (struct plane *a)
{
int i, sum;
for (i = 0, sum = 0; i < SIZE; i++)
{
if (a.sf)
sum++;
}
printf ("一共还有%d个空位\n", sum);
}
void show_listnum (struct plane *b)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (b.sf)
printf ("座号:%d", b.num);
}
}
void show_alphabeticallist (struct plane *c)
{
int i;
for (i = 0; i < SIZE; i++)
{
printf ("座号:%d ", c.num);
if (c.sf)
printf ("空座\n");
else
printf ("%s %s\n", c.name);
}
}
void assign_num (struct plane *d)
{
int i;
printf ("请输入需要的座位:");
scanf ("%d", &i);
if (d.sf)
{
printf ("请输入姓名:");
scanf ("%s", &d.name);
d.sf = 0;
}
else
printf ("此座位已被预定\n");
}
void delete_num (struct plane *e)
{
int i;
printf ("请输入需要退订的座位:");
scanf ("%d", &i);
if (e.sf)
printf ("此座位无预定");
else
e.sf = 1;
}
主函数中
if (q != 'f')
{
print ();
scanf ("%c", &q);
}
!!!!!! print();怎么执行了2次
#include <stdio.h>
#define SIZE 12
void print ();
void show_nullnum (struct plane *a);
void show_listnum (struct plane *b);
void show_alphabeticallist (struct plane *c);
void assign_num (struct plane *d);
void delete_num (struct plane *e);
struct plane {
int num;
int sf;
char name;
};
int main (void)
{
char q;
struct plane planes =
{
{0, 1, " "},
{1, 1, " "},
{2, 1, " "},
{3, 1, " "},
{4, 1, " "},
{5, 1, " "},
{6, 1, " "},
{7, 1, " "},
{8, 1, " "},
{9, 1, " "},
{10, 1, " "},
{11, 1, " "},
};
print ();
scanf ("%c", &q);
while (q != 'f')
{
while ( q < 'a' || q > 'f') //!!!!!!!!!!这个地方要是|| 运算符
{
printf ("输入错误\n");
print ();
fflush(stdin); /*!!!!!!!!!!清空缓冲区,也可以使用rewind(stdin);*/
scanf ("%c", &q);
}
switch (q)
{
case 'a':
show_nullnum (planes);
break;
case 'b':
show_listnum (planes);
break;
case 'c':
show_alphabeticallist (planes);
break;
case 'd':
assign_num (planes);
break;
case 'e':
delete_num (planes);
break;
case 'f':
break;
}
if (q != 'f')
{
print ();
fflush(stdin); /*!!!!!!!!!!!清空缓冲区,也可以使用rewind(stdin);*/
scanf ("%c", &q);
}
}
return 0;
}
void print ()
{
printf ("选择一个函数,输入字母标签\n");
printf ("a) 显示的空座位号码\n");
printf ("b) 显示的空座位表\n");
printf ("c) 显示的席位按字母顺序排列的列表\n");
printf ("d) 分配一个客户一个座位\n");
printf ("e) 删除一个座位\n");
printf ("f) 退出\n");
}
void show_nullnum (struct plane *a)
{
int i, sum;
for (i = 0, sum = 0; i < SIZE; i++)
{
if (a.sf)
sum++;
}
printf ("一共还有%d个空位\n", sum);
}
void show_listnum (struct plane *b)
{
int i;
for (i = 0; i < SIZE; i++)
{
if (b.sf)
printf ("座号:%d", b.num);
}
}
void show_alphabeticallist (struct plane *c)
{
int i;
for (i = 0; i < SIZE; i++)
{
printf ("座号:%d ", c.num);
if (c.sf)
printf ("空座\n");
else
printf ("%s %s\n", c.name);
}
}
void assign_num (struct plane *d)
{
int i;
printf ("请输入需要的座位:");
scanf ("%d", &i);
if (d.sf)
{
printf ("请输入姓名:");
scanf ("%s", &d.name);
d.sf = 0;
}
else
printf ("此座位已被预定\n");
}
void delete_num (struct plane *e)
{
int i;
printf ("请输入需要退订的座位:");
scanf ("%d", &i);
if (e.sf)
printf ("此座位无预定");
else
e.sf = 1;
}
//程序改了,现在可以了
//解释:scanf()函数是从输入流缓冲区中读取值的,而并非从键盘(也就是终端)缓冲区读取。而读取时遇到回车(n)而结束的,这个n会一起读入输入流缓冲区的,所以第一次接受输入时取走字符后会留下字符n,这样第二次的读入函数直接从缓冲区中把n取走了,显然读取成功了,所以不会再从终端读取,读取的是10而你的那个判断不是正确指令的while语句写错了,所以又执行了一次print()函数!
所以要清空缓冲区的残留数据。
使用 fflush(stdin); 或 rewind(stdin); 均可起到清空键盘缓冲区的作用,这两个函数均包含在stdio.h这个头文件中(程序中已注释了!)
//呵呵,有问题,再交流啊!
你输入a,b,c,d敲回车的时候,回车符值('\n')赋给了q,等于你输入一次,运行了两次while(q!=f),你单步运行一下就看得出来 本帖最后由 畩と嘫 于 2014-3-30 23:24 编辑
把if (q != 'f')
{
print ();
scanf ("%c", &q);
}这个中的print ();放在if循环外面就好了!!!理由就是楼上所说的,放在里面就执行了两次!
页:
[1]