C 计算器程序的不准确答案
Inaccurate Answers to a Calculator Program in C
所以我正在学习一些 C,并且我制作了一个简单的计算器,它会根据用户输入的内容执行一些基本计算。该程序使用开关结构,仅此而已。每次我输入计算时,都会给我 0.000000。没有错误给出错误的答案。该程序是使用 VS Code 创建的。节目:
/*
The problem is to write a simple calculator that can add, subtract, multiply, divide, and find the
remainder when one
number is divided by another. The program must allow the calculation that is to be performed to be
keyed in a natural
way, such as 5.6 * 27 or 3 + 6.
*/
#include<stdio.h>
int main(int argc, char* argv[])
{
//declare and initialise objects
double num1 = 0.0; //1st operand value
double num2 = 0.0; //2nd operand value
char operation; //operations that will be performed: +, -, *, /, %
//user input
printf("Enter the calculation: ");
scanf("%1f %c %1f", &num1, &operation, &num2);
//calculations
switch (operation)
{
case '+':
printf(" = %1f\n", num1 + num2);
break;
case '-':
printf(" = %1f\n", num1 - num2);
break;
case '*':
printf(" = %1f\n", num1*num2);
break;
case '/':
if (num2 == 0) //can't divide by zero
{
printf("\n\nDivision by zero error!\n");
}
else
{
printf(" = %1f\n", num1/num2);
}
break;
case '%':
if ((long)num2 == 0) //can't divide by zero
{
printf("\n\nDivision by zero error!\n");
}
else
{
printf(" = %1ld\n", (long)num1 % (long)num2);
}
break;
default:
printf("\n\naIllegal Operation!"); //operation is illegal if we get to here
break;
}
//end of program
return 0;
}
示例:
Enter the calculation: 4.7-3.2
= 0.000000
我觉得问题很简单,但是 Google 失败了,因为我的“搜索”似乎太具体了。任何知道这里发生了什么的人都将不胜感激。
您绝对必须使用和理解编译器警告。如果您理解了它们的原因然后解决了它们,就没有必要在这里 post 了。这些是我在编译您最初 posted:
的代码时收到的警告
main.c:12:11: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
main.c:12:17: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double *’ [-Wformat=]
main.c:47:30: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
违规警告在您的 scanf
中,printf
在 '%'
打印方程式的案例中。
为了通过 scanf
读取 double
值,格式说明符是 %lf
,而不是 %f
。
参见:Reading in double values with scanf in c
为了通过 printf
打印出 long int
值,格式说明符是 %ld
,而不是 %d
。
参见:What is the argument for printf that formats a long?
因此,您的代码需要:
scanf("%lf %c %lf", &num1, &operation, &num2);
并且(与您原来的 post 中的内容相比,后者只有 %1d
):
printf(" = %1ld\n", (long)num1 % (long)num2);
所以我正在学习一些 C,并且我制作了一个简单的计算器,它会根据用户输入的内容执行一些基本计算。该程序使用开关结构,仅此而已。每次我输入计算时,都会给我 0.000000。没有错误给出错误的答案。该程序是使用 VS Code 创建的。节目:
/*
The problem is to write a simple calculator that can add, subtract, multiply, divide, and find the
remainder when one
number is divided by another. The program must allow the calculation that is to be performed to be
keyed in a natural
way, such as 5.6 * 27 or 3 + 6.
*/
#include<stdio.h>
int main(int argc, char* argv[])
{
//declare and initialise objects
double num1 = 0.0; //1st operand value
double num2 = 0.0; //2nd operand value
char operation; //operations that will be performed: +, -, *, /, %
//user input
printf("Enter the calculation: ");
scanf("%1f %c %1f", &num1, &operation, &num2);
//calculations
switch (operation)
{
case '+':
printf(" = %1f\n", num1 + num2);
break;
case '-':
printf(" = %1f\n", num1 - num2);
break;
case '*':
printf(" = %1f\n", num1*num2);
break;
case '/':
if (num2 == 0) //can't divide by zero
{
printf("\n\nDivision by zero error!\n");
}
else
{
printf(" = %1f\n", num1/num2);
}
break;
case '%':
if ((long)num2 == 0) //can't divide by zero
{
printf("\n\nDivision by zero error!\n");
}
else
{
printf(" = %1ld\n", (long)num1 % (long)num2);
}
break;
default:
printf("\n\naIllegal Operation!"); //operation is illegal if we get to here
break;
}
//end of program
return 0;
}
示例:
Enter the calculation: 4.7-3.2
= 0.000000
我觉得问题很简单,但是 Google 失败了,因为我的“搜索”似乎太具体了。任何知道这里发生了什么的人都将不胜感激。
您绝对必须使用和理解编译器警告。如果您理解了它们的原因然后解决了它们,就没有必要在这里 post 了。这些是我在编译您最初 posted:
的代码时收到的警告main.c:12:11: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
main.c:12:17: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double *’ [-Wformat=]
main.c:47:30: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
违规警告在您的 scanf
中,printf
在 '%'
打印方程式的案例中。
为了通过 scanf
读取 double
值,格式说明符是 %lf
,而不是 %f
。
参见:Reading in double values with scanf in c
为了通过 printf
打印出 long int
值,格式说明符是 %ld
,而不是 %d
。
参见:What is the argument for printf that formats a long?
因此,您的代码需要:
scanf("%lf %c %lf", &num1, &operation, &num2);
并且(与您原来的 post 中的内容相比,后者只有 %1d
):
printf(" = %1ld\n", (long)num1 % (long)num2);