一、设计学生信息管理系统界面
导入数据按钮:
(1)属性设置
ID:IDC_INPUTDATA
描述文字: 导入数据
(2)添加数据
ID:IDC_INSERTDATA
描述文字: 添加数据
(3)删除数据
ID:IDC_DELDATA
描述文字: 删除数据
(4)修改数据
ID:IDC_MODIFYDATA
描述文字: 修改数据
(5)退出
ID:IDC_EXIT
描述文字: 退出
(6)班级下拉框、科目下拉框
ID:IDC_CLASSCOMBO
ID:IDC_SUBJECTCOMBO
添加变量:
- CComboBox m_class;
- CComboBox m_subject;
(7)排序下拉框
ID:IDC_SORTCOMBO1
ID:IDC_SORTCOMBO2
添加变量:
- CComboBox m_rule1;
- CComboBox m_rule2;
(8)平均分、最高分、最低分、及格率
ID:IDC_AVGEDIT
ID:IDC_HIGHEDIT
ID: IDC_LOWEDIT
ID: IDC_PASSEDIT
添加变量:
- CString m_avg;
- double m_high;
- double m_low;
- CString m_pass;
(9)列表控件
ID:IDC_LIST
添加变量:
- CListCtrl m_list;
二、设计输入学生信息对话框
ID:IDD_InfoDlg
分别给这些编辑框添加变量:CString m_name,CString m_sex,int m_class,double m_english,double m_chinese,double m_math
添加类:StuInfoSystem
双击确定按钮:
void InfoDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
if (m_name == TEXT("") || m_sex == TEXT("")) {
MessageBox(TEXT("您有输入为空!"), TEXT("提示"));
return;
}
if (m_english < 0 || m_english > 100 || m_chinese < 0 || m_chinese > 100 || m_math < 0 || m_math > 100) {
MessageBox(TEXT("您的成绩输入有误!"), TEXT("提示"));
return;
}
CDialogEx::OnOK();
}
三、查找信息对话框
ID:SeekDlg
添加变量:CString m_name
添加类:SeekDlg
双击确定按钮:
void SeekDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
if (m_name == TEXT("")) {
MessageBox(TEXT("您的输入为空!"), TEXT("提示"));
return;
}
CDialogEx::OnOK();
}
- StuInfo.h
#pragma once
class StuInfo
{
public:
string m_name;
string m_sex;
int m_class_;
double m_english;
double m_chinese;
double m_math;
StuInfo();
StuInfo(string name, string sex, int class_, double english, double chinese, double math);
};
- StuInfo.cpp
#include "pch.h"
#include "StuInfo.h"
StuInfo::StuInfo()
{
}
StuInfo::StuInfo(string name, string sex, int class_, double english, double chinese, double math)
{
m_name = name;
m_sex = sex;
m_class_ = class_;
m_english = english;
m_chinese = chinese;
m_math = math;
}
- SQLInterface.h
#pragma once
#define SQL_MAX 256 // sql语句字符数组最大值
#include "StuInfo.h"
class SQLInterface
{
public:
MYSQL_RES* res; // result
MYSQL_ROW row; // 一行数据
MYSQL mysqlConn;//Connect
char sql[SQL_MAX];// SQL语句
bool Connect_MySQL(); // 连接数据库
vector<StuInfo> Get_All_Info(); // 获取全部信息
bool Add_Info(StuInfo Info); // 添加学生信息
bool Del_Info(StuInfo Info); // 删除学生信息
bool Update_Info(StuInfo old_Info, StuInfo new_Info);//修改学生信息
vector<StuInfo> SeekInfo(CString name); // 查找学生信息
void Close_MYSQL();// 关闭数据库
};
- SQLInterface.cpp
#include "pch.h"
#include "SQLInterface.h"
#include "StuInfo.h"
// 连接数据库
bool SQLInterface::Connect_MySQL()
{
mysql_init(&mysqlConn);
// mysqlConn 数据库结构 localhost 主机名 heheda 用户名 "123456" 密码 stuInfo 数据库 3306 端口
if (!mysql_real_connect(&mysqlConn, "localhost", "heheda", "123456", "stuInfo" , 3306, NULL, 0))
{
AfxMessageBox(_T("访问数据库失败!"));
return false;
}
else
{
AfxMessageBox(_T("成功连接数据库!"));
// 设置字符串,使程序支持中文
mysql_query(&mysqlConn, "SET NAMES 'GB2312'");//解决从数据库中读取数据后汉字乱码显示的问题
}
return true;
}
vector<StuInfo> SQLInterface::Get_All_Info() {
vector<StuInfo> Info;
// C语言组合字符串
snprintf(sql, SQL_MAX, "select * from info");
// 查询数据
int ret = mysql_real_query(&mysqlConn, sql, (unsigned long)strlen(sql));
// 判断查询结果
if (ret) {
AfxMessageBox(_T("查询失败!"));
return Info;
}
// 获取结果集
res = mysql_store_result(&mysqlConn);
// 获取查询到的一行数据
// 给row赋值,判断row是否为空,不为空就打印函数
while (row = mysql_fetch_row(res)) {
StuInfo stuInfo = StuInfo(row[0], row[1], atoi(row[2]), stod(row[3]), stod(row[4]), stod(row[5]));
Info.push_back(stuInfo);
}
// 释放结果集
mysql_free_result(res);
// 关闭数据库
//mysql_close(&mysqlConn);
return Info;
}
// 添加学生信息
bool SQLInterface::Add_Info(StuInfo Info)
{
// C语言字符串组合
snprintf(sql, SQL_MAX, "insert into info values('%s','%s',%d, %lf , %lf , %lf);",
Info.m_name.c_str(), Info.m_sex.c_str(), Info.m_class_, Info.m_english, Info.m_chinese, Info.m_math);
int ret = mysql_real_query(&mysqlConn, sql, (unsigned long)strlen(sql));
if (ret) {
AfxMessageBox(_T("添加失败!"));
return false;
}
return true;
}
bool SQLInterface::Del_Info(StuInfo Info)
{
// C语言字符串组合
snprintf(sql, SQL_MAX, "delete from Info where name = '%s';", Info.m_name.c_str());
int ret = mysql_real_query(&mysqlConn, sql, (unsigned long)strlen(sql));
// 判断查询结果
if (ret) {
AfxMessageBox(_T("删除失败!"));
return false;
}
return true;
}
// 修改学生信息
bool SQLInterface::Update_Info(StuInfo old_Info, StuInfo new_Info)
{
// C语言字符串组合
snprintf(sql, SQL_MAX, "update Info set name = '%s',sex = '%s',className = %d,english = %lf,chinese = %lf,math = %lf where name = '%s';",
new_Info.m_name.c_str(),new_Info.m_sex.c_str(),new_Info.m_class_,new_Info.m_english,new_Info.m_chinese,new_Info.m_math,
old_Info.m_name.c_str());
int ret = mysql_real_query(&mysqlConn, sql, (unsigned long)strlen(sql));
// 判断查询结果
if (ret) {
AfxMessageBox(_T("修改失败!"));
return false;
}
return true;
}
vector<StuInfo> SQLInterface::SeekInfo(CString name)
{
vector<StuInfo> result;
// C语言字符串组合
snprintf(sql, SQL_MAX, "select * from Info where name = '%s'", name);
int ret = mysql_real_query(&mysqlConn, sql, (unsigned long)strlen(sql));
if (ret) {
AfxMessageBox(_T("查询失败!"));
return result;
}
// 获取结果集
res = mysql_store_result(&mysqlConn);
// 获取查询到的一行数据
// 给row赋值,判断row是否为空,不为空就打印数据
while (row = mysql_fetch_row(res)) {
StuInfo stuInfo = StuInfo(row[0], row[1], atoi(row[2]), stod(row[3]), stod(row[4]), stod(row[5]));
result.push_back(stuInfo);
}
// 释放结果集
mysql_free_result(res);
return result;
}
// 关闭数据库
void SQLInterface::Close_MYSQL()
{
mysql_close(&mysqlConn);
}
初始化:
BOOL CStuInfoSystemDlg::OnInitDialog() {
...
// TODO: 在此添加额外的初始化代码
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);// 整行选择、网格线
m_list.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, 105);
m_list.InsertColumn(1, _T("性别"), LVCFMT_LEFT, 105);
m_list.InsertColumn(2, _T("班级"), LVCFMT_LEFT, 105);
m_list.InsertColumn(3, _T("英语"), LVCFMT_LEFT, 105);
m_list.InsertColumn(4, _T("语文"), LVCFMT_LEFT, 105);
m_list.InsertColumn(5, _T("数学"), LVCFMT_LEFT, 105);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
1.双击导入数据按钮:
SQLInterface SQL;
vector<StuInfo> Info;
void CStuInfoSystemDlg::OnBnClickedInputdata()
{
// TODO: 在此添加控件通知处理程序代码
if (SQL.Connect_MySQL()) {
Info = SQL.Get_All_Info();
//MessageBox(TEXT("heheda 是个大笨蛋!"));
UpdateList();
}
else {
return;
}
}
void CStuInfoSystemDlg::UpdateList()
{
CString str;
m_list.DeleteAllItems();
for (int i = 0; i < Info.size(); i++) {
m_list.InsertItem(i, Info[i].m_name.c_str());
m_list.SetItemText(i, 1, Info[i].m_sex.c_str());
str.Format(TEXT("%d"), Info[i].m_class_);
m_list.SetItemText(i, 2, str);
str.Format(TEXT("%.1f"), Info[i].m_english);
m_list.SetItemText(i, 3, str);
str.Format(TEXT("%.1f"), Info[i].m_chinese);
m_list.SetItemText(i, 4, str);
str.Format(TEXT("%.1f"), Info[i].m_math);
m_list.SetItemText(i, 5, str);
}
}
2.双击添加数据按钮:
void CStuInfoSystemDlg::OnBnClickedInsertdata()
{
// TODO: 在此添加控件通知处理程序代码
InfoDlg dlg;
if (dlg.DoModal() == IDOK) {
if (SQL.Add_Info(StuInfo(dlg.m_name.GetBuffer(), dlg.m_sex.GetBuffer(), dlg.m_class, dlg.m_english, dlg.m_chinese, dlg.m_math)))
{
MessageBox(TEXT("添加成功!"),TEXT("提示"));
Info.push_back(StuInfo(dlg.m_name.GetBuffer(), dlg.m_sex.GetBuffer(), dlg.m_class, dlg.m_english, dlg.m_chinese, dlg.m_math));
UpdateList();
}
}
}
3.双击删除按钮
void CStuInfoSystemDlg::OnBnClickedDeldata()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex = m_list.GetSelectionMark(); // 获取选中行的行号
if (nIndex == -1) {
MessageBox(TEXT("请先选择要删除的数据!"),TEXT("提示"));
return;
}
else {
UINT i;
i = MessageBox(_T("确认要删除这条信息吗?"), _T("提示"), MB_YESNO | MB_ICONQUESTION);
if (i == IDYES) {
if (SQL.Del_Info(Info[nIndex])) {
MessageBox(TEXT("删除成功!"), TEXT("提示"));
Info.erase(Info.begin() + nIndex);
UpdateList();
}
}
}
}
4.双击修改按钮
void CStuInfoSystemDlg::OnBnClickedModifydata()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex = m_list.GetSelectionMark(); // 获取选中行的行号
if (nIndex == -1) {
MessageBox(TEXT("请先选择要修改的数据!"), TEXT("提示"));
return;
}
else {
InfoDlg dlg;
dlg.m_name = Info[nIndex].m_name.c_str();
dlg.m_sex = Info[nIndex].m_sex.c_str();
dlg.m_class = Info[nIndex].m_class_;
dlg.m_english = Info[nIndex].m_english;
dlg.m_chinese = Info[nIndex].m_chinese;
dlg.m_math = Info[nIndex].m_math;
if (dlg.DoModal() == IDOK) {
if (SQL.Update_Info(Info[nIndex], StuInfo(dlg.m_name.GetBuffer(), dlg.m_sex.GetBuffer(), dlg.m_class, dlg.m_english, dlg.m_chinese, dlg.m_math))) {
MessageBox(TEXT("修改成功!"), TEXT("提示"));
Info[nIndex] = StuInfo(dlg.m_name.GetBuffer(), dlg.m_sex.GetBuffer(), dlg.m_class, dlg.m_english, dlg.m_chinese, dlg.m_math);
UpdateList();
}
}
}
}
5.双击查找按钮
void CStuInfoSystemDlg::OnBnClickedFinddata()
{
// TODO: 在此添加控件通知处理程序代码
SeekDlg dlg;
if (dlg.DoModal() == IDOK) {
vector<StuInfo> result = SQL.SeekInfo(dlg.m_name);
if (result.empty()) {
MessageBox(TEXT("查无此人!"), TEXT("提示"));
return;
}
CString str;
m_list.DeleteAllItems();
for (int i = 0; i < result.size(); i++) {
m_list.InsertItem(i, result[i].m_name.c_str());
m_list.SetItemText(i, 1, result[i].m_sex.c_str());
str.Format(TEXT("%d"), result[i].m_class_);
m_list.SetItemText(i, 2, str);
str.Format(TEXT("%.1f"), result[i].m_english);
m_list.SetItemText(i, 3, str);
str.Format(TEXT("%.1f"), result[i].m_chinese);
m_list.SetItemText(i, 4, str);
str.Format(TEXT("%.1f"), result[i].m_math);
m_list.SetItemText(i, 5, str);
}
}
}
6.双击退出按钮
void CStuInfoSystemDlg::OnBnClickedExit()
{
// TODO: 在此添加控件通知处理程序代码
UINT i;
i = MessageBox(_T("确认要退出吗?"), _T("提示"), MB_YESNO | MB_ICONQUESTION);
if (i == IDYES) {
// 关闭数据库
SQL.Close_MYSQL();
exit(0);
}
}
7.双击计算按钮
初始化:
BOOL CStuInfoSystemDlg::OnInitDialog() {
...
// TODO: 在此添加额外的初始化代码
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);// 整行选择、网格线
m_list.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, 105);
m_list.InsertColumn(1, _T("性别"), LVCFMT_LEFT, 105);
m_list.InsertColumn(2, _T("班级"), LVCFMT_LEFT, 105);
m_list.InsertColumn(3, _T("英语"), LVCFMT_LEFT, 105);
m_list.InsertColumn(4, _T("语文"), LVCFMT_LEFT, 105);
m_list.InsertColumn(5, _T("数学"), LVCFMT_LEFT, 105);
// 注释:新添加代码
m_class.AddString(TEXT("1班"));
m_class.AddString(TEXT("2班"));
m_class.AddString(TEXT("3班"));
m_class.AddString(TEXT("全部"));
m_class.SetCurSel(3);
m_subject.AddString(TEXT("英语"));
m_subject.AddString(TEXT("语文"));
m_subject.AddString(TEXT("数学"));
m_subject.SetCurSel(0);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CStuInfoSystemDlg::OnBnClickedCalculatebutton()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
int num = 0;
double sum = 0;
double highScore = 0;
double lowScore = 100;
int pass = 0;
int curClass = m_class.GetCurSel();
int curSubject = m_subject.GetCurSel();
vector<double> selectedScore;
for (int i = 0; i < Info.size(); i++) {
if (Info[i].m_class_ == curClass + 1 || curClass == 3) {
switch (curSubject) { // 统计科目
case 0:
selectedScore.push_back(Info[i].m_english); // 英语
break;
case 1:
selectedScore.push_back(Info[i].m_chinese); // 语文
break;
case 2:
selectedScore.push_back(Info[i].m_math); // 数学
break;
default:
break;
}
}
}
//MessageBox(TEXT("查无此人!"), TEXT("提示"));
for (int i = 0; i < selectedScore.size(); i++) {
sum += selectedScore[i];
highScore = max(highScore, selectedScore[i]);
lowScore = min(lowScore, selectedScore[i]);
if (selectedScore[i] >= 60) pass++;
}
num = selectedScore.size();
m_avg.Format(TEXT("%.2lf"), sum / num);
m_pass.Format(TEXT("%.2lf"), pass * 1.0 / num);
m_high = highScore;
m_low = lowScore;
UpdateData(FALSE);
}
8.ID为IDC_SORTCOMBO1 的排序下拉框,属性->控制事件->CBN_SELCHANGE
ID为IDC_SORTCOMBO2 的排序下拉框,属性->控制事件->CBN_SELCHANGE
BOOL CStuInfoSystemDlg::OnInitDialog() {
...
// TODO: 在此添加额外的初始化代码
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);// 整行选择、网格线
m_list.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, 105);
m_list.InsertColumn(1, _T("性别"), LVCFMT_LEFT, 105);
m_list.InsertColumn(2, _T("班级"), LVCFMT_LEFT, 105);
m_list.InsertColumn(3, _T("英语"), LVCFMT_LEFT, 105);
m_list.InsertColumn(4, _T("语文"), LVCFMT_LEFT, 105);
m_list.InsertColumn(5, _T("数学"), LVCFMT_LEFT, 105);
m_class.AddString(TEXT("1班"));
m_class.AddString(TEXT("2班"));
m_class.AddString(TEXT("3班"));
m_class.AddString(TEXT("全部"));
m_class.SetCurSel(3);
m_subject.AddString(TEXT("英语"));
m_subject.AddString(TEXT("语文"));
m_subject.AddString(TEXT("数学"));
m_subject.SetCurSel(0);
// 注释:新添加代码
m_rule1.AddString(TEXT("班级"));
m_rule1.AddString(TEXT("英语"));
m_rule1.AddString(TEXT("语文"));
m_rule1.AddString(TEXT("数学"));
m_rule1.SetCurSel(0);
m_rule2.AddString(TEXT("升序"));
m_rule2.AddString(TEXT("降序"));
m_rule2.SetCurSel(0);
flag = false;
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
自动生成如下函数
afx_msg void OnCbnSelchangeSortcombo1();
afx_msg void OnCbnSelchangeSortcombo2();
在头文件添加:
static bool flag;
static bool cmp_by_class(StuInfo a, StuInfo b);
static bool cmp_by_english(StuInfo a, StuInfo b);
static bool cmp_by_chinese(StuInfo a, StuInfo b);
static bool cmp_by_math(StuInfo a, StuInfo b);
bool CStuInfoSystemDlg::cmp_by_class(StuInfo a,StuInfo b) {
if(flag == false) return a.m_class_ < b.m_class_;
else return a.m_class_ > b.m_class_;
}
bool CStuInfoSystemDlg::cmp_by_english(StuInfo a, StuInfo b) {
if (flag == false) return a.m_english < b.m_english;
else return a.m_english > b.m_english;
}
bool CStuInfoSystemDlg::cmp_by_chinese(StuInfo a, StuInfo b) {
if (flag == false) return a.m_chinese < b.m_chinese;
else return a.m_chinese > b.m_chinese;
}
bool CStuInfoSystemDlg::cmp_by_math(StuInfo a, StuInfo b) {
if (flag == false) return a.m_math < b.m_math;
else return a.m_math > b.m_math;
}
void CStuInfoSystemDlg::OnCbnSelchangeSortcombo1()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex = m_rule1.GetCurSel();
if (nIndex == 0) sort(Info.begin(), Info.end(), cmp_by_class);
if (nIndex == 1) sort(Info.begin(), Info.end(), cmp_by_english);
if (nIndex == 2) sort(Info.begin(), Info.end(), cmp_by_chinese);
if (nIndex == 3) sort(Info.begin(), Info.end(), cmp_by_math);
UpdateList();
}
void CStuInfoSystemDlg::OnCbnSelchangeSortcombo2()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex = m_rule2.GetCurSel();
if (nIndex == 0) flag = false; // 升序
else flag = true; // 倒序
nIndex = m_rule1.GetCurSel();
if (nIndex == 0) sort(Info.begin(), Info.end(), cmp_by_class);
if (nIndex == 1) sort(Info.begin(), Info.end(), cmp_by_english);
if (nIndex == 2) sort(Info.begin(), Info.end(), cmp_by_chinese);
if (nIndex == 3) sort(Info.begin(), Info.end(), cmp_by_math);
UpdateList();
}
测试:
(1)导入数据
(2)添加数据
(3)点击删除按钮,即可删除;
(4)然后选中一行,点击修改数据
(5)查找数据
(6)选中班级和科目下拉框的选项,点击计算按钮
(8)选中排序的两个下拉框,一个可以选择按照班级、英语、语文、数学排序、另一个是选择升序或者降序
文章来源:https://www.toymoban.com/news/detail-851468.html
文章来源地址https://www.toymoban.com/news/detail-851468.html
到了这里,关于Navicate + MFC 简易学生信息管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!