我正在制作一个科学计算器。并想到循环语句,直到用户使用 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 optionn");
}
}
return 0;
}
我希望它只是退出并退出程序,但它printf("Enter two numbers ---n");
要求输入,输入值后退出。
但即使在输入 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)
也是如此,因此检查它返回 2 ...
正如@OznOg在对情况 12 和 13 的评论中所说,您只需要读取一个数字,而不是两个。另请注意,如果代码无效,询问数字是没有用的,因此:
if (scanf("%d",&a) != 1)
return -1;
if (a == 0)
return 0;
if ((a < 0) || (a > 14))
printf("Enter correct optionn");
else if ((a == 12) || (a == 13)) {
printf("Enter one numbern");
if (scanf("%f",&b) != 1)
return -1;
if (a == 12)
printf("%f",log10(b));
else
printf("%f",exp(b)); break;
}
else {
printf("Enter two numbersn");
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;
}
}