gpt4 book ai didi

c - while循环中的switch语句

转载 作者:太空宇宙 更新时间:2023-11-04 07:17:47 24 4
gpt4 key购买 nike

<分区>

当我运行这段代码时,这是我的输出:

$ make 6-4 && ./6-4
`6-4' is up to date.
Begin calculations
S 3
= 3.000000
+ 2
Invalid operator
= 3.000000
= 5.000000

它开始很好,“S 3”给我“= 3.000000”。比起我键入“+ 2”,这给了我“无效运算符”和两个答案。这就是我想要的:

S 3
= 3.000000
+ 2
= 5.000000

我错过了什么?

#include<stdio.h>

char operator;
float number = 0;
float result = 0;

main()
{
printf("Begin calculations\n");

while (1) {
scanf("%c%f", &operator, &number);
switch(operator)
{
case '+': {
result += number;
break;
}
case '-': {
result -= number;
break;
}
case '*': {
result *= number;
break;
}
case '/': {
result /= number;
break;
}
case 'S': {
result = number;
break;
}
case 'E': {
return 0;
break;
}
default: {
printf("Invalid operator\n");
break;
}
}
printf(" = %f\n", result);
}
}

谢谢。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com