| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 450 人关注过本帖
标题:接收回车的问题,求教各位!
取消只看楼主 加入收藏
LowryD
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
接收回车的问题,求教各位!
请教大家一个问题,下面的程序是一个简单的 加减乘除/求三角函数的程序,费了很大的功夫终于算是完成了。但是当我想实现按'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
LowryD
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-22
收藏
得分:0 
回复 2 楼 ying8501
请问这个 getchar();应该加到哪里? 我自己也试过,但是还是不行.. 求赐教
2014-04-16 22:35
LowryD
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-3-22
收藏
得分:0 
终于搞明白了,感谢二位!

另: 使用的是fflush(stdin);  请问如果要使用getchar(); 我应该把这段语句放在哪里呢?搞了半天都不行..
2014-04-16 22:55
快速回复:接收回车的问题,求教各位!
数据加载中...
 
   



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

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