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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
#include <iostream>
#include<fstream>
#include<string>
#define line for (int n = 0; n <= 100; n++) cout << "-"
#define FILENAME "stdu.dat"
using namespace std;
class student { //学生类
public:
int Is_Exist(string stuId, int a);
int get_student_number(); //获取文件中学生人数
bool File_Is_Empty; //文件是否为空的标识
student *studentArray; //将文件中的内容,以student对象方式储存在studentArray[]数组中
int student_number; //学生人数
string studentId; //学号
string name; //姓名
float score[3];//成绩*3
float Total; // 总分
int Average;//平均分
void sort(int n=1); //排序函数
void delete_stu(); //删除学生
void init(); //初始化内容,将文件中的内容读到studentArray中
void save(); //保存文件
void show();//展示界面
void add_stu(int number = 1); //添加学生
void showInfo(); //展示学生信息
void search();//搜索学生
student() //默认构造函数,判断文件是否为空,设置File_Is_Empty值
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
this->student_number = 0;
this->studentArray = NULL;
this->studentId = "0";
this->name = "0";
this->score[0] = 0;
this->Average = 0;
this->Total = 0;
this->File_Is_Empty = true;
ifs.close();
return;
}
char c;
ifs >> c;
if (ifs.eof())
{
//文件空
this->student_number = 0;
this->studentArray = NULL;
this->studentId = "0";
this->name = "0";
this->score[0] = 0;
this->Average = 0;
this->Total = 0;
this->File_Is_Empty = true;
ifs.close();
return;
}
int num = this->get_student_number(); //获取文件中学生数量
this->student_number = num;
//cout << "现在学生人数为:" << num << endl;;
//system("pause");
};
~student() //析构函数
{
delete[]this->studentArray;
this->studentArray = NULL; //防止指针变为野指针
}
student(string stuId,string stuName, float stuScore[3]) //带参数的构造函数
{
this->studentId = stuId;
this->name = stuName;
for (int i = 0; i < 3; i++)
this->score[i] = stuScore[i];
this->Average = (stuScore[0] + stuScore[1] + stuScore[2]) / 3;
this->Total = stuScore[0] + stuScore[1] + stuScore[2];
};
};
void student::sort(int n) //排序,当n=1的时候为按学号排序,n=2为按总成绩排序
{
int num1; //学号在设计的时候是string类型,用int类型排序需要atoi,用num接收转换过的值
int num2; //
student a; //用于交换对象数组的中间变量
if (this->File_Is_Empty) //判断文件是否为空
{
cout << "数据文件不存在或者为空\n";
system("pause");
}
else
{
cout << "3.学生信息排序\n";
if (n == 1)
{
cout << "将学生信息按学号顺序排序\n";
for (int i = 0; i < this->student_number - 1; i++)//冒泡排序
{
for (int t = 0; t < this->student_number - 1 - i; t++)
{
num1 = atoi(this->studentArray[t].studentId.c_str()); //将string类型变量变为int类型
num2 = atoi(this->studentArray[t + 1].studentId.c_str());
if (num1 > num2)
a = this->studentArray[t + 1], this->studentArray[t + 1] = this->studentArray[t], this->studentArray[t] = a;
}
}
}
if (n == 2)
{
cout << "将学生信息按总成绩顺序排序\n";
for (int i = 0; i < this->student_number - 1; i++)//冒泡排序
{
for (int t = 0; t < this->student_number - 1 - i; t++)
{
if (this->studentArray[t].Total > this->studentArray[t + 1].Total)
a = this->studentArray[t + 1], this->studentArray[t + 1] = this->studentArray[t], this->studentArray[t] = a;
}
}
}
}
}
void student::search() //查找学生成绩
{
string id;
int ret; //studentArray[]的下标,在Is_Exist()中
int a = 1; //判断根据学号查找还是姓名查找
cout << "4.查找学生成绩\n" << "1.按照学号查找\t2.根据姓名查找\n输入你的选项>";
cin >> a;
if (a == 1)
{
cout << "请输入学号:", cin >> id;
ret = this->Is_Exist(id, 1);
}
else if (a == 2)
{
cout << "请输入姓名:", cin >> id;
ret = this->Is_Exist(id, 2);
}
else
{
ret = this->Is_Exist(id, 1);
}
if (ret == -1)
{
system("cls");
cout << "学生不存在" << endl;
}
else
{
cout << "查找成功,下面为该学生信息\n" << endl;
cout << "学号:" << this->studentArray[ret].studentId
<< " 姓名:" << this->studentArray[ret].name << endl
<< "各门成绩:语文:" << this->studentArray[ret].score[0]
<< " 数学:" << this->studentArray[ret].score[0]
<< " 英语:" << this->studentArray[ret].score[2]
<< " 平均成绩:" << this->studentArray[ret].Average
<< " 总成绩:" << this->studentArray[ret].Total << endl;
}
system("pause");
}
int student::Is_Exist(string stuId,int a)
{
//a=1 则使用学生学号查找
//a=2 则使用学生姓名查找
if (File_Is_Empty) //判断文件是否不存在或者为空,如果为空则返回-2
{
cout << "数据文件不存在或者为空\n";
system("pause");
return -2;
}
for (int i = 0; i < this->student_number; i++)
{
if ((a==1)&&(this->studentArray[i].studentId == stuId)) //根据学号查找
{
return i;
}
if ((a == 2) && (this->studentArray[i].name == stuId)) //根据姓名查找
{
return i;
}
}
return -1; //如果没找到返回-1
}
void student::delete_stu() //删除学生
{
int ret; //接收 Is_Exist的返回值,返回值是此学生在studentArray[]里的下标
string id; //可以用来接收学号或者姓名
int a = 1; //判断是按照学生学号寻找还是根据学生姓名寻找
cout << "5.删除学生成绩\n"
<< "1.按照学号删除\t2.根据姓名删除\n输入你的选项>";
cin >> a;
if (a == 1) //按照学生学号查找
{
cout << "请输入学号:", cin >> id;
ret = this->Is_Exist(id, 1);
}
else if(a==2)//按照学生姓名
{
cout << "请输入姓名:", cin >> id;
ret = this->Is_Exist(id, 2);
}
else //默认按照学生学号进行查找
{
cout << "请输入学号:", cin >> id;
ret = this->Is_Exist(id, 1);
}
if (ret == -1) //返回值为-1则学生不存在
{
system("cls");
cout << "学生不存在"<<endl;
}
else if(ret == -2) //返回值为-2文件不存在或为空,直接退出函数
{
return;
}
else //如果学生信息存在,并且ret是studentArray[]中的下标
{//输出要删除的学生信息
cout << "该学生的信息将被删除"<<endl;
cout << "学号:" << this->studentArray[ret].studentId
<< " 姓名:" << this->studentArray[ret].name << endl
<< "各门成绩:语文:" << this->studentArray[ret].score[0]
<< " 数学:" << this->studentArray[ret].score[0]
<< " 英语:" << this->studentArray[ret].score[2]
<< " 平均成绩:" << this->studentArray[ret].Average
<< " 总成绩:" << this->studentArray[ret].Total << endl;
for (int i = ret; i < this->student_number - 1; i++)
{
this->studentArray[i] = this->studentArray[i + 1];
}
this->student_number--;
this->save();
}
system("pause");
}
void student::init()
{
string name; //用来储存读取到的姓名
string student_id;//用来储存读取到的学号
int i = 0; //标识符,用来把实例化的类储存到对应的对象数组
float score[3]; //用来储存读取到的成绩
float total; //用来储存读取到的成总分
int average; //用来储存读取到的平均分
ifstream inf; //实例化一个文件对象
this->studentArray = new student[this->student_number]; //根据读取到的学生数量开辟空间,学生数量
inf.open(FILENAME, ios::in);
while (inf >> student_id && inf >> name && inf >> score[0]&& inf >> score[1] && inf >> score[2] && inf >> average && inf >> total)
{ //格式化读取文件,从文件读到变量
student stu(student_id, name, score); //使用读到的值实例化student对象
this->studentArray[i] = stu; //把实例化的对象放到stu1对象的studentArray中
i++;
}
}
int student::get_student_number() //获取文件中学生的数量,在add_stu()函数中加上需要增加的学生数量,为最新需要开辟的空间的大小
{
string name;
string student_id;
int num=0; //每格式化读取一块数据,则加1人数
float score[3];
float total;
int average;
ifstream inf;
inf.open(FILENAME, ios::in);
while (inf >> student_id && inf >> name && inf >> score[0] && inf >> score[1] && inf >> score[2] && inf >> average && inf >> total)
{
num++;
}
inf.close();
return num; //返回学生数量
}
void student::save() //将stu1中studentArray中的每个student对象储存到文件中
{
ofstream ofs;
ofs.open(FILENAME,ios::out);
for (int i = 0; i < this->student_number; i++)
{
ofs << this->studentArray[i].studentId << " "
<< this->studentArray[i].name << " "
<< this->studentArray[i].score[0] << " "
<< this->studentArray[i].score[1] << " "
<< this->studentArray[i].score[2]<< " "
<<this->studentArray[i].Average<<" "<< this->studentArray->Total<<" ";
}
}
void student::showInfo() //显示学生信息
{
if (this->File_Is_Empty) //构造函数里的文件标识符,判断文件是否存在或者为空,每实例化一个对象都检查一遍
{
system("cls");
cout << "文件不存在或内容为空,请先输入数据\n";
}
else
{ //文件不为空则输出stu1中studentArray中储存的内容
for (int i=0;i<this->student_number;i++)
{
cout << "学号:" << this->studentArray[i].studentId
<< " 姓名:" << this->studentArray[i].name << endl
<< "各门成绩:语文:" << this->studentArray[i].score[0]
<< " 数学:" << this->studentArray[i].score[1]
<< " 英语:" << this->studentArray[i].score[2]
<< " 平均成绩:" << this->studentArray[i].Average
<< " 总成绩:" << this->studentArray[i].Total << endl;
line;
cout << endl;
}
}
system("pause");
}
void student::add_stu(int add_number) //添加学生
{
//计算需要的新的空间大小
int newsize = this->student_number + add_number; //需要开辟的空间 = 文件中学生的人数+需要添加的数量
//cout << "newxize=" << newsize;
student *newspace = new student[newsize]; //开辟内存空间
student *stu3;
string name;
string studentId;
float score[3];
if (this->studentArray!= NULL) //如果studentArray不为空就先将studentArray中的内容先复制到newspace数组中,再在newspace中增加学生信息,
//最后再将newspace的内容复制到studentArray中,实现动态数组
{
for (int i = 0; i < this->student_number; i++)
{
newspace[i] = this->studentArray[i];
}
}
for (int i = 0; i < add_number; i++) //输入学生信息
{
cout << "请输入学生姓名:", cin >> name, cout << endl;
cout << "请输入学生学号:", cin >> studentId, cout << endl;
cout << "请输入学生语文成绩:", cin >> score[0], cout << endl;
cout << "请输入学生数学成绩:", cin >> score[1], cout << endl;
cout << "请输入学生英语成绩:", cin >> score[2], cout << endl;
line;
cout << endl;
student stu3(studentId, name, score); //将输入的内容实例化成对象
newspace[this->student_number + i] = stu3; //将对象依次储存到newspace中
}
delete[] this->studentArray;
this->studentArray = newspace; //将newspace赋给studentArray,用来在别的成员函数中访问
this->student_number = newsize; //更新学生人数大小
this->File_Is_Empty = false; // 输入了内容以后,文件不为空
this->save(); //保存到文件中
cout << "添加成功"<<endl;
system("pause");
}
void student::show()
{
cout << "1.输入学生成绩" << endl << "2.增加学生成绩" << endl << "3.学生信息排序"
<< endl << "4.查找学生成绩" << endl << "5.删除学生成绩" << endl << "6.显示学生成绩"
<< endl << "7.安全退出系统" << endl;
line;
cout << endl;
cout<< "输入你的选择>:";
}
student stu1; //实例化对象,此时已经得到文件中的学生数量
int main()
{
int choice=7;
stu1.init(); // 将文件里的内容按照格式读入内存,储存到
// cout << "stu1里的人数" << stu1.student_number;
// system("pause");
while (true)
{
system("cls");
stu1.show();
cin >> choice;
line;
cout << endl;
if (cin.good() && choice <= 7 && choice >= 1) //判断用户输入是否合法
{
switch (choice)
{
case 1:
//添加学生功能,默认参数为1,用来增加学生时直接调用,初始化添加十个学生时传递参数10
stu1.add_stu(10);
break;
case 2:
//增加学生,要求按照学号顺序插入,则在增加学生后调用排序函数,再进行保存
{
stu1.add_stu();
stu1.sort();
stu1.save();
}
break;
case 3:
//排序功能
{
int n = 1;
cout << "1.按照学号排序\t2.按照总成绩排序\n";
cin >> n;
stu1.sort(n);
stu1.showInfo();
}
break;
case 4:
//搜索功能
stu1.search();
break;
case 5:
//删除功能
stu1.delete_stu();
break;
case 6:
//显示学生信息
stu1.showInfo();
break;
case 7:
//退出程序
return 0;
break;
default:
system("cls");
break;
}
}
else
{ //归位cin标识符,不至于死循环
cin.clear();
cin.ignore();
cout << "非法数据"<<endl;
system("pause");
}
}
}
|