C++课程设计:学校人员信息管理系统(可视化界面)

目录

学校人员信息管理系统

操作演示

MP4转GIF动图

设计功能要求

评分标准

QT Creator安装和新建项目

QT安装

QT新建项目

管理系统程序设计

mainwindow.h 文件

mainwindow.h 程序释义

mainwindow.cpp 文件

mainwindow.cpp 程序释义

main.cpp 文件

TXT文件生成


        博主最近面临SWUST的C++课设作业,我们是有3个题目选择的:电梯控制系统程序设计、学校人员信息管理系统和个人银行账户管理系统。本来想直接用上一届师兄的课设-----电梯控制系统程序设计,但看到题目要求说有可视化界面加大分,但是师兄的并未包含这个内容,所以就自己做一个吧,毕竟我也想要把分数搞高一点呀,哈哈!

但是在这里还是把师兄的课设放在这里供大家参考吧!

电梯控制系统程序设计(控制台):C++课程设计:电梯控制系统程序设计

学校人员信息管理系统

操作演示

OK啦,我们直接看下效果好吧

由于完整操作视频转为GIF图后较大,无法直接写入本博客,想要查看完整演示的可以看下面这个

QT学校人员管理系统

MP4转GIF动图

在这里也给大家安利一波MP4视频文件转GIF动图的方法吧,可以说是百试不爽啦

首先安装 moviepy

pip install moviepy

听说有时安装会报错,由于我之前把默认下载源改为清华镜像源的了,所以这里并未出现任何报错,如果大家有安装报错的也可以尝试用镜像源安装,如下:

pip install moviepy -i https://pypi.tuna.tsinghua.edu.cn/simple

接下来输入以下代码进行转换

from moviepy.editor import VideoFileClip

def convert_mp4_to_gif(input_file, output_file, fps=10):
    # 使用with语句确保clip在使用后自动关闭
    with VideoFileClip(input_file) as clip:
        # 将视频转换为GIF
        clip.write_gif(output_file, fps=fps)

if __name__ =='__main__':
    input_file = "E:\Desktop\Qt.mp4" # 替换为你的MP4文件路径
    output_file = 'qt.gif'  # 输出的GIF文件路径
    convert_mp4_to_gif(input_file, output_file)

设计功能要求

(1)包括一个基本的人员信息类,人员信息有编号、姓名、性别、年龄等,允许用户进行以下操作:开户、销户、登陆;用户登陆后可以查看相关信息。

(2)人员管理管理系统包括教师学生两部分,学生又分本科生和研究生

(3)教师类在人员信息类基础上新增职称和部门等数据信息,新增功能:①输入并保存教师信息;②教师用户登陆后可以查看相关信息。

(4)学生用户登陆后可以查看各门课相关信息(包括成绩、在已输入学生中的成绩排名)。

(5)由教师类和学生类派生一个研究生类,新增研究方向和导师等数据信息,新增功能:①输入并保存信息;②研究生用户登陆后可以查看相关信息。

(6)将所有用户信息存于文件中。

评分标准

1.程序结构图绘制正确,流程清晰,得分(1~10分)。

2. 报告撰写格式规范,语句通顺,逻辑清晰,图表清晰,得分(1~10分)。

3.设计功能的饱满程度,得分(1~20分)。

4.程序功能有必要的注释,注释得分(1~10分)。

5.各章知识综合应用,如第5章的静态数据、常数据,第6章的动态创建数组或vector模板的应用,第7章的继承与派生,第8章的多态,第11章的文件流等,综合应用程度得分(1~30分)。

6.可视化方式实现,难度值加分(10~20分)。

7.控制台实现,难度值加分(1~10分)。

可以看到哈,可视化界面和控制台两种方式连打的基础分都不一样,所以可视化特别重要呀

QT Creator安装和新建项目

可视化界面的实现一定是离不开QT软件的呀,这里给大家简单介绍一下QT的安装和使用方法吧!

QT安装

下载地址:Index of/archive/qt/

官方地址:https://www.qt.io/download-open-source

这里我以官方地址为例,大家根据电脑配置选择合适的即可

QT的安装需要注册,大家按照安装流程正常操作即可

这里选择上面那个,接着一路下一步即可

由于我之前已经安装过QT软件,所以这里可能会继承我之前选择的一些内容,大家如果有步骤和上面不一样(或者需要选择组件的话),可以按照下面的组件选择

QT新建项目

创建项目 - > Application(Qt) - > Qt Widgets Application

这里的名称和路径都可以自定义

这里选择qmake

由于代码的缘故,此处的名字一定要是MainWindow默认就行,否则运行时会报错:类名未引入)注意:Generate form一定要勾选(不然后面也会报错)

这里我只有一个组件,如果大家遇到有多个的话,直接全部勾选即可

最后会生成对应的 .h 、.cpp 以及 .ui 文件

管理系统程序设计

mainwindow.h 文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QVector>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

