小学生出题系统

一个小学生四则运算考试系统,本系统需要实现以下功能:

1、主界面包括:计算练习、计算考试、查询成绩、成绩排序、学生信息删除。

2、计算练习时,系统随机出题,运算数均在100以内,运算类型包括加减乘除;除法应能整除,显示算式,键入结果,正确和错误均有提示,出错时允许再输入,最多三次机会,若还不正确,给出答案。根据提示信息继续练习或者退出练习

退出前显示总题数,正确数和正确率。退出练习后根据提示信息进入主界面或者退出系统

3、计算考试时提示输入学号和姓名后,可以输入出题数量,系统随机出题,每题只给一次机会,总分为100分,根据正确率计算得分,统计得分。

结束后,给出总分,显示各题的对错信息,错误的给出答案;并将学生学号、姓名和成绩保存入文件,成绩只保留最高分。考试结束后根据提示信息进入主界面或者退出系统

4、查询成绩时,首先提示输入学生学号和学生姓名,输出学生的考试成绩。 查询结束后退出练习后根据提示信息进入主界面或者退出系统。

5、排序成绩时,将已有学生成绩降序排序,并输出排序前后的结果。排序后根据提示信息进入主界面或者退出系统

6、学生信息删除时,提示输入学号和姓名,删除学生相关信息。如果不存在此学生时,给出提示信息。信息删除后根据提示信息进入主界面或者退出系统

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct Student 
{
    string id;
    string name;
    int score;
};

// 生成100以内的随机整数
int generateRandomNumber() 
{
    return rand() % 101;
}

// 随机选择运算符
char generateRandomOperator() 
{
    char operators[] = { '+', '-', '*', '/' };
    int index = rand() % 4;
    return operators[index];
}

// 生成四则运算表达式,并返回答案
int generateExpression(int& operand1, int& operand2, char& oper) 
{
    operand1 = generateRandomNumber();
    operand2 = generateRandomNumber();
    oper = generateRandomOperator();
    switch (oper) 
    {
    case '+':
        return operand1 + operand2;
    case '-':
        return operand1 - operand2;
    case '*':
        return operand1 * operand2;
    case '/':
        while (operand1 % operand2 != 0)
        {
            operand1 = rand() % 100;
            operand2 = rand() % 100;
        }
        return operand1 / operand2;
    default:
        return 0;
    }
}

// 计算练习
void practice() 
{
    int total = 0, correct = 0;
    int tryCount = 3;
    cout << "********** 计算练习 **********" << endl;
    while (true) 
    {
        int number1, number2, result;
        char oper;
        result = generateExpression(number1, number2, oper);
        cout << number1 << " " << oper << " " << number2 << " = ";
        int inputResult;
        cin >> inputResult;
        if (inputResult == result) 
        {
            cout << "恭喜你,回答正确!" << endl;
            correct++;
        }
        else 
        {
            tryCount--;
            if (tryCount > 0) 
            {
                cout << "回答错误,请再尝试!还有" << tryCount << "次机会。" << endl;
                continue;
            }
            else 
            {
                cout << "回答错误!答案是 " << result << "。" << endl;
            }
        }
        total++;
        cout << "继续练习输入'T',退出练习输入其他任意键:" << endl;
        char choice;
        cin >> choice;
        if (choice != 'T' && choice != 't') 
        {
            cout << "总题数量:" << total << ",正确数量:" << correct << ",正确率:" << (double)correct / total * 100.0 << "%" << endl;
            break;
        }
    }
    cout << "按任意键返回主界面:" << endl;
    cin.ignore();
    cin.get();
}
// 计算考试
void exam() {
    int total = 0, correct = 0;
    int score = 0;
    int numberOfQuestions;
    string id, name;
    cout << "********** 计算考试 **********" << endl;
    cout << "输入学生学号:";
    cin >> id;
    cout << "输入学生姓名:";
    cin >> name;
    cout << "输入出题数量:";
    cin >> numberOfQuestions;
    vector<string> expressions;
    vector<bool> results;
    for (int i = 0; i < numberOfQuestions; i++) 
    {
        int number1, number2, result;
        char oper;
        result = generateExpression(number1, number2, oper);
        string expression = to_string(number1) + " " + oper + " " + to_string(number2) + " = ";
        cout << expression;
        int inputResult;
        cin >> inputResult;
        bool isCorrect = (inputResult == result);
        if (isCorrect) 
        {
            correct++;
            score++;
        }
        total++;
        expressions.push_back(expression);
        results.push_back(isCorrect);
    }
    cout << "********* 考试结束 **********" << endl;
    cout << "总分:100,得分:" << score << endl;
    int number1, number2;
    char oper;
    for (int i = 0; i < expressions.size(); i++) 
    {
        cout << expressions[i] << (results[i] ? "正确" : "错误,答案是 " + to_string(generateExpression(number1, number2, oper))) << endl;
    }
    // 将学生学号、姓名和成绩保存至文件,覆盖保存最高分.
    fstream file;
    file.open("scores.txt", ios::out | ios::app);
    if (file.is_open()) 
    {
        file << id << " " << name << " " << score << endl;
        file.close();
        cout << "学生学号、姓名和成绩已成功保存!" << endl;
    }
    else 
    {
        cout << "保存失败!" << endl;
    }
    cout << "按任意键返回主界面:" << endl;
    cin.ignore();
    cin.get();
}
// 查询成绩
void queryScore() 
{
    string id, name;
    cout << "********** 查询成绩 **********" << endl;
    cout << "输入学生学号:";
    cin >> id;
    cout << "输入学生姓名:";
    cin >> name;
    string searchId, searchName;
    int searchScore;
    fstream file;
    file.open("scores.txt", ios::in);
    bool found = false;
    if (file.is_open()) {
        while (file >> searchId >> searchName >> searchScore) {
            if (id == searchId && name == searchName) {
                cout << "学生学号:" << searchId << ",学生姓名:" << searchName << ",考试成绩:" << searchScore << endl;
                found = true;
                break;
            }
        }
    }

    file.close();

    if (!found) {
        cout << "未找到该学生的成绩!" << endl;
    }

    cout << "按任意键返回主界面:" << endl;
    cin.ignore();
    cin.get();
}
// 成绩排序
bool compareScore(const Student& a, const Student& b)
{
    return a.score > b.score;
}

