-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCAddStudentDlg.cpp
More file actions
138 lines (115 loc) · 4.03 KB
/
Copy pathCAddStudentDlg.cpp
File metadata and controls
138 lines (115 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// AddStudent.cpp: 实现文件
//
#include "pch.h"
#include "Student Information Management System.h"
#include "CAddStudentDlg.h"
#include "afxdialogex.h"
#include "Student.h"
#include "Management.h"
// AddStudent 对话框
IMPLEMENT_DYNAMIC(CAddStudentDlg, CDialogEx)
CAddStudentDlg::CAddStudentDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_ADD_DIALOG, pParent)
, m_Name_Edit(_T(""))
, m_ID_Edit(_T(""))
, m_Program_Edit(_T(""))
, m_Math_Edit(_T(""))
{
}
CAddStudentDlg::~CAddStudentDlg()
{
}
void CAddStudentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_NAME_EDIT, m_Name_Edit);
DDX_Text(pDX, IDC_PROGRAM_EDIT, m_Program_Edit);
DDX_Text(pDX, IDC_ID_EDIT, m_ID_Edit);
DDX_Text(pDX, IDC_MATH_EDIT, m_Math_Edit);
}
BOOL CAddStudentDlg::OnInitDialog()
{
// TODO: 在此添加额外的初始化代码
CDialogEx::OnInitDialog();
//在窗口左上角设置自己的姓名和学号(课设要求)
CString str = "添加或修改信息";
SetWindowText(str);
if (theApp.Tell)
{
SetDlgItemText(IDC_ID_EDIT, theApp.m_student.ID); //设置显示的学生的学号
SetDlgItemText(IDC_NAME_EDIT, theApp.m_student.name); //设置显示的学生的姓名
if (theApp.m_student.gender == 1)
CheckRadioButton(IDC_MAN, IDC_WOMEN, IDC_MAN);
else
CheckRadioButton(IDC_MAN, IDC_WOMEN, IDC_WOMEN);
//设置显示的学生的性别
//用CheckRadioButton设置RadioButton的显示
SetDlgItemText(IDC_MATH_EDIT, theApp.m_student.math);//设置显示的学生的高数成绩
SetDlgItemText(IDC_PROGRAM_EDIT, theApp.m_student.program);//设置显示的学生的c++课设成绩
}
return TRUE;
}
BEGIN_MESSAGE_MAP(CAddStudentDlg, CDialogEx)
ON_BN_CLICKED(ID_ADD_STUDENT_SAVE, &CAddStudentDlg::OnBnClickedAddStudentSave)
END_MESSAGE_MAP()
// AddStudent 消息处理程序
void CAddStudentDlg::OnBnClickedAddStudentSave()
{
//TODO: 在此添加控件通知处理程序代码
Student temp;
GetDlgItemText(IDC_ID_EDIT, temp.ID, sizeof(temp.ID)); //将学号编辑框的内容写入temp.id
GetDlgItemText(IDC_NAME_EDIT, temp.name, sizeof(temp.name)); //将姓名编辑框的内容写入temp.name
if (IDC_MAN == GetCheckedRadioButton(IDC_MAN, IDC_WOMEN))
temp.gender=true;
else
temp.gender=false;
GetDlgItemText(IDC_MATH_EDIT, temp.math, sizeof(temp.math)); //将高数编辑框的内容写入temp.math
GetDlgItemText(IDC_PROGRAM_EDIT, temp.program, sizeof(temp.program)); //将C++课设编辑框的内容写入temp.program
CFile file;
if (!file.Open("./studentfile.dat", CFile::modeReadWrite | CFile::shareDenyNone))//文件无法打开,想办法创建新的文件类型
{
MessageBox(_T("添加失败,无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
if (theApp.Tell)
{
int i = 0;
Student ur;
while (file.Read(&ur, sizeof(ur))) //读取学生成绩储存文件,找到该学生信息储存的对应位置
{
if ((CString)theApp.m_student.ID == (CString)ur.ID)
{
break;
}
i++;
}
file.Close();
if (!file.Open("./studentfile.dat", CFile::modeWrite | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
file.SeekToBegin(); // 重新设置file文件的写指针时期刚好在要修改的学生信息那里
file.Seek(i * sizeof(temp), CFile::current);//移到文件头
file.Write(&temp, sizeof(temp)); //用新的信息对原来的信息进行覆盖,实现修改
MessageBox(_T("修改保存成功!"), _T("成功"), MB_OK | MB_ICONASTERISK);
file.Close();
theApp.Tell = false;
}
else
{
Student temp2;
while (file.Read(&temp2, sizeof(temp2))) //读取学生文件信息,看学号是否重复
{
if ((CString)temp2.ID == (CString)temp.ID)
{
MessageBox(_T("该学生已存在!"), _T("警告"), MB_OK | MB_ICONWARNING);
return;
}
}
file.SeekToEnd(); //将指针移到文件末尾
file.Write(&temp, sizeof(temp)); //在文件末尾写入新的学生信息
MessageBox(_T("保存成功!"), _T("成功"), MB_OK | MB_ICONASTERISK);
}
OnOK();
}