/***************************
    添加一个具有默认参数值的函数。
    使用引用。
    定义内联函数。
    添加静态数据和常数据。
 ***************************/

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void showAccountCreation();
    void showAccountDeletion();
    void showLogin();
    void returnToMainMenu();
    void createTeacherAccount();
    void createUndergraduateAccount();
    void createGraduateAccount();
    void deleteAccount();
    void login();
    void showTeacherForm();
    void showUndergraduateForm();
    void showGraduateForm();
    void displayUserInfo(const QString& userInfo);
    void showWelcomeScreen();  // 新增的槽函数
    void exitApplication();    // 新增的槽函数


private:
    Ui::MainWindow *ui;
    QStackedWidget *stackedWidget;
    QWidget *mainMenu;
    QWidget *accountCreationMenu;
    QWidget *teacherForm;
    QWidget *undergraduateForm;
    QWidget *graduateForm;
    QWidget *accountDeletionMenu;
    QWidget *loginMenu;
    QWidget *userInfoDisplay;
    QWidget *welcomeScreen;  // 新增的初始界面

    QLabel *welcomeLabel;
    QLabel *welcomeImage;

    QLineEdit *teacherIdInput;
    QLineEdit *teacherNameInput;
    QLineEdit *teacherGenderInput;
    QLineEdit *teacherAgeInput;
    QLineEdit *teacherTitleInput;
    QLineEdit *teacherDepartmentInput;

    QLineEdit *undergradIdInput;
    QLineEdit *undergradNameInput;
    QLineEdit *undergradGenderInput;
    QLineEdit *undergradAgeInput;
    QLineEdit *undergradMathInput;
    QLineEdit *undergradChineseInput;
    QLineEdit *undergradEnglishInput;

    QLineEdit *gradIdInput;
    QLineEdit *gradNameInput;
    QLineEdit *gradGenderInput;
    QLineEdit *gradAgeInput;
    QLineEdit *gradResearchDirectionInput;
    QLineEdit *gradAdvisorInput;

    QLineEdit *deleteIdInput;
    QLineEdit *deleteNameInput;

    QLineEdit *loginIdInput;
    QLineEdit *loginNameInput;

    QLabel *userInfoLabel;

    class Person {
    public:
        QString id;
        QString name;
        QString gender;
        int age;
        virtual QString getInfo() const = 0;
        virtual ~Person() = default;
    };

    class Teacher : public Person {
    public:
        QString title;
        QString department;
        QString getInfo() const override {
            return QString("教师编号: %1\n姓名: %2\n性别: %3\n年龄: %4\n职称: %5\n部门: %6")
                .arg(id).arg(name).arg(gender).arg(age).arg(title).arg(department);
        }
    };

    class Student : public Person {
    public:
        int mathScore;
        int chineseScore;
        int englishScore;
        QString getInfo() const override {
            return QString("学生编号: %1\n姓名: %2\n性别: %3\n年龄: %4\n数学成绩: %5\n语文成绩: %6\n英语成绩: %7")
                .arg(id).arg(name).arg(gender).arg(age).arg(mathScore).arg(chineseScore).arg(englishScore);
        }


    };

    class Graduate : public Person {
    public:
        QString researchDirection;
        QString advisor;
        QString getInfo() const override {
            return QString("研究生编号: %1\n姓名: %2\n性别: %3\n年龄: %4\n研究方向: %5\n导师: %6")
                .arg(id).arg(name).arg(gender).arg(age).arg(researchDirection).arg(advisor);
        }
    };

    QVector<Person*> people;

    void setupMainMenu();
    void setupAccountCreationMenu();
    void setupTeacherForm();
    void setupUndergraduateForm();
    void setupGraduateForm();
    void setupAccountDeletionMenu();
    void setupLoginMenu();
    void setupUserInfoDisplay();
    void setupWelcomeScreen();  // 新增的设置初始界面函数
    void savePeopleInfoToFile(const QString &filename = "people_info.txt"); // 默认参数值的函数
    void loadPeopleInfoFromFile();
    void loadPeopleInfoFromFile(const QString &filename);
    void parsePerson(const QStringList &lines);
    inline void clearInputs(); // 内联函数

};

#endif // MAINWINDOW_H

mainwindow.h 程序释义

这个文件大家主要关注4个点就可以咯,其他的都是QT实现的功能(非重点)

class Person:基本的人员信息录入

class Teacher : public Person:教师新增信息录入,职称和部门

class Student : public Person:本科生信息录入,语数英3科成绩(排名后面会自动排序)

class Graduate : public Person:研究生新增信息录入,研究方向和导师

void savePeopleInfoToFile(const QString &filename = "people_info.txt")filename是最后保存的txt文件路径

此外,下面这个是该 .h 文件包含的C++课设考察内容,具体是哪一部分大家需要自己查看

/***************************
    添加一个具有默认参数值的函数。
    使用引用。
    定义内联函数。
    添加静态数据和常数据。
 ***************************/

mainwindow.cpp 文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <algorithm>
#include <QFile>
#include <QTextStream>
#include <QPixmap>
#include <QDir>
#include <QFile>

