0%

学生信息管理系统

C++课程作业

非文件操作:

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
#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
typedef long long ll;
using namespace std;
int K=0;
class STU
{
private:
ll Num;
char Name[10];
char Sex;
double Score;
bool flag;
public:
void TITLE_STU();
void ADD_STU();
void CHANGE_STU();
void DEL_STU();
void SHOW_STU();
void SORT_STU();
void MAX_STU();
friend bool CMP(STU,STU);
STU()
{
flag=0;
}
~STU(){}
}Boy[1000];
bool CMP(STU a,STU b)
{
if (a.Score == b.Score)
return a.Num<b.Num;
return a.Score>b.Score;
}
void STU::TITLE_STU()
{
cout<<"\t1:\tADD A STUDENT"<<endl;
cout<<"\t2:\tCHANGE STUDENTS\'S INFORMATION"<<endl;
cout<<"\t3:\tDELETE A STUDENT"<<endl;
cout<<"\t4:\tSHOW STUDENTS INFORMATION"<<endl;
cout<<"\t5:\tSORT BY SCORE"<<endl;
cout<<"\t6:\tSHOW THE MAX SCORE\' STUDENT"<<endl;
cout<<"\t0:\tEXIT THE STUDENTS INFORMATION MANAGE SYSTEM"<<endl;
cout<<endl<<"\tPLEASE ENTER SELECTED CHOICES FROM ABOVE LISTS"<<endl;
}
void STU::ADD_STU()
{
cout<<"请输入要添加的学生人数"<<endl;
int T;cin>>T;
cout<<"请输入"<<T<<"行学生的学号,姓名,性别,成绩"<<endl;
for(int i=0;i<T;i++,K++)
{
cin>>Boy[K].Num>>Boy[K].Name>>Boy[K].Sex>>Boy[K].Score;
Boy[K].flag=0;
}
cout<<endl<<"学生信息导入成功"<<endl;
}
void STU::CHANGE_STU()
{
cout<<"请输入要修改的学生学号,输入0停止删除"<<endl;
ll num;
while(cin>>num)
{
int flag=0;
for(int i=0;i<K;i++)
{
if(num==Boy[i].Num)
{
cout<<"请再次输入学生的学号,姓名,性别,成绩"<<endl;
cin>>Boy[i].Num>>Boy[i].Name>>Boy[i].Sex>>Boy[i].Score;
Boy[i].flag=0;
flag=1;
cout<<"学生信息修改成功"<<endl;
}
}
if(num==0)break;
if(flag==0)cout<<"NO SUCH STUDENTS"<<endl;
}
}
void STU::DEL_STU()
{
cout<<"请输入要删除的学生学号,输入0停止删除"<<endl;
ll num;
while(cin>>num)
{
int flag=0;
for(int i=0;i<K;i++)
{
if(num==Boy[i].Num)
{
Boy[i].flag=1;
flag=1;
cout<<"DELETE SUCCESS!"<<endl;
break;
}
}
if(num==0)break;
if(flag==0)cout<<"NO SUCH STUDENTS"<<endl;
}
}
void STU::SHOW_STU()
{
cout<<endl<<"\tThe Lists Of Students Are Here"<<endl;
cout<<"-----------------------------------------------"<<endl;
cout<<" 学号\t姓名\t\t性别\t成绩"<<endl;
cout<<"-----------------------------------------------"<<endl;
cout.setf(std::ios::left);
for(int i=0;i<K;i++)
{
if(Boy[i].flag==0)
{
cout<<Boy[i].Num<<'\t';
cout<<setw(8)<<Boy[i].Name;
cout<<"\t "<<Boy[i].Sex<<'\t';
cout<<setiosflags(ios::fixed)<<setprecision(2)<<Boy[i].Score<<endl;
}
}
cout<<"-----------------------------------------------"<<endl;
}
void STU::SORT_STU()
{
sort(Boy,Boy+K,CMP);
cout<<"学生信息排序成功"<<endl;
}
void STU::MAX_STU()
{
int Maxn=0,Flag=0;
for(int i=0;i<K;i++)
{
if(Boy[i].Score>=Boy[Maxn].Score)
{
Maxn=i;
Flag=1;
}
}
if(Flag==0)cout<<"NO STUDENT"<<endl;
else
{
cout<<"-----------------------------------------------"<<endl;
cout<<" 学号\t姓名\t\t性别\t成绩"<<endl;
cout<<"-----------------------------------------------"<<endl;
cout.setf(std::ios::left);
cout<<Boy[Maxn].Num<<'\t';
cout<<setw(8)<<Boy[Maxn].Name;
cout<<"\t "<<Boy[Maxn].Sex<<'\t';
cout<<setiosflags(ios::fixed)<<setprecision(2)<<Boy[Maxn].Score<<endl;
cout<<"-----------------------------------------------"<<endl;
}
}
int main()
{
STU S;
S.TITLE_STU();
int choice;
while(cin>>choice)
{
if(choice==1)S.ADD_STU();
else if(choice==2)S.CHANGE_STU();
else if(choice==3)S.DEL_STU();
else if(choice==4)S.SHOW_STU();
else if(choice==5)S.SORT_STU();
else if(choice==6)S.MAX_STU();
else if(choice==0)break;
system("PAUSE&CLS");
S.TITLE_STU();
}
return 0;
}

