所以,我正在制作的这个计算器一直有问题(仍在学习 C),它在代码的最后给我一个错误,“预期 }”。但我最后有 },我添加了另一个,看看是否可行,但随后显示“在 do/while 循环中预期”。
#include <stdio.h>
#include <stdbool.h>
int main()
{
float number[100];
int operator = '0';
int doAgainAnswer = 0;
int doAgain;
float finished;
int error;
do{
if(number[1] == 0){
printf("Number 1: ");
scanf("%f", &number[1]);
} else if(number[1] != 0) {
printf("\n\nNumber 1: %.2f\n", number[1]);
printf("Number 2: ");
scanf("%f", &number[2]);
printf("\nOperator:\n1 Addition\n2 Subract\n3 Multiply\n4 Divide.\nChoice: ");
scanf("%d", &operator);
if(operator == 1){
finished = number[1] + number[2];
printf("\n\n%.2f \+ %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 2){
finished = number[1] - number[2];
printf("\n\n%.2f \- %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 3){
finished = number[1] * number[2];
printf("\n\n%.2f \* %.2f is: %.2f", number[1], number[2], finished);
}
if(operator == 4){
finished = number[1] / number[2];
printf("\n\n%.2f \/ %.2f is: %.2f", number[1], number[2], finished);
}
if(operator > 4){
printf("\n\nERROR: Invalid operation.\n\n ");
error = '1';
}
if(error != 1){
printf("\nContinue?\n1: Yes\n2: Yes and use answer as starting value.\n3: No\nAnswer: ");
} else if(error == 1){
printf("\nTry again? 1 Yes 3 No: ");
}
scanf("%d", &doAgainAnswer);
if(doAgainAnswer == 1) {
doAgain = 1;
} else
if(doAgainAnswer == 2){
doAgain = 1;
number[1] = finished;
} else
if(doAgainAnswer == 3){
doAgain = 0;
printf("Goodbye :(");
exit(0);
} else
if(doAgainAnswer > 4){
printf("\n\nERROR INVALID OPERATION.\n\n");
}
}while(doAgain == 1);
return 0;
}