| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1066 人关注过本帖
标题:求高人:如果想程序当输入“ctrl+z”时才结束,怎样改编呢?(请见第9楼程序 ...
只看楼主 加入收藏
huhao3225
Rank: 2
来 自:四川成都
等 级:论坛游民
帖 子:39
专家分:25
注 册:2011-11-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:11 
求高人:如果想程序当输入“ctrl+z”时才结束,怎样改编呢?(请见第9楼程序)有木有人会啊?
请见第9楼程序
/*输入学生成绩,统计总人数和各个等级的人数*/
#include<stdio.h>
main()
{
    int score,a,total=0,errtotal=0;
    int acount=0,bcount=0,ccount=0,dcount=0,ecount=0;
    printf("Please enter scores:\n");
    while (score!=EOF)
    {
    scanf("%d",&score);
    a=score%10;
     if (score>=0)
     {
           switch(a)
          {
            case 10:
             case 9:
                {
                    ++acount;
                    ++total;
                    break;
                }
            case 8:
                {
                    ++bcount;
                    ++total;
                    break;
                }
             case 7:
                {
                    ++ccount;
                    ++total;
                    break;
                }
            case 6:
                {
                    ++dcount;
                    ++total;
                    break;
                }
            default:
                {
                    ++ecount;
                    ++total;
                    break;
                }
          }
     }
        else
            {
                printf("Sorry.You have input a wrong score.\n");
                ++errtotal;
            }
    }
    printf("\n有效输入次数为:%d,无效输入次数为:%d\n",total,errtotal);
    printf("总人数为:%d,成绩分布如下:\n");
    printf("A级\tB级\tC级\tD级\tE级\n");
    printf("%d人\t%d人\t%d人\t%d人\t%d人\n",acount,bcount,ccount,dcount,ecount);
}

[ 本帖最后由 huhao3225 于 2011-11-13 09:42 编辑 ]
搜索更多相关主题的帖子: 统计 include scores 
2011-11-12 00:28
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
你这最基本的是缺少循环
2011-11-12 00:41
huhao3225
Rank: 2
来 自:四川成都
等 级:论坛游民
帖 子:39
专家分:25
注 册:2011-11-9
收藏
得分:0 
书上有个类似的也没有循环啊…
2011-11-12 00:50
xp0213
Rank: 7Rank: 7Rank: 7
来 自:湖北武汉
等 级:黑侠
威 望:1
帖 子:222
专家分:524
注 册:2011-10-26
收藏
得分:10 
#include<stdio.h>
main()
{
    int score,a,total=0,errtotal=0;
    int acount=0,bcount=0,ccount=0,dcount=0,ecount=0;
    printf("Please enter scores:\n");
   
    while (1)
    {
        scanf("%d",&score);
    a=score/10;
     if (score>=0)
     {
           switch(a)
          {
            case 10:
             case 9:
                {
                    ++acount;
                    ++total;
                    break;
                }
            case 8:
                {
                    ++bcount;
                    ++total;
                    break;
                }
             case 7:
                {
                    ++ccount;
                    ++total;
                    break;
                }
            case 6:
                {
                    ++dcount;
                    ++total;
                    break;
                }
            default:
                {
                    ++ecount;
                    ++total;
                    break;
                }
          }
         
     }
        else
            {
                printf("Sorry.You have input a wrong score.\n");
                ++errtotal;
                break;
            }

    }
    printf("\n有效输入次数为:%d,无效输入次数为:%d\n",total,errtotal);
    printf("总人数为:%d,成绩分布如下:\n",total);
    printf("A级\tB级\tC级\tD级\tE级\n");
    printf("%d人\t%d人\t%d人\t%d人\t%d人\n",acount,bcount,ccount,dcount,ecount);
}
2011-11-12 01:21
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
以下是引用xp0213在2011-11-12 01:21:13的发言:

#include
main()
{
    int score,a,total=0,errtotal=0;
    int acount=0,bcount=0,ccount=0,dcount=0,ecount=0;
    printf("Please enter scores:\n");
   
    while (1)
    {
        scanf("%d",&score);
    a=score/10;
     if (score>=0)
     {
           switch(a)
          {
            case 10:
             case 9:
                {
                    ++acount;
                    ++total;
                    break;
                }
            case 8:
                {
                    ++bcount;
                    ++total;
                    break;
                }
             case 7:
                {
                    ++ccount;
                    ++total;
                    break;
                }
            case 6:
                {
                    ++dcount;
                    ++total;
                    break;
                }
            default:
                {
                    ++ecount;
                    ++total;
                    break;
                }
          }
         
     }
        else
            {
                printf("Sorry.You have input a wrong score.\n");
                ++errtotal;
                break;
            }

    }
    printf("\n有效输入次数为:%d,无效输入次数为:%d\n",total,errtotal);
    printf("总人数为:%d,成绩分布如下:\n",total);
    printf("A级\tB级\tC级\tD级\tE级\n");
    printf("%d人\t%d人\t%d人\t%d人\t%d人\n",acount,bcount,ccount,dcount,ecount);
}



while(1)就是循环语句
2011-11-12 13:08
huhao3225
Rank: 2
来 自:四川成都
等 级:论坛游民
帖 子:39
专家分:25
注 册:2011-11-9
收藏
得分:0 
不对啊,要求是按"ctrl+z"结束,然后输出结果,并在输入负数时提示“Sorry.You have input a wrong score.”,程序仍然继续运行,除非"ctrl+z".
上述程序在输入负数时程序就结束了。
2011-11-12 21:06
yu376021
Rank: 2
等 级:论坛游民
帖 子:41
专家分:68
注 册:2011-11-11
收藏
得分:10 
scanf只度一次,只有放到循环里面才可以多次输入
2011-11-12 21:18
huhao3225
Rank: 2
来 自:四川成都
等 级:论坛游民
帖 子:39
专家分:25
注 册:2011-11-9
收藏
得分:0 
7楼,谢谢,已经发现了,不过输入-1就结束,而不是“ctrl+z”
2011-11-12 21:21
huhao3225
Rank: 2
来 自:四川成都
等 级:论坛游民
帖 子:39
专家分:25
注 册:2011-11-9
收藏
得分:0 
#include<stdio.h>
 main()
 {
     int score,a,x,total=0,errtotal=0;
     int acount=0,bcount=0,ccount=0,dcount=0,ecount=0;
     printf("Please enter scores:\n");
     while (score!=12345)
     {
     scanf("%d",&score);
     a=score/10;
      if (score>=0&&score<=100)
      {
            switch(a)
           {
             case 10:
              case 9:
                 {
                     ++acount;
                     ++total;
                     break;
                 }
             case 8:
                 {
                     ++bcount;
                     ++total;
                     break;
                 }
              case 7:
                 {
                     ++ccount;
                     ++total;
                     break;
                 }
             case 6:
                 {
                     ++dcount;
                     ++total;
                     break;
                 }
             default:
                 {
                     ++ecount;
                     ++total;
                     break;
                 }
           }
           
     }
         else
             {
                 printf("Sorry.You have input a wrong score.\n");
                 ++errtotal;
             }
    }
     printf("\n有效输入次数为:%d,无效输入次数为:%d\n",total,errtotal);
     printf("总人数为:%d,成绩分布如下:\n",total);
     printf("A级\tB级\tC级\tD级\tE级\n");
     printf("%d人\t%d人\t%d人\t%d人\t%d人\n",acount,bcount,ccount,dcount,ecount);
 }
这样就解决了,当输入“12345”时程序结束,输出统计结果。
不过,如果想程序当输入“ctrl+z”时才结束,又该怎样改编呢?
2011-11-12 22:55
huhao3225
Rank: 2
来 自:四川成都
等 级:论坛游民
帖 子:39
专家分:25
注 册:2011-11-9
收藏
得分:0 
原来这样可以,看来还是要靠自己啊~~~~~~~
#include<stdio.h>
 main()
 {
      int score,a,x,total=0,errtotal=0;
      int acount=0,bcount=0,ccount=0,dcount=0,ecount=0;
      printf("Please enter scores:\n");
      while (scanf("%d",&score)!=EOF)
      {
       a=score/10;
       if (score>=0&&score<=100)
       {
             switch(a)
            {
              case 10:
               case 9:
                  {
                      ++acount;
                      ++total;
                      break;
                  }
              case 8:
                  {
                      ++bcount;
                      ++total;
                      break;
                  }
               case 7:
                  {
                      ++ccount;
                      ++total;
                      break;
                  }
              case 6:
                  {
                      ++dcount;
                      ++total;
                      break;
                  }
              default:
                  {
                      ++ecount;
                      ++total;
                      break;
                  }
            }
            
      }
          else
              {
                  printf("Sorry.You have input a wrong score.\n");
                  ++errtotal;
              }
     }
      printf("\n有效输入次数为:%d,无效输入次数为:%d\n",total,errtotal);
      printf("总人数为:%d,成绩分布如下:\n",total);
      printf("A级\tB级\tC级\tD级\tE级\n");
      printf("%d人\t%d人\t%d人\t%d人\t%d人\n",acount,bcount,ccount,dcount,ecount);
 }
2011-11-13 21:41
快速回复:求高人:如果想程序当输入“ctrl+z”时才结束,怎样改编呢?(请见第9 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026778 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved