请在此代码中增加答题窗口:int main() { welcome(); int i, j, k, answer, result; int num1, num2; char op; int score = 0; // 调用欢迎界面函数 welcome(); // 等待用户按下任意键继续 system("pause"); // 清屏 system("cls"); // 生成随机数种子 srand((unsigned)time(NULL)); // 循环出题 for (i = 0; i < 10; i++) { // 生成两个随机数和一个随机运算符 num1 = rand() % 100 + 1; num2 = rand() % 100 + 1; k = rand() % 4; switch (k) { case 0: op = '+'; result = num1 + num2; break; case 1: op = '-'; result = num1 - num2; break; case 2: op = '*'; result = num1 * num2; break; case 3: // 如果除数为0,则重新生成题目 if (num2 == 0) { i--; continue; } // 如果不能整除,则重新生成除数 if (num1 % num2 != 0) { i--; continue; } op = '/'; result = num1 / num2; break; } // 输出题目 printf("第%d题:%d %c %d = ", i + 1, num1, op, num2); // 等待用户输入答案 scanf("%d", &answer); // 判断答案是否正确 if (answer == result) { printf("回答正确!\n"); score += 10; } else { printf("回答错误!\n"); } }
时间: 2024-03-07 22:47:46 浏览: 55
可以在代码中增加以下答题窗口的代码:
```c
#include <windows.h>
// 定义答题窗口的大小和位置
#define BOX_WIDTH 60
#define BOX_HEIGHT 10
#define BOX_LEFT ((GetSystemMetrics(SM_CXSCREEN) - BOX_WIDTH) / 2)
#define BOX_TOP ((GetSystemMetrics(SM_CYSCREEN) - BOX_HEIGHT) / 2)
// 显示答题窗口
void showAnswerWindow() {
// 获取窗口句柄和设备上下文
HWND hwnd = GetForegroundWindow();
HDC hdc = GetDC(hwnd);
// 创建画刷,填充背景色为白色
HBRUSH hbrush = CreateSolidBrush(RGB(255, 255, 255));
RECT rect = { BOX_LEFT, BOX_TOP, BOX_LEFT + BOX_WIDTH, BOX_TOP + BOX_HEIGHT };
FillRect(hdc, &rect, hbrush);
// 绘制答题框和文本框
HBRUSH hbrush2 = CreateSolidBrush(RGB(220, 220, 220));
RECT rect2 = { BOX_LEFT + 10, BOX_TOP + 10, BOX_LEFT + BOX_WIDTH - 10, BOX_TOP + BOX_HEIGHT - 10 };
FillRect(hdc, &rect2, hbrush2);
MoveToEx(hdc, BOX_LEFT + 10, BOX_TOP + 30, NULL);
LineTo(hdc, BOX_LEFT + BOX_WIDTH - 10, BOX_TOP + 30);
TextOut(hdc, BOX_LEFT + 20, BOX_TOP + 20, "请在下方输入答案:", strlen("请在下方输入答案:"));
// 释放资源
DeleteObject(hbrush);
DeleteObject(hbrush2);
ReleaseDC(hwnd, hdc);
}
// 获取用户输入的答案
int getAnswerFromUser() {
int answer;
// 显示答题窗口
showAnswerWindow();
// 获取用户输入
HWND hwnd = GetForegroundWindow();
HDC hdc = GetDC(hwnd);
char str[100];
scanf("%s", str);
answer = atoi(str);
// 清空输入缓冲区
fflush(stdin);
// 释放资源
ReleaseDC(hwnd, hdc);
return answer;
}
```
然后在主函数中使用`getAnswerFromUser()`函数来获取用户输入的答案:
```c
// 循环出题
for (i = 0; i < 10; i++) {
// 生成两个随机数和一个随机运算符
num1 = rand() % 100 + 1;
num2 = rand() % 100 + 1;
k = rand() % 4;
switch (k) {
case 0:
op = '+';
result = num1 + num2;
break;
case 1:
op = '-';
result = num1 - num2;
break;
case 2:
op = '*';
result = num1 * num2;
break;
case 3:
// 如果除数为0,则重新生成题目
if (num2 == 0) {
i--;
continue;
}
// 如果不能整除,则重新生成除数
if (num1 % num2 != 0) {
i--;
continue;
}
op = '/';
result = num1 / num2;
break;
}
// 输出题目
printf("第%d题:%d %c %d = ", i + 1, num1, op, num2);
// 等待用户输入答案
int answer = getAnswerFromUser();
// 判断答案是否正确
if (answer == result) {
printf("回答正确!\n");
score += 10;
} else {
printf("回答错误!\n");
}
}
```
阅读全文