鱼C论坛

 找回密码
 立即注册
查看: 1551|回复: 3

C语言程序问题!!!!!求助

[复制链接]
发表于 2014-3-30 21:37:57 | 显示全部楼层 |阅读模式
4鱼币
#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[20];
};

int main (void)
{
        char q;

        struct plane planes[SIZE] =
        {
                {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[i].sf)
                        sum++;
        }
        printf ("一共还有%d个空位\n", sum);
}

void show_listnum (struct plane *b)
{
        int i;
       
        for (i = 0; i < SIZE; i++)
        {
                if (b[i].sf)
                        printf ("座号:%d", b[i].num);
        }
}

void show_alphabeticallist (struct plane *c)
{
        int i;

        for (i = 0; i < SIZE; i++)
        {
                printf ("座号:%d   ", c[i].num);
                if (c[i].sf)
                        printf ("空座\n");
                else
                        printf ("%s %s\n", c[i].name);
        }
}
void assign_num (struct plane *d)
{
        int i;
        printf ("请输入需要的座位:");
        scanf ("%d", &i);
        if (d[i].sf)
        {
                printf ("请输入姓名:");
                scanf ("%s", &d[i].name);
                d[i].sf = 0;
        }
        else
                printf ("此座位已被预定\n");

}
void delete_num (struct plane *e)
{
        int i;
        printf ("请输入需要退订的座位:");
        scanf ("%d", &i);
        if (e[i].sf)
                printf ("此座位无预定");
        else
                e[i].sf = 1;
}

主函数中
if (q != 'f')
        {
                print ();
                scanf ("%c", &q);
        }

!!!!!!      print();怎么执行了2次

最佳答案

查看完整内容