文件操作:

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <algorithm>
typedef long long ll;
using namespace std;
int K = 0;
class STU
{
private:
ll Num;
char Name[10];
char Sex;
double Score;
bool flag;
public:
void READ_STU();
void WRITE_STU();
void TITLE_STU();
void ADD_STU();
void CHANGE_STU();
void DEL_STU();
void SHOW_STU();
void SORT_STU();
void MAX_STU();
friend bool CMP1(STU, STU);
friend bool CMP2(STU, STU);
friend bool CMP3(STU, STU);
STU()
{
flag = 0;
}
~STU() {}
}Boy[1000];
void STU::READ_STU()
{
FILE *fp = fopen("STU.dat", "r");
while (fscanf(fp, "%lld %s %c %lf", &Boy[K].Num, &Boy[K].Name, &Boy[K].Sex, &Boy[K].Score) != EOF)
{
Boy[K].flag = 0;
K++;
}
fclose(fp);
}
void STU::WRITE_STU()
{
FILE *fp = fopen("STU.dat", "w");
for (int i = 0; i < K; i++)
{
if(Boy[i].flag==0)
fprintf(fp, "%lld %s %c %lf\n", Boy[i].Num, Boy[i].Name, Boy[i].Sex, Boy[i].Score);
}
fclose(fp);
}
bool CMP1(STU a, STU b)
{
return a.Num<b.Num;
}
bool CMP2(STU a, STU b)
{
if (strcmp(a.Name,b.Name)==0)
return a.Num<b.Num;
return strcmp(a.Name,b.Name)<0;
}
bool CMP3(STU a, STU b)
{
if (a.Score == b.Score)
return a.Num<b.Num;
return a.Score>b.Score;
}
void STU::TITLE_STU()
{
cout << "\t1:\tADD A STUDENT" << endl;
cout << "\t2:\tCHANGE STUDENTS\'S INFORMATION" << endl;
cout << "\t3:\tDELETE A STUDENT" << endl;
cout << "\t4:\tSHOW STUDENTS INFORMATION" << endl;
cout << "\t5:\tSORT THE STUDENTS" << endl;
cout << "\t6:\tSHOW THE MAX SCORE\' STUDENT" << endl;
cout << "\t0:\tEXIT THE STUDENTS INFORMATION MANAGE SYSTEM" << endl;
cout << endl << "\tPLEASE ENTER SELECTED CHOICES FROM ABOVE LISTS" << endl;
}
void STU::ADD_STU()
{
cout << "请输入要添加的学生人数" << endl;
int T; cin >> T;
cout << "请输入" << T << "行学生的学号,姓名,性别,成绩" << endl;
for (int i = 0; i<T; i++, K++)
{
cin >> Boy[K].Num >> Boy[K].Name >> Boy[K].Sex >> Boy[K].Score;
Boy[K].flag = 0;
}
cout << endl << "学生信息导入成功" << endl;
}
void STU::CHANGE_STU()
{
cout << "请输入要修改的学生学号,输入0停止删除" << endl;
ll num;
while (cin >> num)
{
int flag = 0;
for (int i = 0; i<K; i++)
{
if (num == Boy[i].Num)
{
cout << "请再次输入学生的学号,姓名,性别,成绩" << endl;
cin >> Boy[i].Num >> Boy[i].Name >> Boy[i].Sex >> Boy[i].Score;
Boy[i].flag = 0;
flag = 1;
cout << "学生信息修改成功" << endl;
}
}
if (num == 0)break;
if (flag == 0)cout << "NO SUCH STUDENTS" << endl;
}
}
void STU::DEL_STU()
{
cout << "请输入要删除的学生学号,输入0停止删除" << endl;
ll num;
while (cin >> num)
{
int flag = 0;
for (int i = 0; i<K; i++)
{
if (num == Boy[i].Num)
{
Boy[i].flag = 1;
flag = 1;
cout << "DELETE SUCCESS!" << endl;
break;
}
}
if (num == 0)break;
if (flag == 0)cout << "NO SUCH STUDENTS" << endl;
}
}
void STU::SHOW_STU()
{
cout << endl << "\tThe Lists Of Students Are Here" << endl;
cout << "-----------------------------------------------" << endl;
cout << " 学号\t姓名\t\t性别\t成绩" << endl;
cout << "-----------------------------------------------" << endl;
cout.setf(std::ios::left);
for (int i = 0; i<K; i++)
{
if (Boy[i].flag == 0)
{
cout << Boy[i].Num << '\t';
cout << setw(8) << Boy[i].Name;
cout << "\t " << Boy[i].Sex << '\t';
cout << setiosflags(ios::fixed) << setprecision(2) << Boy[i].Score << endl;
}
}
cout << "-----------------------------------------------" << endl;
}
void STU::SORT_STU()
{
int x=0;
cout << "1.按学号排序" << endl;
cout << "2.按姓名排序" << endl;
cout << "3.按成绩排序" << endl << endl;;
cout << "请选择排序规则!" << endl;
cin >> x;
if(x==1)sort(Boy, Boy + K, CMP1);
else if(x==2)sort(Boy, Boy + K, CMP2);
else if (x == 3)sort(Boy, Boy + K, CMP3);
else cout << "输入错误,排序失败" << endl << endl;;
if (x == 1 || x == 2 || x == 3)cout << "学生信息排序成功" << endl << endl;
}
void STU::MAX_STU()
{
int Maxn = 0, Flag = 0;
for (int i = 0; i<K; i++)
{
if (Boy[i].Score >= Boy[Maxn].Score)
{
Maxn = i;
Flag = 1;
}
}
if (Flag == 0)cout << "NO STUDENT" << endl;
else
{
cout << "-----------------------------------------------" << endl;
cout << " 学号\t姓名\t\t性别\t成绩" << endl;
cout << "-----------------------------------------------" << endl;
cout.setf(std::ios::left);
cout << Boy[Maxn].Num << '\t';
cout << setw(8) << Boy[Maxn].Name;
cout << "\t " << Boy[Maxn].Sex << '\t';
cout << setiosflags(ios::fixed) << setprecision(2) << Boy[Maxn].Score << endl;
cout << "-----------------------------------------------" << endl;
}
}
int main()
{
STU S;
S.TITLE_STU();
S.READ_STU();
int choice;
while (cin >> choice)
{
if (choice == 1)S.ADD_STU();
else if (choice == 2)S.CHANGE_STU();
else if (choice == 3)S.DEL_STU();
else if (choice == 4)S.SHOW_STU();
else if (choice == 5)S.SORT_STU();
else if (choice == 6)S.MAX_STU();
else if (choice == 0)
{
S.WRITE_STU();
break;
}
system("PAUSE&CLS");
S.TITLE_STU();
}
return 0;
}

赏点呗!