程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> enum { sum = 0, dec, mult, divi }; int main() { int a, b, c; int opration; int loop = 0, flag = 0; int count = 0; time_t starttime; time_t endtime; srand(time(&starttime)); while (loop < 10) { flag = 0; opration = rand() % 4; while (1) { a = rand() % 100; b = rand() % 100; switch (opration) { case sum: { if ((a + b) < 100) { printf("%d + %d = ", a, b); scanf("%d", &c); if (c == a + b) { count++; } flag = 1; } break; } case dec: { if (((a - b) < 100) && (a > b)) { printf("%d - %d = ", a, b); scanf("%d", &c); if (c == a - b) { count++; } flag = 1; } else if (((b - a) < 100) && (a < b)) { printf("%d - %d = ", b, a); scanf("%d", &c); if (c == b - a) { count++; } flag = 1; } break; } case mult: { if (((a * b) < 100) && (a > 1) && (b > 1)) //乘数大于1,可以根据要求修改 { printf("%d × %d = ", a, b); scanf("%d", &c); if (c == a * b) { count++; } flag = 1; } break; } case divi: { if ((a > b) && (b > 1) && (a % b == 0)) //除数数大于1,可以根据要求修改 { printf("%d ÷ %d = ", a, b); scanf("%d", &c); if (c == a / b) { count++; } flag = 1; } else if ((a < b) && (a > 1) && (b % a == 0)) { printf("%d ÷ %d = ", b, a); scanf("%d", &c); if (c == b / a) { count++; } flag = 1; } break; } default: break; } if (flag) { break; } } loop++; } time(&endtime); printf("使用时间%ds\n", endtime- starttime); printf("作对%d题\n", count); }