void sortScore() {
    vector<Student> students;
    fstream file;
    file.open("scores.txt", ios::in);
    Student student;
    if (file.is_open()) 
    {
        while (file >> student.id >> student.name >> student.score) {
            students.push_back(student);
        }
    }
    file.close();
    cout << "********* 成绩排序 **********" << endl;
    cout << "排序前的成绩:" << endl;
    for (const auto& s : students) 
    {
        cout << "学号:" << s.id << ",姓名:" << s.name << ",成绩:" << s.score << endl;
    }
    sort(students.begin(), students.end(), compareScore);
    cout << "排序后的成绩:" << endl;
    for (const auto& s : students) 
    {
        cout << "学号:" << s.id << ",姓名:" << s.name << ",成绩:" << s.score << endl;
    }

    cout << "按任意键返回主界面:" << endl;
    cin.ignore();
    cin.get();
}

// 学生信息删除
void deleteStudent()

{
    string id, name;
    bool found = false;
    cout << "********** 学生信息删除 **********" << endl;
    cout << "输入学生学号:";
    cin >> id;
    cout << "输入学生姓名:";
    cin >> name;
    string searchId, searchName, searchScore;
    vector<string> studentData;
    fstream file;
    file.open("scores.txt", ios::in);
    if (file.is_open())
    {
        while (file >> searchId >> searchName >> searchScore) 
        {
            if (id == searchId && name == searchName) 
            {
                found = true;
                continue;
            }
            studentData.push_back(searchId + " " + searchName + " " + searchScore);
        }
    }
    file.close();
    if (found) 
    {
        file.open("scores.txt", ios::out | ios::trunc);
        if (file.is_open()) 
        {
            for (const auto& data : studentData) 
            {
                file << data << endl;
            }
            file.close();
            cout << "学生信息删除成功!" << endl;
        }
    }
    else 
    {
        cout << "未找到该学生的信息,删除失败!" << endl;
    }
    cout << "按任意键返回主界面:" << endl;
    cin.ignore();
    cin.get();
}
// 主界面
void mainMenu() 
{
    while (true) 
    {
        cout << "********** 主界面 **********" << endl;
        cout << "1. 计算练习" << endl;
        cout << "2. 计算考试" << endl;
        cout << "3. 查询成绩" << endl;
        cout << "4. 成绩排序" << endl;
        cout << "5. 学生信息删除" << endl;
        cout << "0. 退出系统" << endl;
        cout << "*******************************" << endl;
        cout << "请输入选项:";
        int choice;
        cin >> choice;
        switch (choice) 
        {
        case 1:
            practice();
            break;
        case 2:
            exam();
            break;
        case 3:
            queryScore();
            break;
        case 4:
            sortScore();
            break;
        case 5:
            deleteStudent();
            break;
        case 0:
            cout << "感谢使用,再见!" << endl;
            return;
        default:
            cout << "无效选项!请重新输入。" << endl;
            break;
        }
    }
}

int main() 
{
    // 使用当前时间来初始化随机数生成器
    srand(time(0));
    mainMenu();
    return 0;
}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值