/********************************
    实现默认参数值的函数。
    使用引用。
    实现内联函数。
    定义类的构造函数和析构函数。
    使用动态创建的数组或vector模板。
    实现继承与派生以及多态。
    使用文件流。
 ********************************/

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    stackedWidget = new QStackedWidget(this);
    setCentralWidget(stackedWidget);
    this->resize(1100, 800);    //设置窗口大小
    // this->setFixedSize(1000, 800);
    this->setStyleSheet("QWidget { font-size: 18pt; }");    //设置字体大小

    this->setBackgroundRole(QPalette::Window);
    QPalette palette;
    palette.setBrush(QPalette::Window, QBrush(QPixmap("E:\\Desktop\\花.jpg")));  //插入背景图片
    this->setPalette(palette);

    setupWelcomeScreen();  // 设置初始界面
    setupMainMenu();
    setupAccountCreationMenu();
    setupTeacherForm();
    setupUndergraduateForm();
    setupGraduateForm();
    setupAccountDeletionMenu();
    setupLoginMenu();
    setupUserInfoDisplay();

    stackedWidget->addWidget(welcomeScreen);  // 添加初始界面到stackedWidget
    stackedWidget->addWidget(mainMenu);
    stackedWidget->addWidget(accountCreationMenu);
    stackedWidget->addWidget(teacherForm);
    stackedWidget->addWidget(undergraduateForm);
    stackedWidget->addWidget(graduateForm);
    stackedWidget->addWidget(accountDeletionMenu);
    stackedWidget->addWidget(loginMenu);
    stackedWidget->addWidget(userInfoDisplay);

    stackedWidget->setCurrentWidget(welcomeScreen);  // 设置初始界面为当前显示界面

    QString filePath = QDir::currentPath() + "/people_info.txt";
    loadPeopleInfoFromFile(filePath);
}

MainWindow::~MainWindow()
{
    savePeopleInfoToFile();
    delete ui;
    qDeleteAll(people);
}

void MainWindow::setupWelcomeScreen()
{
    welcomeScreen = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(welcomeScreen);

    welcomeLabel = new QLabel("西南科技大学人员信息管理系统", welcomeScreen);
    welcomeLabel->setAlignment(Qt::AlignCenter);

    welcomeImage = new QLabel(welcomeScreen);
    // QPixmap pixmap("E:\\Desktop\\花.jpg");
    // welcomeImage->setPixmap(pixmap);
    welcomeImage->setAlignment(Qt::AlignCenter);

    QPushButton *welcomeButton = new QPushButton("欢迎使用", welcomeScreen);
    QPushButton *exitButton = new QPushButton("退出", welcomeScreen);

    layout->addWidget(welcomeImage);
    layout->addWidget(welcomeLabel);
    layout->addWidget(welcomeButton);
    layout->addWidget(exitButton);

    connect(welcomeButton, &QPushButton::clicked, this, &MainWindow::showWelcomeScreen);
    connect(exitButton, &QPushButton::clicked, this, &MainWindow::exitApplication);

    welcomeScreen->setLayout(layout);
}

void MainWindow::loadPeopleInfoFromFile(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::warning(this, "错误", "无法打开文件读取信息");
        return;
    }

    QTextStream in(&file);
    QStringList lines;

    // 清空当前的people向量
    qDeleteAll(people);
    people.clear();

    while (!in.atEnd()) {
        QString line = in.readLine();
        if (line.startsWith("教师编号") || line.startsWith("学生编号") || line.startsWith("研究生编号")) {
            if (!lines.isEmpty()) {
                parsePerson(lines);
                lines.clear();
            }
        }
        lines.append(line);
    }
    if (!lines.isEmpty()) {
        parsePerson(lines);
    }

    file.close();
}


void MainWindow::parsePerson(const QStringList &lines)
{
    if (lines.isEmpty()) return;

    QString type = lines.first().split(": ").first();
    if (type == "教师编号") {
        Teacher *teacher = new Teacher;
        for (const QString &line : lines) {
            QStringList parts = line.split(": ");
            if (parts.size() != 2) continue;
            QString key = parts[0].trimmed();
            QString value = parts[1].trimmed();
            if (key == "教师编号") teacher->id = value;
            else if (key == "姓名") teacher->name = value;
            else if (key == "性别") teacher->gender = value;
            else if (key == "年龄") teacher->age = value.toInt();
            else if (key == "职称") teacher->title = value;
            else if (key == "部门") teacher->department = value;
        }
        people.append(teacher);
    } else if (type == "学生编号") {
        Student *student = new Student;
        for (const QString &line : lines) {
            QStringList parts = line.split(": ");
            if (parts.size() != 2) continue;
            QString key = parts[0].trimmed();
            QString value = parts[1].trimmed();
            if (key == "学生编号") student->id = value;
            else if (key == "姓名") student->name = value;
            else if (key == "性别") student->gender = value;
            else if (key == "年龄") student->age = value.toInt();
            else if (key == "数学成绩") student->mathScore = value.toInt();
            else if (key == "语文成绩") student->chineseScore = value.toInt();
            else if (key == "英语成绩") student->englishScore = value.toInt();
            // 排名信息可以忽略,因为在其他地方会计算
        }
        people.append(student);
    } else if (type == "研究生编号") {
        Graduate *graduate = new Graduate;
        for (const QString &line : lines) {
            QStringList parts = line.split(": ");
            if (parts.size() != 2) continue;
            QString key = parts[0].trimmed();
            QString value = parts[1].trimmed();
            if (key == "研究生编号") graduate->id = value;
            else if (key == "姓名") graduate->name = value;
            else if (key == "性别") graduate->gender = value;
            else if (key == "年龄") graduate->age = value.toInt();
            else if (key == "研究方向") graduate->researchDirection = value;
            else if (key == "导师") graduate->advisor = value;
        }
        people.append(graduate);
    }
}

