用c语言编程小学数学学习系统, 具有随机出题、自动评分、查看正确答案、错题重做等功能的测试系统。山 设计程序运行界面 用户登录功能一 能用菜单选择测试系统的各功能,写出代码
时间: 2024-03-20 11:43:48 浏览: 17
以下是一个简单的 C 语言实现,实现了基本的功能,可以根据实际需求进行改进:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 存储用户信息
struct user {
int score;
struct question *questions;
};
struct user *users = NULL;
int num_users = 0;
// 生成随机题目
struct question {
char *text;
int answer;
};
struct question *generate_question() {
static char text[50];
struct question *q = (struct question*)malloc(sizeof(struct question));
// 题目类型:加、减、乘、除
char operators[] = {'+', '-', '*', '/'};
char operator = operators[rand() % 4];
// 生成两个操作数
int a, b;
if (operator == '+') {
a = rand() % 100 + 1;
b = rand() % 100 + 1;
} else if (operator == '-') {
a = rand() % 100 + 1;
b = rand() % a + 1;
} else if (operator == '*') {
a = rand() % 10 + 1;
b = rand() % 10 + 1;
} else {
b = rand() % 10 + 1;
a = b * (rand() % 10 + 1);
}
// 生成题目字符串
sprintf(text, "%d %c %d = ?", a, operator, b);
// 计算正确答案
if (operator == '/') {
q->answer = a / b;
} else {
q->answer = eval(text);
}
q->text = (char*)malloc(strlen(text)+1);
strcpy(q->text, text);
return q;
}
// 用户登录
void login() {
char username[20];
printf("请输入用户名:");
scanf("%s", username);
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].name, username) == 0) {
printf("欢迎回来,%s\n", username);
return;
}
}
num_users++;
users = (struct user*)realloc(users, num_users * sizeof(struct user));
users[num_users-1].name = (char*)malloc(strlen(username)+1);
strcpy(users[num_users-1].name, username);
users[num_users-1].score = 0;
users[num_users-1].questions = NULL;
printf("欢迎新用户%s\n", username);
}
// 生成试卷
struct question **generate_paper() {
struct question **paper = (struct question**)malloc(10 * sizeof(struct question*));
for (int i = 0; i < 10; i++) {
paper[i] = generate_question();
}
return paper;
}
// 开始考试
void start_exam() {
int user_index = -1;
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].name, username) == 0) {
user_index = i;
break;
}
}
if (user_index == -1) {
printf("您还没有登录\n");
return;
}
struct question **paper = generate_paper();
int score = 0;
users[user_index].questions = (struct question*)malloc(10 * sizeof(struct question));
for (int i = 0; i < 10; i++) {
printf("第%d题:%s", i+1, paper[i]->text);
int user_answer;
scanf("%d", &user_answer);
if (user_answer == paper[i]->answer) {
printf("回答正确!\n");
score += 10;
} else {
printf("回答错误,正确答案是%d\n", paper[i]->answer);
}
users[user_index].questions[i] = *paper[i];
}
users[user_index].score += score;
printf("考试结束,本次得分:%d\n", score);
}
// 查看历史成绩
void view_score() {
int user_index = -1;
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].name, username) == 0) {
user_index = i;
break;
}
}
if (user_index == -1) {
printf("您还没有登录\n");
return;
}
printf("您的历史成绩为:%d\n", users[user_index].score);
}
// 查看错题
void view_mistakes() {
int user_index = -1;
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].name, username) == 0) {
user_index = i;
break;
}
}
if (user_index == -1) {
printf("您还没有登录\n");
return;
}
int num_mistakes = 0;
struct question *mistakes = (struct question*)malloc(10 * sizeof(struct question));
for (int i = 0; i < 10; i++) {
if (users[user_index].questions[i].answer != users[user_index].questions[i].user_answer) {
mistakes[num_mistakes] = users[user_index].questions[i];
num_mistakes++;
}
}
if (num_mistakes == 0) {
printf("恭喜您,没有错题!\n");
} else {
printf("您的错题如下:\n");
for (int i = 0; i < num_mistakes; i++) {
printf("题目:%s\n", mistakes[i].text);
printf("您的答案:%d\n", mistakes[i].user_answer);
printf("正确答案:%d\n", mistakes[i].answer);
}
}
}
// 主程序
int main() {
srand(time(NULL));
while (1) {
printf("欢迎使用小学数学学习系统\n");
printf("请选择要进行的操作:\n");
printf("1. 登录\n");
printf("2. 开始考试\n");
printf("3. 查看历史成绩\n");
printf("4. 查看错题\n");
printf("5. 退出\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
login();
} else if (choice == 2) {
start_exam();
} else if (choice == 3) {
view_score();
} else if (choice == 4) {
view_mistakes();
} else if (choice == 5) {
printf("谢谢使用,再见!\n");
break;
} else {
printf("无效的选择,请重新输入\n");
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)