The calculator works as intended with everything but float numbers. I cannot comprehend how inputting different variables to "a" and "c" can throw me to a different part of the if-statement in this case. For example, inputting "2", then "+", then "3", and the calculator works perfectly giving "5.0" as output. However, when inputting "2.1", then "+", then "3.2", the output is "ERR". How is this possible and why is the output not 5.3?
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
int main() {
float a;
char b;
float c;
float res;
char m1[2] = "-";
char m2[2] = "+";
char m3[2] = "*";
char m4[2] = "/";
scanf("%f %c %f", &a, &b, &c);
if (strcmp(&b,m1) == 0) {
res = a - c;
printf("%.1f
", res);
}
else if (strcmp(&b,m2) == 0) {
res = a + c;
printf("%.1f
", res);
}
else if (strcmp(&b,m3) == 0) {
res = a * c;
printf("%.1f
", res);
}
else if (strcmp(&b,m4) == 0) {
res = a / c;
printf("%.1f
", res);
}
else {
printf("ERR");
return 0;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…