#include <stdio.h> #include <stdlib.h> #include <time.h> void getTwoNumber(int* num1, int* num2) { *num1 = rand() % 50; int tmp = rand() % (50 - *num1); *num2 = (*num1 - tmp >= 0) ? tmp : -tmp; } int getTwoNumberOperator() { return rand() % 2; } void checkAnswer(int sum, int* A, int* B, int* C) { int stusum; printf("回答错误,请再尝试一次:"); fflush(stdin); scanf("%d", &stusum); if (sum == stusum) { (*B)++; } else { printf("回答错误,请再尝试一次:"); fflush(stdin); scanf("%d", &stusum); if (sum == stusum) { (*C)++; } } } int main() { int i, A = 0, B = 0, C = 0; int score = 0; srand((int)time(NULL)); for (i = 0; i < 10; i++) { int num1 = 0, num2 = 0, operator = 0; getTwoNumber(&num1, &num2); operator = getTwoNumberOperator(); int sum = operator == 0 ? num1 + num2 : num1 - num2; printf("%d %s %d = ", num1, operator == 0 ? "+" : "-", abs(num2)); fflush(stdin); int stusum; scanf("%d", &stusum); if (sum == stusum) { A++; } else { checkAnswer(sum, &A, &B, &C); } printf("******************\n"); printf("*** %d %s %d = %d ***\n", num1, operator == 0 ? "+" : "-", abs(num2), sum); printf("******************\n"); printf("________________________________\n"); } score = A * 10 + B * 7 + C * 5; printf("score = %d ", score); if (score > 90) { printf("SMART\n"); } else if (score >= 80) { printf("GOOD\n"); } else if (score >= 70) { printf("OK\n"); } else if (score >= 60) { printf("PASS\n"); } else { printf("TRY AGAIN\n"); } return 0; } 此代码有误修改此代码
时间: 2024-01-28 17:03:26 浏览: 63
这段代码存在一个问题:变量名 `operator` 是 C 语言中的关键字,不能作为变量名。以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void getTwoNumber(int* num1, int* num2) {
*num1 = rand() % 50;
int tmp = rand() % (50 - *num1);
*num2 = (*num1 - tmp >= 0) ? tmp : -tmp;
}
int getTwoNumberOperator() {
return rand() % 2;
}
void checkAnswer(int sum, int* A, int* B, int* C) {
int stusum;
printf("回答错误,请再尝试一次:");
fflush(stdin);
scanf("%d", &stusum);
if (sum == stusum) {
(*B)++;
} else {
printf("回答错误,请再尝试一次:");
fflush(stdin);
scanf("%d", &stusum);
if (sum == stusum) {
(*C)++;
}
}
}
int main() {
int i, A = 0, B = 0, C = 0;
int score = 0;
srand((int)time(NULL));
for (i = 0; i < 10; i++) {
int num1 = 0, num2 = 0, opr = 0;
getTwoNumber(&num1, &num2);
opr = getTwoNumberOperator();
int sum = opr == 0 ? num1 + num2 : num1 - num2;
printf("%d %s %d = ", num1, opr == 0 ? "+" : "-", abs(num2));
fflush(stdin);
int stusum;
scanf("%d", &stusum);
if (sum == stusum) {
A++;
} else {
checkAnswer(sum, &A, &B, &C);
}
printf("******************\n");
printf("*** %d %s %d = %d ***\n", num1, opr == 0 ? "+" : "-", abs(num2), sum);
printf("******************\n");
printf("________________________________\n");
}
score = A * 10 + B * 7 + C * 5;
printf("score = %d ", score);
if (score > 90) {
printf("SMART\n");
} else if (score >= 80) {
printf("GOOD\n");
} else if (score >= 70) {
printf("OK\n");
} else if (score >= 60) {
printf("PASS\n");
} else {
printf("TRY AGAIN\n");
}
return 0;
}
```
阅读全文