[英]C++, constructing a program that reads two real numbers and a character
I am trying to write a program that is able to read two real numbers followed by a character which is inputted by the user. 我正在尝试编写一个程序,该程序能够读取两个实数,然后再读取用户输入的字符。 Then the program will evaluate the two numbers by the character.
然后程序将通过字符评估两个数字。 The character can be any of the ones that I have listed below:
角色可以是我在下面列出的任何角色:
1. + (addition) 1. +(加法)
2. - (subtraction) 2.-(减法)
3. * (multiplication) 3. *(乘法)
4. / (division) 4. /(部门)
5. % (remainder) 5.%(剩余)
Below I have posted the code that I have written just to check if the values printed out are correct: 下面我发布了我编写的代码,用于检查打印出的值是否正确:
#include<stdio.h>
int main(){
float a,b,add,subtract,division,multiply,remainder;
char op;
printf("Enter two real numbers followed by one these characters:+, -, *, /, or % : ");
scanf("%f%f %c",&a,&b,&op);
printf("%.1f %c %.1f\n",a,op,b);
if (op=='+'){
add = a + b;
printf("%f",add);
}
else if (op=='-'){
subtract=a-b;
printf("%f",subtract);
}
else if (op=='/'){
division=a/b;
printf("%f",division);
}
else if (op=='*'){
multiply =a*b;
printf("%f",multiply);
}
else if (op=='%'){
remainder=a%b;
printf("%f",remainder);
}
else{
printf("invalid symbol");
}
return 0;
}
Can anyone tell me why I am getting a run time error? 谁能告诉我为什么我遇到运行时错误?
NOTE: The OP significantly altered the initial question after it had been answered, which is what this post was focusing on, so the answer below may look completely off-target by now. 注意: OP在回答了最初的问题后已大大改变了该问题,而这正是本文所关注的内容,因此,下面的答案目前看来已完全偏离目标。
If anyone can explain why I see different values that would be greatly appreciated.
如果有人能解释为什么我看到不同的价值观,将不胜感激。
There're multiple issues with your code. 您的代码有多个问题。
float
types, but it converts them to int
s. float
类型,但是它将它们转换为int
。 Your scanf
should use "%f %f %c"
instead to take real numbers instead of integer numbers; scanf
应该使用"%f %f %c"
代替实数而不是整数 。 2 2 +
, but your scanf
says "%d%d %c"
(notice the missing space in your format string vs your extra space in your input) 2 2 +
,但是您的scanf
表示"%d%d %c"
(注意格式字符串中的空格与输入中的多余空间) printf
function call needs the arguments swapped to say printf("%f %c %f",a, op, b);
printf
函数调用需要将参数交换为printf("%f %c %f",a, op, b);
(notice the format string using "%f"
and the inversion of op
and b
variables) "%f"
以及op
和b
变量的取反) The 1st point is based on the printed text for the user, requesting "real" numbers. 第一点基于用户的打印文本,要求“真实”数字。
The 2nd and 3rd points are the culprits, because when you enter 2 2 +
on the prompt, your variables look are a = 2
, b = 2
, and op = 43
, which is the numeric value of the '+'
character. 第二点和第三点是罪魁祸首,因为当您在提示符下输入
2 2 +
时,变量外观为a = 2
, b = 2
和op = 43
,这是'+'
字符的数值。
When you then print it, you end up interpreting the '+'
char as if it were an integer and you get 43
instead. 然后,当您打印它时,最终将
'+'
字符解释为整数,而得到的却是43
。
A fixed version of your program is below: 程序的固定版本如下:
#include<stdio.h>
int main(){
float a, b, result;
char op;
printf("%s", "Enter two real numbers followed an operator (+, -, *, /, %): ");
scanf("%f %f %c", &a, &b, &op);
switch(op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
/* make sure b != 0 */
result = a / b;
break;
case '%':
/* make sure b != 0 */
/* we type-cast to int because modulus is not defined for floats */
result = (float)((int)a % (int)b);
break;
default:
printf("%s\n", "Unknown operation");
break;
}
printf("%f %c %f = %f",a, op, b, result);
return 0;
}
Its usage and output: 其用法和输出:
➜ /tmp ./test
Enter two real numbers followed an operator (+, -, *, /, %): 5 5 +
5.000000 + 5.000000 = 10.000000
➜ /tmp ./test
Enter two real numbers followed an operator (+, -, *, /, %): 5 5 *
5.000000 * 5.000000 = 25.000000%
➜ /tmp ./test
Enter two real numbers followed an operator (+, -, *, /, %): 5 5 /
5.000000 / 5.000000 = 1.000000%
➜ /tmp ./test
Enter two real numbers followed an operator (+, -, *, /, %): 10 5 %
10.000000 % 5.000000 = 0.000000%
➜ /tmp ./test
Enter two real numbers followed an operator (+, -, *, /, %): 5 10 %
5.000000 % 10.000000 = 5.000000%
➜ /tmp ./test
Enter two real numbers followed an operator (+, -, *, /, %): 8 5 -
8.000000 - 5.000000 = 3.000000
The problem is in the way you're printing it. 问题在于您的打印方式。 You're trying to print a number as a char and a char as a number:
您正在尝试将数字打印为char和将char打印为数字:
printf("%d %c %d",a,b,op);
I think you meant: 我想你的意思是:
printf("%d %d %c",a,b,op);
So it was just printing the ASCII value of b, which will give you a funny character as you have there. 因此,这只是打印b的ASCII值,这将为您提供一个有趣的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.