新手的每日一问
#include <stdio.h>int main()
{
int a,b;
char process;
float result;
printf("请输入式子:");
scanf("%s",&process);
if (process == "+")
{
a = (int)process;
b = (int)process;
result = a + b;
printf("结果为%f\n",result);
}
else if (process == "-")
{
a = (int)process;
b = (int)process;
result = a - b;
printf("结果为%f\n",result);
}
else if (process == "*")
{
a = (int)process;
b = (int)process;
result = a * b;
printf("结果为%f\n",result);
}
if (process == "/")
{
a = (int)process;
b = (int)process;
result = a / b;
printf("结果为%f\n",result);
}
return 0;
}
这个是一个10以内加减乘除计算器编译通过了但是没有输出{:10_243:}如图一
很奇葩的代码。
int a, b, c;
if (process == '+')
a = (int)process-48;
b = (int)process-48;
c = a + b;
printf("%d\n", c); ba21 发表于 2019-2-22 23:08
很奇葩的代码。
为什么要-48呢? GumpYu 发表于 2019-2-22 23:11
为什么要-48呢?
也许这样写清楚一点
int a, b, c;
if (process == '+'){
a = (int)process-(int)'0';
b = (int)process-(int)'0';
c = a + b;
} 按你思路,只能这么改了:#include <stdio.h>
int main()
{
int a,b;
char process;
float result;
printf("请输入式子:");
scanf("%s",process);
if (process == '+')
{
a = (int)process - 48;
b = (int)process - 48;
printf("结果为%d\n", a+b);
}
else if (process == '-')
{
a = (int)process - 48;
b = (int)process - 48;
printf("结果为%d\n", a-b);
}
else if (process == '*')
{
a = (int)process - 48;
b = (int)process - 48;
printf("结果为%d\n", a*b);
}
else if (process == '/')
{
a = (int)process - 48;
b = (int)process - 48;
printf("结果为%d\n", a/b);
}
return 0;
} Croper 发表于 2019-2-23 00:03
也许这样写清楚一点
好的谢谢了,懂了,忘了这一点了 Charles未晞 发表于 2019-2-23 09:51
按你思路,只能这么改了:
谢谢 您了
页:
[1]