我在做科学计算器。并且考虑循环语句,直到用户要求通过使用0作为选项之一来停止。但是即使输入0后,它也会在最后一次询问此语句:
printf("Enter two numbers (For only one no. required you can just enter other number anything)\n");
我尝试使用goto, exit(0)
和return
0条语句。甚至while(1)
和for(;;)
也会循环。
#include <stdio.h>
#include<math.h>
int main()
{
int a;
float b,c;
float d=3.14159/180;
while(1)
{
printf("\nScientific Calculator :\n");
printf("Enter option:\n 0- Exit, 1-Add, 2-Sub, 3-Multiply, 4-Divide,\n 5-sin(x), 6-cos(x), 7-tan(x), 8-sinh(x), 9-cosh(x), 10-tanh(x),\n11-log10(x),12-exponent,13-power of x w.r.t y \n");
scanf("%d",&a);
printf("Enter two numbers (For only one no. required you can just enter other number anything)\n"); //Here is where it starts even after return 0
scanf("%f%f",&b,&c); //Here after inputting value it ends.
switch(a)
{
case 0:return 0; //Here is the return 0;
case 1:printf("%d",(int)(b+c)); break;
case 2:printf("%d",(int)(b-c)); break;
case 3:printf("%d",(int)(b*c)); break;
case 4:printf("%f",b/c); break;
case 5:printf("%f",sin(b*d)); break;
case 6:printf("%f",cos(b*d)); break;
case 7:printf("%f",tan(b*d)); break;
case 8:printf("%f",sinh(b*d)); break;
case 9:printf("%f",cosh(b*d)); break;
case 10:printf("%f",cosh(b*d)); break;
case 11:printf("%f",tanh(b*d)); break;
case 12:printf("%f",log10(b)); break;
case 13:printf("%f",exp(b)); break;
case 14:printf("%f",pow(b,c)); break;
default:printf("Enter correct option\n");
}
}
return 0;
}
我希望它退出并退出程序,但它要求输入printf("Enter two numbers ---\n");
,并且在输入值后退出。
答案 0 :(得分:0)
但是即使输入0,它也会在最后一次询问以下语句:
printf("Enter two numbers (For only one no. required you can just enter other number anything)\n");
您需要立即检查大小写'0',所以
printf("Enter option:\n 0- Exit, 1-Add, 2-Sub, 3-Multiply, 4-Divide,\n 5-sin(x), 6-cos(x), 7-tan(x), 8-sinh(x), 9-cosh(x), 10-tanh(x),\n11-log10(x),12-exponent,13-power of x w.r.t y \n"); scanf("%d",&a); printf("Enter two numbers (For only one no. required you can just enter other number anything)\n"); //Here is where it starts even after return 0 scanf("%f%f",&b,&c); //Here after inputting value it ends. switch(a) case 0:return 0; //Here is the return 0; case 1:printf("%d",(int)(b+c)); break; ...
必须
printf("Enter option:\n 0- Exit, 1-Add, 2-Sub, 3-Multiply, 4-Divide,\n 5-sin(x), 6-cos(x), 7-tan(x), 8-sinh(x), 9-cosh(x), 10-tanh(x),\n11-log10(x),12-exponent,13-power of x w.r.t y \n");
scanf("%d",&a);
if (a == 0)
return 0;
printf("Enter two numbers (For only one no. required you can just enter other number anything)\n"); //Here is where it starts even after return 0
scanf("%f%f",&b,&c); //Here after inputting value it ends.
switch(a)
case 1:printf("%d",(int)(b+c)); break;
...
请注意,检查输入的有效 int 也可能是一个好主意,因此请检查scanf("%d",&a)
是否返回1,而对于scanf("%f%f",&b,&c)
则返回1,因此进行检查返回2 ...
正如@OznOg在第12和13种情况中所说,您只需要读取一个数字,而不是两个。还请注意,如果代码无效,则要求输入数字是没有用的,所以:
if (scanf("%d",&a) != 1)
return -1;
if (a == 0)
return 0;
if ((a < 0) || (a > 14))
printf("Enter correct option\n");
else if ((a == 12) || (a == 13)) {
printf("Enter one number\n");
if (scanf("%f",&b) != 1)
return -1;
if (a == 12)
printf("%f",log10(b));
else
printf("%f",exp(b)); break;
}
else {
printf("Enter two numbers\n");
if (scanf("%f%f",&b,&c) != 2)
return -1;
switch(a)
{
case 1:printf("%d",(int)(b+c)); break;
case 2:printf("%d",(int)(b-c)); break;
case 3:printf("%d",(int)(b*c)); break;
case 4:printf("%f",b/c); break;
case 5:printf("%f",sin(b*d)); break;
case 6:printf("%f",cos(b*d)); break;
case 7:printf("%f",tan(b*d)); break;
case 8:printf("%f",sinh(b*d)); break;
case 9:printf("%f",cosh(b*d)); break;
case 10:printf("%f",cosh(b*d)); break;
case 11:printf("%f",tanh(b*d)); break;
case 14:printf("%f",pow(b,c)); break;
}
}