gpt4 book ai didi

c - 我在这里做错了什么?

转载 作者:太空宇宙 更新时间:2023-11-04 05:25:00 25 4
gpt4 key购买 nike

我已经试过很多次了,但我不知道哪里出了问题!请帮我解决这个错误。

#include <`stdio.h>

int main(void)
{
float accum = 0, number = 0;
char oper;

printf("\nHello. This is a simple 'printing' calculator. Simply enter the number followed");
printf(" by the operator that you wish to use. ");
printf("It is possible to use the standard \noperators ( +, -, *, / ) as well as two extra ");
printf("operators:\n");
printf("1) S, which sets the accumulator; and\n");
printf("2) N, that ends the calculation (N.B. Must place a zero before N). \n");

do
{
printf("\nPlease enter a number and an operator: ");
scanf("%f %c", &number, &oper);
if (number == 0 && oper == 'N')
{
printf("Total = %f", accum);
printf("\nEnd of calculations.");
}
else if (oper == '+', '-', '*', '/', 'S')
{
switch (oper)
{
case 'S':
accum = number;
printf("= %f", accum);
break;
case '+':
accum = accum + number;
printf("= %f", accum);
break;
case '-':
accum = accum - number;
printf("= %f", accum);
break;
case '*':
accum = accum * number;
printf("= %f", accum);
break;
case '/':
if (number != 0)
{
accum = accum / number;
printf("= %f", accum);
}
else
printf("Cannot divide by zero.");
break;
default:
printf("Error. Please ensure you enter a correct number and operator.");
break;
}
}
else
printf("Error. Please ensure you enter a correct number and operator.");
}
while (oper != 'N');
return 0;
}

当我编译此代码时,出现以下错误,如此处的快照图像所示。 snapshot of error message

最佳答案

遵循 ,-operator this 的规则

if (oper == '+', '-', '*', '/', 'S')

和这个一样

if ('-', '*', '/', 'S')

和这个一样

if ('*', '/', 'S')

和这个一样

if ('/', 'S')

和这个一样

if ('S')

不等于 0,但始终为“true”。

来自C11 Standard (draft) :

6.5.17 Comma operator

[...]

Semantics

2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

关于c - 我在这里做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33519393/

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