本文主要是介绍C++课程设计:学校人员信息管理系统(可视化界面),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
学校人员信息管理系统
操作演示
MP4转GIF动图
设计功能要求
评分标准
QT Creator安装和新建项目
QT安装
QT新建项目
管理系统程序设计
mainwindow.h 文件
mainwindow.h 程序释义
mainwindow.cpp 文件
mainwindow.cpp 程序释义
main.h 文件
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 VideoFileClipdef convert_mp4_to_gif(input_file, output_file, fps=10):# 使用with语句确保clip在使用后自动关闭with VideoFileClip(input_file) as clip:# 将视频转换为GIFclip.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)将所有用户信息存于文件中。
评分标准
程序中有默认形参值的函数(+3);程序用到引用的知识(+3);程序中用到内联函数(+3);数据类型丰富(+1)。 |
正确定义类(+3);类中构造函数、析构函数完整(+5);数据访问属性明确(+2)。 |
各章知识综合应用,如第5章的静态数据、常数据(+3),指针正确使用(+2);第6章的动态创建数组或vector模板的应用(+5);第7章的继承与派生正确使用(+10),第8章的多态(+5),第11章的文件流等(+5)。 |
可视化方式实现,难度值加分(10~20分) 控制台实现,难度值加分(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
这里的名称可以自定义,注意: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_OBJECTpublic: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"); // 默认参数值的函数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>/********************************实现默认参数值的函数。使用引用。实现内联函数。定义类的构造函数和析构函数。使用动态创建的数组或vector模板。实现继承与派生以及多态。使用文件流。********************************/MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);stackedWidget = new QStackedWidget(this);setCentralWidget(stackedWidget);setupWelcomeScreen(); // 设置初始界面setupMainMenu();setupAccountCreationMenu();setupTeacherForm();setupUndergraduateForm();setupGraduateForm();setupAccountDeletionMenu();setupLoginMenu();setupUserInfoDisplay();stackedWidget->addWidget(welcomeScreen); // 添加初始界面到stackedWidgetstackedWidget->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); // 设置初始界面为当前显示界面
}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("F://QT_code//Manage//image.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::showWelcomeScreen()
{stackedWidget->setCurrentWidget(mainMenu);
}void MainWindow::exitApplication()
{QApplication::quit();
}void MainWindow::savePeopleInfoToFile(const QString &filename)
{QFile file(filename);if (!file.open(QIODevice::Append | QIODevice::Text)) {QMessageBox::warning(this, "错误", "无法打开文件保存信息");return;}QTextStream out(&file);for (Person *person : people) {if (Student *student = dynamic_cast<Student*>(person)) {// Calculate rankings for the studentauto calculateRanking = [&](auto 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 it = std::find(scores.begin(), scores.end(), scoreFunc(student));return std::distance(scores.begin(), it) + 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("\n数学成绩排名: %1\n语文成绩排名: %2\n英语成绩排名: %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);layout->addWidget(createAccountButton);layout->addWidget(deleteAccountButton);layout->addWidget(loginButton);layout->addWidget(exitButton);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 informationif (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 informationif (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 informationif (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 informationif (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 loginif (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 rankingsauto calculateRanking = [&](auto 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 it = std::find(scores.begin(), scores.end(), scoreFunc(student));return std::distance(scores.begin(), it) + 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\n语文成绩排名: %2\n英语成绩排名: %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 程序释义
这个部分我自己写了一个详细的界面释义,大家感兴趣的可以看一下
此外,为了呈现出的可视化效果更好,我在初始界面又增添了图片展示
// 图片路径自己更改即可
QPixmap pixmap("F://QT_code//Manage//image.jpg");
同样的,这个部分的课设考察内容如下:
/********************************实现默认参数值的函数。使用引用。实现内联函数。定义类的构造函数和析构函数。使用动态创建的数组或vector模板。实现继承与派生以及多态。使用文件流。********************************/
main.h 文件
// 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.h 文件就可以实现全部内容啦!
TXT文件生成
该TXT文件会在点击退出键时保存所有未销户的人员信息,并不会产生覆盖,也就是说重复操作时都只会保存在这一个文件上。
总结:这个课设基本上全部实现了功能要求并且达到了评分标准,但就是可视化出的界面太丑咯呀,大家后面可以在该基础上添加背景和字体颜色这些美化的东西哦。至此,本次的C++课设就全部完成啦,希望对大家的课程设计能够有所帮助呀!
这篇关于C++课程设计:学校人员信息管理系统(可视化界面)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!