接收回车的问题,求教各位!
请教大家一个问题,下面的程序是一个简单的 加减乘除/求三角函数的程序,费了很大的功夫终于算是完成了。但是当我想实现按'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;
}