void MainWindow::showWelcomeScreen()
{
    stackedWidget->setCurrentWidget(mainMenu);
}

void MainWindow::exitApplication()
{
    QApplication::quit();
}

void MainWindow::savePeopleInfoToFile(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QMessageBox::warning(this, "错误", "无法打开文件保存信息");
        return;
    }

    QTextStream out(&file);

    for (Person *person : people) {
        if (Student *student = dynamic_cast<Student*>(person)) {
            auto calculateRanking = [&](std::function<int(Student*)> scoreFunc) {
                std::vector<int> scores;
                for (Person *p : people) {
                    if (Student *s = dynamic_cast<Student*>(p)) {
                        scores.push_back(scoreFunc(s));
                    }
                }
                std::sort(scores.begin(), scores.end(), std::greater<int>());
                auto iter = std::find(scores.begin(), scores.end(), scoreFunc(student));
                return std::distance(scores.begin(), iter) + 1;
            };

            int mathRank = calculateRanking([](Student *s) { return s->mathScore; });
            int chineseRank = calculateRanking([](Student *s) { return s->chineseScore; });
            int englishRank = calculateRanking([](Student *s) { return s->englishScore; });

            out << student->getInfo() << QString("\t数学成绩排名: %1\t语文成绩排名: %2\t英语成绩排名: %3\n")
                                             .arg(mathRank).arg(chineseRank).arg(englishRank);
        } else {
            out << person->getInfo() << "\n";
        }
    }

    file.close();
}

inline void MainWindow::clearInputs()
{
    teacherIdInput->clear();
    teacherNameInput->clear();
    teacherGenderInput->clear();
    teacherAgeInput->clear();
    teacherTitleInput->clear();
    teacherDepartmentInput->clear();

    undergradIdInput->clear();
    undergradNameInput->clear();
    undergradGenderInput->clear();
    undergradAgeInput->clear();
    undergradMathInput->clear();
    undergradChineseInput->clear();
    undergradEnglishInput->clear();

    gradIdInput->clear();
    gradNameInput->clear();
    gradGenderInput->clear();
    gradAgeInput->clear();
    gradResearchDirectionInput->clear();
    gradAdvisorInput->clear();
}


void MainWindow::setupMainMenu()
{
    mainMenu = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(mainMenu);

    QPushButton *createAccountButton = new QPushButton("开户", mainMenu);
    QPushButton *deleteAccountButton = new QPushButton("销户", mainMenu);
    QPushButton *loginButton = new QPushButton("登录", mainMenu);
    QPushButton *exitButton = new QPushButton("退出", mainMenu);

    // 设置按钮宽度
    createAccountButton->setFixedWidth(600);
    deleteAccountButton->setFixedWidth(600);
    loginButton->setFixedWidth(600);
    exitButton->setFixedWidth(600);

    // 创建水平布局来居中按钮
    QHBoxLayout *createAccountLayout = new QHBoxLayout();
    createAccountLayout->addStretch();
    createAccountLayout->addWidget(createAccountButton);
    createAccountLayout->addStretch();

    QHBoxLayout *deleteAccountLayout = new QHBoxLayout();
    deleteAccountLayout->addStretch();
    deleteAccountLayout->addWidget(deleteAccountButton);
    deleteAccountLayout->addStretch();

    QHBoxLayout *loginLayout = new QHBoxLayout();
    loginLayout->addStretch();
    loginLayout->addWidget(loginButton);
    loginLayout->addStretch();

    QHBoxLayout *exitLayout = new QHBoxLayout();
    exitLayout->addStretch();
    exitLayout->addWidget(exitButton);
    exitLayout->addStretch();

    // 将水平布局添加到垂直布局
    layout->addLayout(createAccountLayout);
    layout->addLayout(deleteAccountLayout);
    layout->addLayout(loginLayout);
    layout->addLayout(exitLayout);

    connect(createAccountButton, &QPushButton::clicked, this, &MainWindow::showAccountCreation);
    connect(deleteAccountButton, &QPushButton::clicked, this, &MainWindow::showAccountDeletion);
    connect(loginButton, &QPushButton::clicked, this, &MainWindow::showLogin);
    connect(exitButton, &QPushButton::clicked, this, &MainWindow::close);

    mainMenu->setLayout(layout);
}


