| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 450 人关注过本帖
标题:接收回车的问题,求教各位!
只看楼主 加入收藏
LowryD
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
接收回车的问题,求教各位!
请教大家一个问题,下面的程序是一个简单的 加减乘除/求三角函数的程序,费了很大的功夫终于算是完成了。但是当我想实现按'q'时回到选择菜单时, 回是回来了但是程序却直接结束,我感觉是不是系统把我的回车算作输入了,实在是一头雾水,求各位赐教!    跳转时我用了goto   跳转语句已经用红色字体标出。

#include<stdio.h>
#include<math.h>
#define PI 3.141592
int main(void)
{
    char ch1,ch2,ch3,judge,dr;  //  judge = yes/no   dr = degree or radian//
    float a1,a2;
    double a,x;
main:
printf("-------------------------------\n");
printf("1.Arithmetic operation\n");
printf("2.Trigonometric operation\n");
printf("-------------------------------\n");
printf("Select one:\n");
scanf("%c",&ch1);
switch(ch1)
{
  loop1:               //   loop 1= continue +-*% program //
  case '1':
      printf("-------------------------------\n");
      printf("- Arithmetic operation -\n");
      printf("a.A+B\n");
      printf("b.A-B\n");
      printf("c.A*B\n");
      printf("d.A/B\n");
      printf("q.Return to main\n");
      printf("-------------------------------\n");
      printf("Select one:\n");
      getchar();
      scanf("%c",&ch2);
           switch(ch2)
           {
                case 'a':
                       printf("Enter the A:");
                       scanf("%f",&a1);
                       printf("Enter the B:");
                       scanf("%f",&a2);
                       printf("\n");
                       printf("%f+%f=%f\n",a1,a2,a1+a2);
                       printf("Do you want to continue?(y/n)\n");
                       getchar();
                       scanf("%c",&judge);
                          switch(judge)
                          {
                               case 'y':
                                   goto loop1;
                               default:
                                   break;
                          }
                 case 'b':
                       printf("Enter the A:");
                       scanf("%f",&a1);
                       printf("Enter the B:");
                       scanf("%f",&a2);
                       printf("\n");
                       printf("%f-%f=%f\n",a1,a2,a1-a2);
                       printf("Do you want to continue?(y/n)");
                       getchar();
                       scanf("%c",&judge);
                          switch(judge)
                          {
                               case 'y':
                                   goto loop1;
                               default:
                                   break;
                          }
                 case 'c':
                       printf("Enter the A:");
                       scanf("%f",&a1);
                       printf("Enter the B:");
                       scanf("%f",&a2);
                       printf("\n");
                       printf("%f*%f=%f\n",a1,a2,a1*a2);
                       printf("Do you want to continue?(y/n)");
                       getchar();
                       scanf("%c",&judge);
                          switch(judge)
                          {
                               case 'y':
                                   goto loop1;
                               default:
                                   break;
                          }
                 case 'd':
                       printf("Enter the A:");
                       scanf("%f",&a1);
                       printf("Enter the B:");
                       scanf("%f",&a2);
                       while(a2==0)
                       {
                       printf("A cannot be divided by 0\n");
                       goto loop1;
                       }
                       printf("\n");
                       printf("%f/%f=%f\n",a1,a2,a1/a2);
                       printf("Do you want to continue?(y/n)");
                       getchar();
                       scanf("%c",&judge);
                          switch(judge)
                          {
                               case 'y':
                                   goto loop1;
                               default:
                                   break;
                          }
                   case 'q':
                              goto main;
                              break;

                   default:
                             printf("Wrong!");
                             goto loop1;
                     
           
           
           
           }
   case '2':
       loop2:  //  continue sin cos...//
       printf("-------------------------------\n");
       printf("a.sin(x)\n");
       printf("b.cos(x)\n");
       printf("c.tan(x)\n");
       printf("d.sin^-1(x)\n");
       printf("e.cos^-1(x)\n");
       printf("f.tan^-1(x)\n");
       printf("q.Return to main\n");
       printf("-------------------------------\n");
       printf("Select one:\n");
       getchar();
       scanf("%c",&ch3);
            switch(ch3)
            {
                    case 'a':
                        printf("Enter the value:");
                        scanf("%lf",&x);
                        getchar();
                        printf("Enter the 'd' for degree or 'r' for radian: ");
                        scanf("%c",&dr);
                        switch(dr)
                        {
                              case 'd':
                                  a=x*PI/180;
                                  printf("sin(%lf)=%lf\n",x,sin(a));
                                  break;
                              case 'r':
                                  printf("sin(%lf)=%lf\n",x,sin(x));
                                  break;
                        }
                        printf("Do you want to continue?(y/n)");
                        getchar();
                        scanf("%c",&judge);
                        switch(judge)
                        {
                               case 'y':
                                   goto loop2;
                               default:
                                   break;
                        }
                    case 'b':
                        printf("Enter the value:");
                        scanf("%lf",&x);
                        getchar();
                        printf("Enter the 'd' for degree or 'r' for radian: ");
                        scanf("%c",&dr);
                        switch(dr)
                        {
                              case 'd':
                                  a=x*PI/180;
                                  printf("cos(%lf)=%lf\n",x,cos(a));
                                  break;
                              case 'r':
                                  printf("cos(%lf)=%lf\n",x,cos(x));
                                  break;
                        }
                        printf("Do you want to continue?(y/n)");
                        getchar();
                        scanf("%c",&judge);
                        switch(judge)
                        {
                               case 'y':
                                   goto loop2;
                               default:
                                   break;
                        }
                    case 'c':
                        printf("Enter the value:");
                        scanf("%lf",&x);
                        getchar();
                        printf("Enter the 'd' for degree or 'r' for radian: ");
                        scanf("%c",&dr);
                        switch(dr)
                        {
                              case 'd':
                                  a=x*PI/180;
                                  printf("tan(%lf)=%lf\n",x,tan(a));
                                  break;
                              case 'r':
                                  printf("tan(%lf)=%lf\n",x,tan(x));
                                  break;
                        }
                        printf("Do you want to continue?(y/n)");
                        getchar();
                        scanf("%c",&judge);
                        switch(judge)
                        {
                               case 'y':
                                   goto loop2;
                               default:
                                   break;
                        }
                    case 'd':
                        printf("Enter the value:");
                        scanf("%lf",&x);
                        getchar();
                        printf("sin^-1(%lf)=%lf  degree \n",x,asin(x)*180/PI);
                        printf("Do you want to continue?(y/n)");
                        scanf("%c",&judge);
                        switch(judge)
                        {
                               case 'y':
                                   goto loop2;
                               default:
                                   break;
                        }
                    case 'e':
                        printf("Enter the value:");
                        scanf("%lf",&x);
                        getchar();
                        printf("cos^-1(%lf)=%lf  degree \n",x,acos(x)*180/PI);
                        printf("Do you want to continue?(y/n)");
                        scanf("%c",&judge);
                        switch(judge)
                        {
                               case 'y':
                                   goto loop2;
                               default:
                                   break;
                        }
                     case 'f':
                        printf("Enter the value:");
                        scanf("%lf",&x);
                        getchar();
                        printf("tan^-1(%lf)=%lf  degree \n",x,atan(x)*180/PI);
                        printf("Do you want to continue?(y/n)");
                        scanf("%c",&judge);
                        switch(judge)
                        {
                               case 'y':
                                   goto loop2;
                               default:
                                   break;
                        }
                     case 'q':
                         goto main;
                         break;

                     default:
                         printf("Wrong.Please try again\n");
                         goto loop2;
                         break;







            }


}
return 0;
}
搜索更多相关主题的帖子: 功夫 double include 
2014-04-16 22:26
ying8501
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:1092
专家分:1446
注 册:2008-11-24
收藏
得分:5 
加一个getchar();就可以消化,输入留下的回车符
2014-04-16 22:33
LowryD
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-22
收藏
得分:0 
回复 2 楼 ying8501
请问这个 getchar();应该加到哪里? 我自己也试过,但是还是不行.. 求赐教
2014-04-16 22:35
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:15 
在你要继续接收字符的时候,在输入语句的前面清空一下标准输入缓冲区:

fflush(stdin);

或者如楼上所说,加getchar();也行的。


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-04-16 22:48
LowryD
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-22
收藏
得分:0 
终于搞明白了,感谢二位!

另: 使用的是fflush(stdin);  请问如果要使用getchar(); 我应该把这段语句放在哪里呢?搞了半天都不行..
2014-04-16 22:55
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
其实getchar本质上就是接收一个字符但是不把接收到的字符赋值给任何变量(抛弃掉)

深入想一想的话,如果当前缓冲区不止一个字符的话,用getchar是徒劳的,所以我建议fflush的办法。

至于应该把getchar加在哪里,其实如果明白了道理你自然也就知道该放在哪里了。


学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2014-04-17 00:59
快速回复:接收回车的问题,求教各位!
数据加载中...
 
   



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

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