[英]Simple calculator program in C
I need to write a simple program in C that makes simple calculations of: +,-,*,/我需要用 C 编写一个简单的程序来进行简单的计算:+,-,*,/
Now, I am using Visual Studio Express 2013, and here is the code:现在,我使用的是 Visual Studio Express 2013,代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(){
double a, b;
double sum = 0;
char o; //operator
printf("Enter operator\n");
scanf_s("%c", &o);
printf("Enter first operand\n");
scanf_s("%f", &a);
printf("Enter second operand\n");
scanf_s("%f", &b);
if (o == '+'){
sum = a + b;
printf("The result is", &sum);
}
if (o == '-'){
sum = a - b;
printf("The result is", sum);
}
if (o == '*'){
sum = a*b;
printf("The result is", sum);
}
if (o == '/'){
if (b == !0){
sum = a / b;
printf("The result is", sum);
}
else printf("Error");
}
getchar();
}
My output: Enter operator + Enter first operand 3.5 Enter second operand 5.4我的输出:输入运算符 + 输入第一个操作数 3.5 输入第二个操作数 5.4
And after I type the second number- the program exits, and nothing!在我输入第二个数字后 - 程序退出,什么也没有! There are no compilation errors, and I have no idea what to do.
没有编译错误,我不知道该怎么做。 Can someone help, please?
有人可以帮忙吗?
You're not using printf
correctly.您没有正确使用
printf
。 This is what you're using.这就是你正在使用的。
printf("The result is", &sum);
You're not specifying the type of output in the format string, and you're passing the address of the variable you want to print, not the value.您没有在格式字符串中指定输出类型,而是传递要打印的变量的地址,而不是值。
You should use:你应该使用:
printf("The result is %lf\n", sum);
%lf
is specifying that you want to print a double
, \\n
adds a newline, and you pass the value of the variable sum
, not it's address. %lf
指定要打印double
, \\n
添加换行符,然后传递变量sum
的值,而不是它的地址。
Also, you should change if (b == !0){
to if (b != 0){
.此外,您应该将
if (b == !0){
更改为if (b != 0){
。 If you leave what you put, it's the equivalent to if (b == 1){
, which probably isn't what you want.如果你留下你所放的东西,它相当于
if (b == 1){
,这可能不是你想要的。
EDIT Here is the code, with my modifications, which gives correct results.编辑这是经过我修改的代码,它给出了正确的结果。 I'll indicate which lines I changed
我会指出我更改了哪些行
#include <stdio.h>
#include <stdlib.h>
int main(){
double a, b;
double sum = 0;
char o; //operator
/* I had to use scanf, since I'm not using MS/Visual Studio, but GCC */
printf("Enter operator\n");
scanf("%c", &o);
printf("Enter first operand\n");
scanf("%lf", &a); /* changed %f to %lf */
printf("Enter second operand\n");
scanf("%lf", &b); /* changed %f to %lf */
/* I prefer to use if ... else if ..., this is personal preference */
if (o == '+'){
sum = a + b;
printf("The result is %lf\n", sum); /* Changed, see original post */
} else if (o == '-'){
sum = a - b;
printf("The result is %lf\n", sum); /* Changed, see original post */
} else if (o == '*'){
sum = a*b;
printf("The result is %lf\n", sum); /* Changed, see original post */
} else if (o == '/'){
if (b != 0){
sum = a / b;
printf("The result is %lf\n", sum); /* Changed, see original post */
}
else printf("Error");
}
getchar();
return 0;
}
%f
format to read doubles.%f
格式读取双打。 You should use %lf
: scanf_s("%lf", &a);
%lf
: scanf_s("%lf", &a);
printf("The result is %e", sum);
printf("The result is %e", sum);
if (b == !0)
to if (b != 0)
if (b == !0)
更改为if (b != 0)
Now it's working fine I made some changes to it现在它工作正常我对它做了一些改变
the problem was with ur "scanf_s" and "%f" go throw with it问题出在你的“scanf_s”和“%f”上
#include<stdio.h>
#include<stdlib.h>
int main(){
double a, b;
double sum = 0;
char o; //operator
printf("Enter operator\n");
scanf("%c", &o);
printf("Enter first operand\n");
scanf("%d", &a);
printf("Enter second operand\n");
scanf("%d", &b);
if (o == '+'){
sum = a + b;
printf(" The result is %d", sum);
}
if (o == '-'){
sum = a - b;
printf("The result is %d", sum);
}
if (o == '*'){
sum = a*b;
printf("The result is %d", sum);
}
if (o == '/'){
if (b == !0){
sum = a / b;
printf("The result is %d", sum);
}
else printf("Error");
}
getchar();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.