写一个C语言项目代码,一级界面包括游戏名称、开发者信息(专业、班级、学号、姓名)、选择功能键(加法、减法、乘法、除法、混合计算、退出游戏),单击一级界面的功能键,进入二级界面,二级界面功能键包括开始游戏、返回上一级、结束游戏,在二级界面点击“开始游戏”,在该区域随机出现一个题目如加法减法乘法除法以及混合运算,出现题目后,在该区域有一个答案框,把题目答案填入答案框,后台判断准确与否?如果正确,在该区域下方打印“恭喜”并接着出下一道题目 ; 如果错误,在该区域下方打印 “呜!错了,加油-----”,结束游戏。分数显示区:记录游戏的每次的最高得分,记录本次游戏的实时得分,答对一道题,记10分,并增加显示一朵小红花第三步:显示本次游戏的得分,并显示与最高分的差距选择功能键:“结束”--结束游戏“重新玩”---返回第一步
时间: 2024-03-18 22:42:43 浏览: 44
大一原创C语言学生信息管理系统1.0项目源代码压缩包
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义运算符常量
#define ADDITION 1
#define SUBTRACTION 2
#define MULTIPLICATION 3
#define DIVISION 4
#define MIXED 5
// 定义函数
void printMainMenu();
void printGameMenu();
int generateQuestion(int operation);
int calculateAnswer(int num1, int num2, int operation);
int checkAnswer(int answer, int result);
void printResult(int result, int score, int highScore);
// 全局变量
char name[20], major[20], className[20], studentID[20];
int score = 0, highScore = 0;
int main() {
// 获取玩家信息
printf("请输入姓名:\n");
scanf("%s", name);
printf("请输入专业:\n");
scanf("%s", major);
printf("请输入班级:\n");
scanf("%s", className);
printf("请输入学号:\n");
scanf("%s", studentID);
printf("欢迎来到游戏世界,%s!\n", name);
// 进入一级菜单
while (1) {
printMainMenu();
// 获取玩家选择
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: // 加法
generateQuestion(ADDITION);
break;
case 2: // 减法
generateQuestion(SUBTRACTION);
break;
case 3: // 乘法
generateQuestion(MULTIPLICATION);
break;
case 4: // 除法
generateQuestion(DIVISION);
break;
case 5: // 混合运算
generateQuestion(MIXED);
break;
case 6: // 退出游戏
printf("再见,%s!\n", name);
return 0;
default:
printf("无效的选择,请重新选择!\n");
break;
}
}
return 0;
}
void printMainMenu() {
printf("游戏名称:四则运算游戏\n");
printf("开发者信息:\n");
printf("姓名:%s\n", name);
printf("专业:%s\n", major);
printf("班级:%s\n", className);
printf("学号:%s\n", studentID);
printf("请选择功能:\n");
printf("1. 加法\n");
printf("2. 减法\n");
printf("3. 乘法\n");
printf("4. 除法\n");
printf("5. 混合运算\n");
printf("6. 退出游戏\n");
}
void printGameMenu() {
printf("请选择操作:\n");
printf("1. 开始游戏\n");
printf("2. 返回上一级\n");
printf("3. 结束游戏\n");
}
int generateQuestion(int operation) {
// 随机生成两个数
srand((unsigned)time(NULL));
int num1 = rand() % 100;
int num2 = rand() % 100;
// 根据运算符类型生成问题
int result;
switch (operation) {
case ADDITION:
printf("请计算 %d + %d = ", num1, num2);
result = calculateAnswer(num1, num2, ADDITION);
break;
case SUBTRACTION:
printf("请计算 %d - %d = ", num1, num2);
result = calculateAnswer(num1, num2, SUBTRACTION);
break;
case MULTIPLICATION:
printf("请计算 %d * %d = ", num1, num2);
result = calculateAnswer(num1, num2, MULTIPLICATION);
break;
case DIVISION:
printf("请计算 %d / %d = ", num1, num2);
result = calculateAnswer(num1, num2, DIVISION);
break;
case MIXED:
int operationType = rand() % 4 + 1;
switch (operationType) {
case ADDITION:
printf("请计算 %d + %d = ", num1, num2);
result = calculateAnswer(num1, num2, ADDITION);
break;
case SUBTRACTION:
printf("请计算 %d - %d = ", num1, num2);
result = calculateAnswer(num1, num2, SUBTRACTION);
break;
case MULTIPLICATION:
printf("请计算 %d * %d = ", num1, num2);
result = calculateAnswer(num1, num2, MULTIPLICATION);
break;
case DIVISION:
printf("请计算 %d / %d = ", num1, num2);
result = calculateAnswer(num1, num2, DIVISION);
break;
}
break;
}
// 获取玩家答案
int answer;
scanf("%d", &answer);
// 检查答案
int check = checkAnswer(answer, result);
if (check == 1) {
printf("恭喜,回答正确!\n");
score += 10;
} else {
printf("呜!回答错误,加油-----\n");
printResult(result, score, highScore);
score = 0;
return 0;
}
// 显示得分情况
printResult(result, score, highScore);
// 进入二级菜单
while (1) {
printGameMenu();
// 获取玩家选择
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: // 开始游戏
generateQuestion(operation);
break;
case 2: // 返回上一级
return 0;
case 3: // 结束游戏
printf("再见,%s!\n", name);
exit(0);
default:
printf("无效的选择,请重新选择!\n");
break;
}
}
}
int calculateAnswer(int num1, int num2, int operation) {
switch (operation) {
case ADDITION:
return num1 + num2;
case SUBTRACTION:
return num1 - num2;
case MULTIPLICATION:
return num1 * num2;
case DIVISION:
return num1 / num2;
}
}
int checkAnswer(int answer, int result) {
if (answer == result) {
return 1;
} else {
return 0;
}
}
void printResult(int result, int score, int highScore) {
printf("本次得分:%d\n", score);
int diff = score - highScore;
if (diff > 0) {
printf("新纪录!距离最高分还差 %d 分!\n", diff);
highScore = score;
} else {
printf("距离最高分还差 %d 分!\n", -diff);
}
printf("最高分:%d\n", highScore);
}
```
阅读全文