//程序改了,现在可以了 //解释:scanf()函数是从输入流缓冲区中读取值的,而并非从键盘(也就是终端)缓冲区读取。而读取时遇到回车(n)而结束的,这个n会一起读入输入流缓冲区的,所以第一次接受输入时取走字符后会留下字符n,这样第二次的读入函数直接从缓冲区中把n取走了,显然读取成功了,所以不会再从终端读取,读取的是10而你的那个判断不是正确指令的while语句写错了,所以又执行了一次print()函数! 所以要清空缓冲区的 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-3-30 21:37:58 | 显示全部楼层

  1. #include <stdio.h>
  2. #define SIZE 12

  3. void print ();
  4. void show_nullnum (struct plane *a);
  5. void show_listnum (struct plane *b);
  6. void show_alphabeticallist (struct plane *c);
  7. void assign_num (struct plane *d);
  8. void delete_num (struct plane *e);

  9. struct plane {
  10.         int num;
  11.         int sf;
  12.         char name[20];
  13. };

  14. int main (void)
  15. {
  16.         char q;

  17.         struct plane planes[SIZE] =
  18.         {
  19.                 {0, 1, " "},
  20.                 {1, 1, " "},
  21.                 {2, 1, " "},
  22.                 {3, 1, " "},
  23.                 {4, 1, " "},
  24.                 {5, 1, " "},
  25.                 {6, 1, " "},
  26.                 {7, 1, " "},
  27.                 {8, 1, " "},
  28.                 {9, 1, " "},
  29.                 {10, 1, " "},
  30.                 {11, 1, " "},
  31.         };

  32.         print ();

  33.         scanf ("%c", &q);

  34.         while (q != 'f')
  35.         {
  36.         while ( q < 'a' || q > 'f')    //!!!!!!!!!!这个地方要是|| 运算符
  37.         {
  38.                 printf ("输入错误\n");
  39.                 print ();
  40.                 fflush(stdin);   /*!!!!!!!!!!清空缓冲区,也可以使用rewind(stdin);*/
  41.                 scanf ("%c", &q);
  42.         }
  43.         switch (q)
  44.         {
  45.         case 'a':
  46.                 show_nullnum (planes);
  47.                 break;
  48.         case 'b':
  49.         show_listnum (planes);
  50.             break;
  51.         case 'c':
  52.                 show_alphabeticallist (planes);
  53.                 break;
  54.         case 'd':
  55.                 assign_num (planes);
  56.                 break;
  57.         case 'e':
  58.                 delete_num (planes);
  59.                 break;
  60.         case 'f':
  61.                 break;
  62.         }
  63.         if (q != 'f')
  64.         {
  65.                 print ();
  66.                 fflush(stdin);   /*!!!!!!!!!!!清空缓冲区,也可以使用rewind(stdin);*/
  67.                 scanf ("%c", &q);
  68.         }
  69.         }

  70.     return 0;
  71. }

  72. void print ()
  73. {
  74.     printf ("选择一个函数,输入字母标签\n");
  75.         printf ("a) 显示的空座位号码\n");
  76.         printf ("b) 显示的空座位表\n");
  77.         printf ("c) 显示的席位按字母顺序排列的列表\n");
  78.         printf ("d) 分配一个客户一个座位\n");
  79.         printf ("e) 删除一个座位\n");
  80.         printf ("f) 退出\n");
  81. }

  82. void show_nullnum (struct plane *a)
  83. {
  84.         int i, sum;
  85.         for (i = 0, sum = 0; i < SIZE; i++)
  86.         {
  87.                 if (a[i].sf)
  88.                         sum++;
  89.         }
  90.         printf ("一共还有%d个空位\n", sum);
  91. }

  92. void show_listnum (struct plane *b)
  93. {
  94.         int i;
  95.       
  96.         for (i = 0; i < SIZE; i++)
  97.         {
  98.                 if (b[i].sf)
  99.                         printf ("座号:%d", b[i].num);
  100.         }
  101. }

  102. void show_alphabeticallist (struct plane *c)
  103. {
  104.         int i;

  105.         for (i = 0; i < SIZE; i++)
  106.         {
  107.                 printf ("座号:%d   ", c[i].num);
  108.                 if (c[i].sf)
  109.                         printf ("空座\n");
  110.                 else
  111.                         printf ("%s %s\n", c[i].name);
  112.         }
  113. }
  114. void assign_num (struct plane *d)
  115. {
  116.         int i;
  117.         printf ("请输入需要的座位:");
  118.         scanf ("%d", &i);
  119.         if (d[i].sf)
  120.         {
  121.                 printf ("请输入姓名:");
  122.                 scanf ("%s", &d[i].name);
  123.                 d[i].sf = 0;
  124.         }
  125.         else
  126.                 printf ("此座位已被预定\n");

  127. }
  128. void delete_num (struct plane *e)
  129. {
  130.         int i;
  131.         printf ("请输入需要退订的座位:");
  132.         scanf ("%d", &i);
  133.         if (e[i].sf)
  134.                 printf ("此座位无预定");
  135.         else
  136.                 e[i].sf = 1;
  137. }
复制代码
//程序改了,现在可以了
//解释:scanf()函数是从输入流缓冲区中读取值的,而并非从键盘(也就是终端)缓冲区读取。而读取时遇到回车(n)而结束的,这个n会一起读入输入流缓冲区的,所以第一次接受输入时取走字符后会留下字符n,这样第二次的读入函数直接从缓冲区中把n取走了,显然读取成功了,所以不会再从终端读取,读取的是10而你的那个判断不是正确指令的while语句写错了,所以又执行了一次print()函数!
    所以要清空缓冲区的残留数据。
使用 fflush(stdin); 或 rewind(stdin); 均可起到清空键盘缓冲区的作用,这两个函数均包含在stdio.h这个头文件中(程序中已注释了!)
//呵呵,有问题,再交流啊!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-3-30 23:17:38 | 显示全部楼层
你输入a,b,c,d敲回车的时候,回车符值('\n')赋给了q,等于你输入一次,运行了两次while(q!=f),你单步运行一下就看得出来

评分

参与人数 1鱼币 +2 收起 理由
a121517644 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-3-30 23:23:13 | 显示全部楼层
本帖最后由 畩と嘫 于 2014-3-30 23:24 编辑

把if (q != 'f')
        {
                print ();
                scanf ("%c", &q);
        }这个中的print ();放在if循环外面就好了!!!理由就是楼上所说的,放在里面就执行了两次!

评分

参与人数 1鱼币 +2 收起 理由
a121517644 + 2

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-4 00:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表