-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathManagement.cpp
More file actions
355 lines (335 loc) · 8.79 KB
/
Copy pathManagement.cpp
File metadata and controls
355 lines (335 loc) · 8.79 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "pch.h"
#include "Management.h"
bool sort_id(Student a, Student b) //学号大小比较函数
{
return _ttoi((CString)a.ID) < _ttoi((CString)b.ID); //将char转换成CString,再转化成int
}
bool sort_math(Student a, Student b)
{
return _ttoi((CString)a.math) > _ttoi((CString)b.math); //高数比较成绩函数
}
bool sort_sum(Student a, Student b)
{
return (_ttoi((CString)a.program) + _ttoi((CString)a.math)) > (_ttoi((CString)b.program) + _ttoi((CString)b.math)); //总成绩比较函数
}
bool sort_program(Student a, Student b)
{
return _ttoi((CString)a.program) > _ttoi((CString)b.program); //大小比较函数
}
//添加成员
void Management::AddStudent(CListCtrl* pList)
{
CAddStudentDlg temp;
temp.DoModal();//调用CAddStudentDlg将新信息写入ListCtrl
LoadFile(pList);////更新ListCtrl界面
}
//删除成员
void Management::DeleteStudent(CListCtrl* pList)
{
POSITION pos = pList->GetFirstSelectedItemPosition();//找到当前选中行
if (pos == NULL)
{
MessageBox(_T("请选择要删除的成员!"), _T("警告"), MB_OK | MB_ICONWARNING);
return;
}
else
{
while (pos)
{
Student temp;
int nItem = pList->GetNextSelectedItem(pos);//获取当前选中行的行号
pList->GetItemText(nItem, 2, temp.ID, sizeof(temp.ID));//将选中行的学号信息写入临时变量
pList->DeleteItem(nItem);//在ListCtrl中删除选中行
CFile file;
if (!file.Open("./studentfile.dat", CFile::modeRead | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
CFile temporaryfile;//临时数据存储文件,如果不存在则创建新的
if (!temporaryfile.Open("./tempfile.dat", CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
//读取学生成绩储存文件,将未删除的学生信息写入临时文件temporaryfile中
Student u;
while (file.Read(&u, sizeof(u)) == sizeof(u))
{
if ((CString)u.ID == (CString)temp.ID)//根据学号判断是否该写入
continue;
temporaryfile.Write(&u, sizeof(u));
}
file.Close();
temporaryfile.Close();
//这里无论文件是否存在都创建了新的空文件studentfile.dat,是因为原文件中有数据,需要清除
if (!file.Open("./studentfile.dat", CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
if (!temporaryfile.Open("./tempfile.dat", CFile::modeRead | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
//读取学生成绩储存文件,将临时文件tempfile.dat中学生信息写入真正存储学生信息的studentfile.dat中
while (temporaryfile.Read(&u, sizeof(u)) == sizeof(u))
file.Write(&u, sizeof(u));
LoadFile(pList); //更新学生成绩管理系统界面的信息
return;
}
}
}
//修改成员
void Management::ChangeStudent(CListCtrl* pList)
{
CAddStudentDlg tempdlg;
tempdlg.DoModal();
LoadFile(pList);
}
//排序
void Management::SortStudents(CListCtrl* pList, int nSel)
{
Student SomeStudent[100];
CFile file;
if (!file.Open("./studentfile.dat", CFile::modeRead | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
int i = 0;
while (file.Read(&SomeStudent[i], sizeof(SomeStudent[i])) == sizeof(SomeStudent[i]))
{
i++;
}
file.Close();
int b = (CString)SomeStudent[0].ID < (CString)SomeStudent[1].ID;
if (nSel == 0) //0是高数成绩排序
std::sort(SomeStudent, SomeStudent + i, sort_math);
if (nSel == 1) //1是课设成绩排序
std::sort(SomeStudent, SomeStudent + i, sort_program);
if (nSel == 2) //2是学号排序
std::sort(SomeStudent, SomeStudent + i, sort_id);
if (nSel == 3) //3是总成绩排序
std::sort(SomeStudent, SomeStudent + i, sort_sum);
if (!file.Open("./studentfile.dat", CFile::modeWrite | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
int t = 0;
while (t < i) //将排好序的信息写入studentfile.dat
{
file.Write(&SomeStudent[t], sizeof(SomeStudent[t]));
t++;
}
file.Close();
LoadFile(pList); ////更新ListCtrl界面
}
//更新ListCtrl界面
void Management::LoadFile(CListCtrl* pList)
{
CFile file;//打开数据存储文件,如果不存在则创建新文件
if (!file.Open("./studentfile.dat", CFile::modeRead | CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
pList->DeleteAllItems();//清屏
//重新写入ListCtrl
Student u;
int i = 0;
while (file.Read(&u, sizeof(u)) == sizeof(u))
{
u.Sum();
pList->InsertItem(i, u.name);
if (u.gender)
pList->SetItemText(i, 1, _T("男"));
else
pList->SetItemText(i, 1, _T("女"));
pList->SetItemText(i, 2, u.ID);
pList->SetItemText(i, 3, u.math);
pList->SetItemText(i, 4, u.program);
pList->SetItemText(i, 5, u.sum);
i++;
}
file.Close();
}
//导出数据
void Management::SaveToFile(CListCtrl* pList)
{
if (pList->GetItemCount() <= 0)
{
MessageBox(_T("没有需要保存的数据"), _T("警告"), MB_OK | MB_ICONWARNING);
return;
}
//调用系统另存为窗口
char szFilters[] = _T("txt文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
CFileDialog dlg(FALSE, _T("txt"), _T("学生信息"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilters, this);
if (dlg.DoModal() != IDOK)
return;
//获取文件路径
CString strFilePath;
strFilePath = dlg.GetPathName();
//判断文件是否已存在,存在则删除重建
DWORD dwre = GetFileAttributes(strFilePath);
if (dwre != (DWORD)-1)
{
DeleteFile(strFilePath);
}
//保存文件数据
FILE* fp;
fopen_s(&fp, strFilePath, "w");
//char str[1024];
if (fp == NULL)
{
printf("Save file ERROR\n");
return;
}
//获取ListCtrl中每一列的第一行,即数据的类别
int nHeadNum = pList->GetHeaderCtrl()->GetItemCount();
LVCOLUMN lvcol;
char str_out[256];
int nColNum;
nColNum = 0;
lvcol.mask = LVCF_TEXT;
lvcol.pszText = str_out;
lvcol.cchTextMax = 256;
while (pList->GetColumn(nColNum, &lvcol))
{
nColNum++;
fprintf_s(fp, "%-30s", lvcol.pszText);
}
fprintf_s(fp, "\n", lvcol.pszText);//写入文件中
//读取行的数据
int nRow = pList->GetItemCount();
for (int i = 0; i < nRow; i++)
{
for (int j = 0; j < nColNum; j++)
{
CString str_data = pList->GetItemText(i, j);//获取指定列,存为字符串形式
fprintf_s(fp, "%-30s", str_data);
}
fprintf_s(fp, "\n");
}
fclose(fp);
MessageBox(_T("数据保存成功"), _T("成功"), MB_OK | MB_ICONASTERISK);
}
//导入数据
void Management::GetFromFile(CListCtrl* pList)
{
// TODO: 在此添加控件通知处理程序代码
//调用系统窗口
char szFilters[] = _T("txt文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
CFileDialog dlg(TRUE, _T("txt"), _T("学生信息"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilters, this);
dlg.m_ofn.lpstrTitle = _T("导入数据");
if (dlg.DoModal() != IDOK)
return;
//获取文件路径
CString strFilePath;
strFilePath = dlg.GetPathName();
//判断文件是否存在
DWORD dwre = GetFileAttributes(strFilePath);
if (dwre != (DWORD)-1)//如果路径正确
{
//清除之前所有数据
pList->DeleteAllItems();
int nColumnCount = pList->GetHeaderCtrl()->GetItemCount();
for (int i = 0; i < nColumnCount; i++)
{
pList->DeleteColumn(0);
}
}
else
{
MessageBox(_T("文件不存在!"), _T("警告"), MB_OK | MB_ICONWARNING);
return;
}
//读取文件中的数据
FILE* fp;
fopen_s(&fp, strFilePath, "r");
char str[1024];
if (fp == NULL)
{
printf("Open file ERROR\n");
return;
}
int iRow = 0;
CFile file;
if (!file.Open("./studentfile.dat", /*CFile::modeCreate | */CFile::modeRead|CFile::modeWrite | CFile::shareDenyNone))
{
MessageBox(_T("无法打开文件!"), _T("错误"), MB_OK | MB_ICONERROR);
return;
}
Student u;
int nLength = 0;
while (fgets(str, sizeof(str), fp))
{
char _index[256] = "temp";
sprintf_s(_index, "%d", iRow);
pList->InsertItem(iRow, _index);
std::string s = str;
std::string buf;
std::stringstream ss(s);
std::vector<std::string> tokens;
int i = 0;
int j = 0;
while (ss >> buf)
{
if (buf.size() > 0)
{
tokens.push_back(buf);
if (iRow == 0)
{
pList->InsertColumn(++j, _T(tokens.at(i).c_str()), LVCFMT_CENTER, 100);
}
else
{
pList->SetItemText(iRow, i, tokens.at(i).c_str());
char temp[20];//临时储存性别,便于判断
strcpy_s(temp, tokens.at(i).c_str());
switch (i)
{
case 0:
strcpy_s(u.name, tokens.at(i).c_str());
break;
case 1:
if (strcmp("女", temp))
{
u.gender = true;
break;
}
else
{
u.gender = false;
break;
}
case 2:
strcpy_s(u.ID, tokens.at(i).c_str());
break;
case 3:
strcpy_s(u.math, tokens.at(i).c_str());
break;
case 4:
strcpy_s(u.program, tokens.at(i).c_str());
break;
case 5:
strcpy_s(u.sum, tokens.at(i).c_str());
break;
default:
break;
}
}
i++;
}
}
iRow++;
if (iRow > 1)
file.Write(&u, sizeof(u));
}
fclose(fp);
pList->DeleteItem(0);
file.Close();
LoadFile(pList);//更新ListCtrl界面
}