以下回答参考GPT并且由Bony-整理:
以下是一个简单的 C 语言程序,实现了上述基本要求的功能。程序中包含了用户设置、出题、判断对错和用户排名的功能。请注意,这只是一个基本示例,实际应用中可能需要更多的功能和优化。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义运算符枚举
enum Operator {
ADD, SUBTRACT, MULTIPLY, DIVIDE, OPEN_PARENTHESIS, CLOSE_PARENTHESIS, NONE
};
// 定义用户结构体
typedef struct {
char name[50];
int score;
} User;
// 定义题目结构体
typedef struct {
int operand1;
int operand2;
enum Operator op;
} Question;
// 声明全局变量
User users[3]; // 最多三个用户
int currentUserIndex = 0; // 当前用户索引
// 函数声明
void addUser();
Question generateQuestion();
void displayQuestion(Question q);
int calculateResult(Question q, int answer);
void playGame();
void displayRanking();
void sortUsers();
int main() {
// 设置随机种子
srand(time(NULL));
// 主循环
int choice;
do {
printf("\n1. Add User\n2. Play Game\n3. Display Ranking\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addUser();
break;
case 2:
playGame();
break;
case 3:
displayRanking();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 4);
return 0;
}
// 添加用户
void addUser() {
if (currentUserIndex < 3) {
printf("Enter user name: ");
scanf("%s", users[currentUserIndex].name);
users[currentUserIndex].score = 0;
currentUserIndex++;
printf("User added successfully!\n");
} else {
printf("Maximum number of users reached.\n");
}
}
// 生成一个随机的题目
Question generateQuestion() {
Question q;
q.operand1 = rand() % 10 + 1; // 1 到 10 之间的随机数
q.operand2 = rand() % 10 + 1;
q.op = rand() % 4; // 随机选择加、减、乘、除
return q;
}
// 显示题目
void displayQuestion(Question q) {
char operatorChar;
switch (q.op) {
case ADD:
operatorChar = '+';
break;
case SUBTRACT:
operatorChar = '-';
break;
case MULTIPLY:
operatorChar = '*';
break;
case DIVIDE:
operatorChar = '/';
break;
default:
operatorChar = '?';
}
printf("Calculate: %d %c %d = ?\n", q.operand1, operatorChar, q.operand2);
}
// 计算题目结果,并判断对错
int calculateResult(Question q, int answer) {
int result;
switch (q.op) {
case ADD:
result = q.operand1 + q.operand2;
break;
case SUBTRACT:
result = q.operand1 - q.operand2;
break;
case MULTIPLY:
result = q.operand1 * q.operand2;
break;
case DIVIDE:
result = q.operand1 / q.operand2;
break;
default:
result = 0;
}
if (answer == result) {
printf("Correct!\n");
return 2; // 低年级 2 分
} else {
printf("Incorrect!\n");
return 0;
}
}
// 开始游戏
void playGame() {
if (currentUserIndex == 0) {
printf("No users available. Please add a user first.\n");
return;
}
int rounds;
printf("Enter number of rounds: ");
scanf("%d", &rounds);
for (int i = 0; i < rounds; i++) {
Question q = generateQuestion();
displayQuestion(q);
int answer;
printf("Enter your answer: ");
scanf("%d", &answer);
int score = calculateResult(q, answer);
users[currentUserIndex - 1].score += score;
}
printf("Game over. Scores updated.\n");
}
// 显示排名
void displayRanking() {
if (currentUserIndex == 0) {
printf("No users available. Please add a user first.\n");
return;
}
sortUsers();
printf("Ranking:\n");
for (int i = 0; i < currentUserIndex; i++) {
printf("%d. %s - Score: %d\n", i + 1, users[i].name, users[i].score);
}
}
// 排序用户数组
void sortUsers() {
for (int i = 0; i < currentUserIndex - 1; i++) {
for (int j = 0; j < currentUserIndex - i - 1; j++) {
if (users[j].score < users[j + 1].score) {
// 交换位置
User temp = users[j];
users[j] = users[j + 1];
users[j + 1] = temp;
}
}
}
}
这是一个简单的用户排行榜游戏程序,用户可以添加、排名,进行简单的加减乘除运算,也可以自动产生题目进行小学低年级的练习。用户可以选择进行多轮游戏,每轮后会更新用户得分。游戏结束后,可以查看排名。请注意,这只是一个基本的实现,可以根据需要进行更多的功能扩展和优化。