void MainWindow::setupAccountCreationMenu()
{
    accountCreationMenu = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(accountCreationMenu);

    QPushButton *teacherButton = new QPushButton("教师", accountCreationMenu);
    QPushButton *undergradButton = new QPushButton("本科生", accountCreationMenu);
    QPushButton *gradButton = new QPushButton("研究生", accountCreationMenu);
    QPushButton *backButton = new QPushButton("返回", accountCreationMenu);

    layout->addWidget(teacherButton);
    layout->addWidget(undergradButton);
    layout->addWidget(gradButton);
    layout->addWidget(backButton);

    connect(teacherButton, &QPushButton::clicked, this, &MainWindow::showTeacherForm);
    connect(undergradButton, &QPushButton::clicked, this, &MainWindow::showUndergraduateForm);
    connect(gradButton, &QPushButton::clicked, this, &MainWindow::showGraduateForm);
    connect(backButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    accountCreationMenu->setLayout(layout);
}

void MainWindow::setupTeacherForm()
{
    teacherForm = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(teacherForm);

    teacherIdInput = new QLineEdit(teacherForm);
    teacherNameInput = new QLineEdit(teacherForm);
    teacherGenderInput = new QLineEdit(teacherForm);
    teacherAgeInput = new QLineEdit(teacherForm);
    teacherTitleInput = new QLineEdit(teacherForm);
    teacherDepartmentInput = new QLineEdit(teacherForm);

    teacherIdInput->setPlaceholderText("教师编号");
    teacherNameInput->setPlaceholderText("姓名");
    teacherGenderInput->setPlaceholderText("性别");
    teacherAgeInput->setPlaceholderText("年龄");
    teacherTitleInput->setPlaceholderText("职称");
    teacherDepartmentInput->setPlaceholderText("部门");

    QPushButton *saveButton = new QPushButton("保存", teacherForm);
    QPushButton *cancelButton = new QPushButton("取消", teacherForm);

    layout->addWidget(teacherIdInput);
    layout->addWidget(teacherNameInput);
    layout->addWidget(teacherGenderInput);
    layout->addWidget(teacherAgeInput);
    layout->addWidget(teacherTitleInput);
    layout->addWidget(teacherDepartmentInput);
    layout->addWidget(saveButton);
    layout->addWidget(cancelButton);

    connect(saveButton, &QPushButton::clicked, this, &MainWindow::createTeacherAccount);
    connect(cancelButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    teacherForm->setLayout(layout);
}

void MainWindow::setupUndergraduateForm()
{
    undergraduateForm = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(undergraduateForm);

    undergradIdInput = new QLineEdit(undergraduateForm);
    undergradNameInput = new QLineEdit(undergraduateForm);
    undergradGenderInput = new QLineEdit(undergraduateForm);
    undergradAgeInput = new QLineEdit(undergraduateForm);
    undergradMathInput = new QLineEdit(undergraduateForm);
    undergradChineseInput = new QLineEdit(undergraduateForm);
    undergradEnglishInput = new QLineEdit(undergraduateForm);

    undergradIdInput->setPlaceholderText("本科生编号");
    undergradNameInput->setPlaceholderText("姓名");
    undergradGenderInput->setPlaceholderText("性别");
    undergradAgeInput->setPlaceholderText("年龄");
    undergradMathInput->setPlaceholderText("数学成绩");
    undergradChineseInput->setPlaceholderText("语文成绩");
    undergradEnglishInput->setPlaceholderText("英语成绩");

    QPushButton *continueButton = new QPushButton("继续", undergraduateForm);
    QPushButton *finishButton = new QPushButton("结束", undergraduateForm);
    QPushButton *cancelButton = new QPushButton("取消", undergraduateForm);

    layout->addWidget(undergradIdInput);
    layout->addWidget(undergradNameInput);
    layout->addWidget(undergradGenderInput);
    layout->addWidget(undergradAgeInput);
    layout->addWidget(undergradMathInput);
    layout->addWidget(undergradChineseInput);
    layout->addWidget(undergradEnglishInput);
    layout->addWidget(continueButton);
    layout->addWidget(finishButton);
    layout->addWidget(cancelButton);

    connect(continueButton, &QPushButton::clicked, this, &MainWindow::createUndergraduateAccount);
    connect(finishButton, &QPushButton::clicked, [this](){
        createUndergraduateAccount();
        returnToMainMenu();
    });
    connect(cancelButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    undergraduateForm->setLayout(layout);
}

void MainWindow::setupGraduateForm()
{
    graduateForm = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(graduateForm);

    gradIdInput = new QLineEdit(graduateForm);
    gradNameInput = new QLineEdit(graduateForm);
    gradGenderInput = new QLineEdit(graduateForm);
    gradAgeInput = new QLineEdit(graduateForm);
    gradResearchDirectionInput = new QLineEdit(graduateForm);
    gradAdvisorInput = new QLineEdit(graduateForm);

    gradIdInput->setPlaceholderText("研究生编号");
    gradNameInput->setPlaceholderText("姓名");
    gradGenderInput->setPlaceholderText("性别");
    gradAgeInput->setPlaceholderText("年龄");
    gradResearchDirectionInput->setPlaceholderText("研究方向");
    gradAdvisorInput->setPlaceholderText("导师");

    QPushButton *saveButton = new QPushButton("保存", graduateForm);
    QPushButton *cancelButton = new QPushButton("取消", graduateForm);

    layout->addWidget(gradIdInput);
    layout->addWidget(gradNameInput);
    layout->addWidget(gradGenderInput);
    layout->addWidget(gradAgeInput);
    layout->addWidget(gradResearchDirectionInput);
    layout->addWidget(gradAdvisorInput);
    layout->addWidget(saveButton);
    layout->addWidget(cancelButton);

    connect(saveButton, &QPushButton::clicked, this, &MainWindow::createGraduateAccount);
    connect(cancelButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    graduateForm->setLayout(layout);
}

void MainWindow::setupAccountDeletionMenu()
{
    accountDeletionMenu = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(accountDeletionMenu);

    deleteIdInput = new QLineEdit(accountDeletionMenu);
    deleteNameInput = new QLineEdit(accountDeletionMenu);

    deleteIdInput->setPlaceholderText("编号");
    deleteNameInput->setPlaceholderText("姓名");

    QPushButton *deleteButton = new QPushButton("确定销户", accountDeletionMenu);
    QPushButton *cancelButton = new QPushButton("取消", accountDeletionMenu);

    layout->addWidget(deleteIdInput);
    layout->addWidget(deleteNameInput);
    layout->addWidget(deleteButton);
    layout->addWidget(cancelButton);

    connect(deleteButton, &QPushButton::clicked, this, &MainWindow::deleteAccount);
    connect(cancelButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    accountDeletionMenu->setLayout(layout);
}

void MainWindow::setupLoginMenu()
{
    loginMenu = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(loginMenu);

    loginIdInput = new QLineEdit(loginMenu);
    loginNameInput = new QLineEdit(loginMenu);

    loginIdInput->setPlaceholderText("编号");
    loginNameInput->setPlaceholderText("姓名");

    QPushButton *loginButton = new QPushButton("登录", loginMenu);
    QPushButton *cancelButton = new QPushButton("取消", loginMenu);

    layout->addWidget(loginIdInput);
    layout->addWidget(loginNameInput);
    layout->addWidget(loginButton);
    layout->addWidget(cancelButton);

    connect(loginButton, &QPushButton::clicked, this, &MainWindow::login);
    connect(cancelButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    loginMenu->setLayout(layout);
}

void MainWindow::setupUserInfoDisplay()
{
    userInfoDisplay = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(userInfoDisplay);

    userInfoLabel = new QLabel(userInfoDisplay);
    QPushButton *backButton = new QPushButton("返回", userInfoDisplay);
    QPushButton *exitButton = new QPushButton("退出", userInfoDisplay);

    layout->addWidget(userInfoLabel);
    layout->addWidget(backButton);
    layout->addWidget(exitButton);

    connect(backButton, &QPushButton::clicked, this, &MainWindow::showLogin);
    connect(exitButton, &QPushButton::clicked, this, &MainWindow::returnToMainMenu);

    userInfoDisplay->setLayout(layout);
}

void MainWindow::showAccountCreation()
{
    stackedWidget->setCurrentWidget(accountCreationMenu);
}

void MainWindow::showAccountDeletion()
{
    stackedWidget->setCurrentWidget(accountDeletionMenu);
}

void MainWindow::showLogin()
{
    stackedWidget->setCurrentWidget(loginMenu);
}

void MainWindow::returnToMainMenu()
{
    stackedWidget->setCurrentWidget(mainMenu);
    clearInputs();
}

void MainWindow::createTeacherAccount()
{
    // Validate and save teacher information
    if (teacherIdInput->text().isEmpty() || teacherNameInput->text().isEmpty() ||
        teacherGenderInput->text().isEmpty() || teacherAgeInput->text().isEmpty() ||
        teacherTitleInput->text().isEmpty() || teacherDepartmentInput->text().isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入所有信息");
        return;
    }

    Teacher *teacher = new Teacher;
    teacher->id = teacherIdInput->text();
    teacher->name = teacherNameInput->text();
    teacher->gender = teacherGenderInput->text();
    teacher->age = teacherAgeInput->text().toInt();
    teacher->title = teacherTitleInput->text();
    teacher->department = teacherDepartmentInput->text();

    people.append(teacher);

    QMessageBox::information(this, "保存成功", "教师信息已保存");
    returnToMainMenu();
}

void MainWindow::createUndergraduateAccount()
{
    // Validate and save undergraduate information
    if (undergradIdInput->text().isEmpty() || undergradNameInput->text().isEmpty() ||
        undergradGenderInput->text().isEmpty() || undergradAgeInput->text().isEmpty() ||
        undergradMathInput->text().isEmpty() || undergradChineseInput->text().isEmpty() ||
        undergradEnglishInput->text().isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入所有信息");
        return;
    }

    Student *undergrad = new Student;
    undergrad->id = undergradIdInput->text();
    undergrad->name = undergradNameInput->text();
    undergrad->gender = undergradGenderInput->text();
    undergrad->age = undergradAgeInput->text().toInt();
    undergrad->mathScore = undergradMathInput->text().toInt();
    undergrad->chineseScore = undergradChineseInput->text().toInt();
    undergrad->englishScore = undergradEnglishInput->text().toInt();

    people.append(undergrad);

    QMessageBox::information(this, "保存成功", "本科生信息已保存");
    clearInputs();
}

void MainWindow::createGraduateAccount()
{
    // Validate and save graduate information
    if (gradIdInput->text().isEmpty() || gradNameInput->text().isEmpty() ||
        gradGenderInput->text().isEmpty() || gradAgeInput->text().isEmpty() ||
        gradResearchDirectionInput->text().isEmpty() || gradAdvisorInput->text().isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入所有信息");
        return;
    }

    Graduate *grad = new Graduate;
    grad->id = gradIdInput->text();
    grad->name = gradNameInput->text();
    grad->gender = gradGenderInput->text();
    grad->age = gradAgeInput->text().toInt();
    grad->researchDirection = gradResearchDirectionInput->text();
    grad->advisor = gradAdvisorInput->text();

    people.append(grad);

    QMessageBox::information(this, "保存成功", "研究生信息已保存");
    returnToMainMenu();
}

void MainWindow::deleteAccount()
{
    // Validate and delete account information
    if (deleteIdInput->text().isEmpty() || deleteNameInput->text().isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入所有信息");
        return;
    }

    auto it = std::find_if(people.begin(), people.end(), [&](Person* person) {
        return person->id == deleteIdInput->text() && person->name == deleteNameInput->text();
    });

    if (it != people.end()) {
        delete *it;
        people.erase(it);
        QMessageBox::information(this, "销户成功", "账号信息已删除");
    } else {
        QMessageBox::warning(this, "错误", "未找到匹配的账号信息");
    }
    returnToMainMenu();
}

void MainWindow::login()
{
    // Validate and login
    if (loginIdInput->text().isEmpty() || loginNameInput->text().isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入所有信息");
        return;
    }

    auto it = std::find_if(people.begin(), people.end(), [&](Person* person) {
        return person->id == loginIdInput->text() && person->name == loginNameInput->text();
    });

    if (it != people.end()) {
        QString userInfo = (*it)->getInfo();

        if (Student *student = dynamic_cast<Student*>(*it)) {
            // Calculate and add rankings
            auto calculateRanking = [&](std::function<int(Student*)> scoreFunc) {
                std::vector<int> scores;
                for (Person *person : people) {
                    if (Student *s = dynamic_cast<Student*>(person)) {
                        scores.push_back(scoreFunc(s));
                    }
                }
                std::sort(scores.begin(), scores.end(), std::greater<int>());
                auto iter = std::find(scores.begin(), scores.end(), scoreFunc(student));
                return std::distance(scores.begin(), iter) + 1;
            };

            int mathRank = calculateRanking([](Student *s) { return s->mathScore; });
            int chineseRank = calculateRanking([](Student *s) { return s->chineseScore; });
            int englishRank = calculateRanking([](Student *s) { return s->englishScore; });

            userInfo += QString("\n数学成绩排名: %1\t语文成绩排名: %2\t英语成绩排名: %3")
                            .arg(mathRank).arg(chineseRank).arg(englishRank);
        }

        displayUserInfo(userInfo);
    } else {
        QMessageBox::warning(this, "错误", "输入的信息不匹配");
    }
}

void MainWindow::displayUserInfo(const QString &userInfo)
{
    userInfoLabel->setText(userInfo);
    stackedWidget->setCurrentWidget(userInfoDisplay);
}

void MainWindow::showTeacherForm()
{
    stackedWidget->setCurrentWidget(teacherForm);
}

void MainWindow::showUndergraduateForm()
{
    stackedWidget->setCurrentWidget(undergraduateForm);
}

void MainWindow::showGraduateForm()
{
    stackedWidget->setCurrentWidget(graduateForm);
}

mainwindow.cpp 程序释义

这个部分我自己写了一个详细的界面释义,大家感兴趣的可以看一下

此外,为了呈现出的可视化效果更好,我增添了背景图片(可以选择背景图片还是初始界面)

// 图片路径自己更改即可
palette.setBrush(QPalette::Window, QBrush(QPixmap("E:\\Desktop\\花.jpg")));  //插入背景图片

QPixmap pixmap("E:\\Desktop\\花.jpg");    //设置初始界面图片

同样的,这个部分的课设考察内容如下:

/********************************
    实现默认参数值的函数。
    使用引用。
    实现内联函数。
    定义类的构造函数和析构函数。
    使用动态创建的数组或vector模板。
    实现继承与派生以及多态。
    使用文件流。
 ********************************/

main.cpp 文件

// main.cpp
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

/************************************************
     实现目标:
    默认形参值的函数。
    引用的知识。
    内联函数。
    丰富的数据类型。
    正确定义类,类中构造函数、析构函数完整,数据访问属性明确。
    静态数据、常数据。
    指针的正确使用。
    动态创建数组或vector模板的应用。
    继承与派生正确使用,多态。
    文件流的使用。
 ************************************************/

大家直接运行 main.cpp 文件就可以实现全部内容啦!

TXT文件生成

该TXT文件会在点击退出键时保存所有未销户的人员信息,并不会产生覆盖,也就是说重复操作时都只会保存在这一个文件上。

补充:如果大家在运行遇到各种报错的话,大概率是类名引入、图片插入或QT的版本问题。我的QT版本是6.7.0,图片需要插入自己的图片(可能会出现页面刷新重叠问题),类名错误的话可以按照下面的运行演示进行操作。

QT操作演示

总结:这个课设基本上全部实现了功能要求并且基本达到了评分标准,但是这个也存在着一些小缺陷,如研究生的继承方式有误,应该是教师和学生类的多继承,同时也可能有少部分知识点并未使用(如友元函数,剩余请自行查看)。根据这些缺点,大家后面可以进行针对性的优化,也可以把界面弄得更美观点呀!至此,本次的C++课设就全部完成啦,希望对大家的课程设计能够有所帮助呀!

  • 20
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1、问题描述 某高校有四类员工教师、实验员、行政人员教师兼行政人员共有的信息包括编号、姓名、性别、年龄等。其中教师还包含的信息有所在系部、专业、职称实验员还包含的信息由所在实验室、职务行政人员还包含的信息有政治面貌、职称等。 2、功能要求 (1)添加功能程序能够任意添加上述四类人员的记录可提供选择界面供用户选择所要添加的人员类别要求员工的编号要唯一如果添加了重复编号的记录时则提示数据添加重复并取消添加。 (2)查询功能可根据编号、姓名等信息对已添加的记录进行查询如果未找到给出相应的提示信息如果找到则显示相应的记录信息。 (3)显示功能可显示当前系统中所有记录每条记录占据一行。 (4)编辑功能可根据查询结果对相应的记录进行修改修改时注意编号的唯一性。 (5)删除功能主要实现对已添加的人员记录进行删除。如果当前系统中没有相应的人员记录则提示“记录为空”并返回操作否则输入要删除的人员的编号或姓名根据所输入的信息删除该人员记录如果没有找到该人员信息则提示相应的记录不存。 (6)统计功能能根据多种参数进行人员的统计。能统计四类人员数量以及总数,统计男、女员工的数量。 (7)保存功能可将当前系统中各类人员记录存入文件中存入方式任意。 (8)读取功能可将保存在文件中的人员信息读入到当前系统中供用户进行使用。
Linux的可视化日程管理系统课程设计,可以按照如下步骤进行: 1. 确定系统需求和功能:可视化日程管理系统需要实现以下功能: - 用户注册和登录 - 添加和编辑日程事件 - 查看日程事件列表和日历视图 - 搜索和过滤日程事件 - 提醒用户即将到来的日程事件 2. 设计系统架构和数据库结构:可视化日程管理系统可以采用客户端-服务器架构,前端使用GUI界面实现,后端使用MySQL数据库存储数据。数据库可以设计如下的表结构: - users表:存储用户注册信息 - events表:存储日程事件信息,包括事件名称、开始时间、结束时间、地点、提醒时间等字段 3. 实现系统界面和功能:使用C++和Qt库实现系统的界面和功能。可以设计以下界面: - 登录界面:允许用户输入用户名和密码,进行登录 - 注册界面:允许用户输入注册信息,进行账户注册 - 日程事件列表界面:显示用户创建的所有日程事件,包括事件名称、开始时间、结束时间、地点等信息,允许用户编辑、删除和搜索事件 - 日历视图界面:以日历形式显示用户的日程事件,允许用户通过点击日期查看事件详情 - 添加事件界面:允许用户输入新的日程事件信息,包括事件名称、开始时间、结束时间、地点等信息 4. 实现系统的提醒功能:使用Qt的定时器功能实现提醒功能,定时检查数据库中的日程事件,如果有即将到来的事件,弹出提醒窗口提示用户。 5. 测试和调试:进行系统测试和调试,确保系统能够正常运行。 总体来说,Linux的可视化日程管理系统课程设计可以涉及到C++、Qt、MySQL等多个方面的知识,需要进行多方面的学习和实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠眠小虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值