使用单链表进行排序见上一篇文章,这两篇文章相辅相承;
传送门:点我即达(。・ω・。)
****************************************************************************************************************************************
★首先附上一系列运行结果截图:
1:学生信息的初始化
2:排序功能列表
——使用了system("cls") 和 gotoxy() 等函数;
3:排序界面-姓名
4:排序界面-学号
5:排序界面-分数
*文件会被写到同级目录下的这四个文件中,下面代码中也可以分析得出。
★源代码:
★ 由于代码基于某些要求进行编写,可能有些复杂,但是基本原理仍然未变,只是多了些表示方法;
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// 学生排序(函数作为参数传入).cpp : 定义控制台应用程序的入口点。 // 需要使用文件进行输出 // 要求函数结构void student_sort(STUDENT *pStu, int n, int(*pFunc)(STUDENT &, STUDENT &)); #include <string.h> #include <iostream> //文件的操作 #include <fstream> //调用Sleep()/system()函数 #include <windows.h> using namespace std; typedef struct stu { char name[20]; int id; float score; }STUDENT; STUDENT * Init_Stu( int number) { ofstream out("学生信息.txt"); out << "姓名 学号 分数n"; STUDENT *pStu = (STUDENT *)malloc(sizeof(STUDENT) * number); //开辟number个空间 cout << "Please Enter the stu'infomation: " << endl; cout << "姓名 学号 分数" << endl;; for (int i = 0; i < number; i++) { cin >> (pStu + i)->name; cin >> (pStu + i)->id; cin >> (pStu + i)->score; } cout << "n开始写入文件" << endl; for (int i = 0; i < number; i++) { out << (pStu + i)->name << " "; out << (pStu + i)->id << " "; out << (pStu + i)->score << "n"; } out.close(); cout << "……" << endl; Sleep(1000); cout << "……" << endl; Sleep(1000); cout << "文件成功写入!" << endl; cout << "文件名为“Student_by_name.txt”" << endl << endl; system("pause"); return pStu; } int pFunc_name(STUDENT *x, STUDENT *y) { if (strcmp(x->name, y->name) < 0) { return 0; } else { return 1; } } int pFunc_id(STUDENT *x, STUDENT *y) { if (x->id > y->id) { return 1; } else { return 0; } } int pFunc_score(STUDENT *x, STUDENT *y) { if (x->score > y->score) { return 1; } else { return 0; } } void gotoxy(short x, short y) //自定义光标定位 { COORD pos = {x,y}; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); } void student_sort_name(STUDENT *pStu, int n, int (*pFunc)(STUDENT *x, STUDENT *y)) { STUDENT *p; p = pStu; STUDENT *temp; temp = (STUDENT*)malloc(sizeof(STUDENT)); int k,j; //循环使用 for (int i = 1; i < n; i++) { p = pStu; for (j = n - 1, k = 0; j >= i; j--,k++) { if (pFunc_name((p+k), (p + k+1))) { strcpy(temp->name, (p + k)->name); strcpy((p + k)->name, (p + k+1)->name); strcpy((p + k + 1)->name,temp->name); p = pStu; temp->id = (p + k)->id; (p + k)->id = (p + k + 1)->id; (p + k + 1)->id = temp->id; p = pStu; temp->score = (p + k)->score; (p + k)->score = (p + k + 1)->score; (p + k + 1)->score = temp->score; } } } cout << "是否要将排序结果写入文件?(文件名为“Student_by_name.txt”)" << endl; for (int i = 0; i < n; i++) { cout << (pStu + i)->name << " "; cout << (pStu + i)->id << " "; cout << (pStu + i)->score << "n"; } cout << "是否要将排序结果写入文件?(文件名为“Student_by_sno.txt”)" << endl; if (getchar() == 'y') { ofstream out("Student_by_name.txt"); cout << "n开始写入文件" << endl; for (int i = 0; i < n; i++) { out << (pStu + i)->name << " "; out << (pStu + i)->id << " "; out << (pStu + i)->score << "n"; } out.close(); cout << "……" << endl; Sleep(1000); cout << "……" << endl; Sleep(1000); cout << "文件成功写入!" << endl << endl; } else { cout << "感谢使用,Bye~" << endl; } } void student_sort_id(STUDENT *pStu, int n, int(*pFunc)(STUDENT *x, STUDENT *y)) { STUDENT *p; p = pStu; STUDENT *temp; temp = (STUDENT*)malloc(sizeof(STUDENT)); int k, j; //循环使用 for (int i = 1; i < n; i++) { p = pStu; for (j = n - 1, k = 0; j >= i; j--, k++) { if (pFunc_id((p + k), (p + k + 1))) { strcpy(temp->name, (p + k)->name); strcpy((p + k)->name, (p + k + 1)->name); strcpy((p + k + 1)->name, temp->name); p = pStu; temp->id = (p + k)->id; (p + k)->id = (p + k + 1)->id; (p + k + 1)->id = temp->id; p = pStu; temp->score = (p + k)->score; (p + k)->score = (p + k + 1)->score; (p + k + 1)->score = temp->score; } } } for (int i = 0; i < n; i++) { cout << (pStu + i)->name << " "; cout << (pStu + i)->id << " "; cout << (pStu + i)->score << "n"; } cout << "是否要将排序结果写入文件?(文件名为“Student_by_sno.txt”)" << endl; if (getchar() == 'y') { ofstream out ("Student_by_sno.txt"); cout << "n开始写入文件" << endl; for (int i = 0; i < n; i++) { out << (pStu + i)->name << " "; out << (pStu + i)->id << " "; out << (pStu + i)->score << "n"; } out.close(); cout << "……" << endl; Sleep(1000); cout << "……" << endl; Sleep(1000); cout << "文件成功写入!" << endl << endl; } else { cout << "感谢使用,Bye~" << endl; } } void student_sort_score(STUDENT *pStu, int n, int(*pFunc)(STUDENT *x, STUDENT *y)) { STUDENT *p; p = pStu; STUDENT *temp; temp = (STUDENT*)malloc(sizeof(STUDENT)); int k, j; //循环使用 for (int i = 1; i < n; i++) { p = pStu; for (j = n - 1, k = 0; j >= i; j--, k++) { if (pFunc_score((p + k), (p + k + 1))) { strcpy(temp->name, (p + k)->name); strcpy((p + k)->name, (p + k + 1)->name); strcpy((p + k + 1)->name, temp->name); p = pStu; temp->id = (p + k)->id; (p + k)->id = (p + k + 1)->id; (p + k + 1)->id = temp->id; p = pStu; temp->score = (p + k)->score; (p + k)->score = (p + k + 1)->score; (p + k + 1)->score = temp->score; } } } cout << "是否要将排序结果写入文件?(文件名为“Student_by_score.txt”)" << endl; for (int i = 0; i < n; i++) { cout << (pStu + i)->name << " "; cout << (pStu + i)->id << " "; cout << (pStu + i)->score << "n"; } cout << "是否要将排序结果写入文件?(文件名为“Student_by_sno.txt”)" << endl; if (getchar() == 'y') { ofstream out("Student_by_score.txt"); cout << "n开始写入文件" << endl; for (int i = 0; i < n; i++) { out << (pStu + i)->name << " "; out << (pStu + i)->id << " "; out << (pStu + i)->score << "n"; } out.close(); cout << "……" << endl; Sleep(1000); cout << "……" << endl; Sleep(1000); cout << "文件成功写入!" << endl << endl; } else { cout << "感谢使用,Bye~" << endl; } } void run(STUDENT *pStu,int number) { system("title 学生信息排序"); gotoxy(35, 2); cout << "★功能列表★"; gotoxy(30, 3); cout << "1: 按照学生姓名排序"; gotoxy(30, 4); cout << "2:按照学生学号排序"; gotoxy(30, 5); cout << "3:按照学生分数排序"; gotoxy(0, 12); int choice = 0; cout << "★请输入您要执行的功能:"; cin >> choice; getchar(); //吸收回车 switch (choice) { case 1: student_sort_name(pStu, number, pFunc_name); break; case 2: student_sort_id(pStu, number, pFunc_id); break; case 3: student_sort_score(pStu, number, pFunc_score); break; default: cout << "选择失败,感谢使用,再见!" << endl << endl; } } int main() { cout << "***学生信息的初始化***" << endl; STUDENT * pStu; int number; cout << "How many people? " << endl; cin >> number; pStu = Init_Stu(number); system("cls"); run(pStu,number); return 0; }
*附加:
其实上面的运行结果排序的那几张图片还是有一些问题的,细心观察就会发现,好好我及时发现了这些问题,并在代码中进行了相应的修改。
问题来源:
交换的只是结构体中指定的两个数据,而不能保证在该结构体的其他数据一并交换,故我在每一段交换的代码中将其他数据也一并进行了交换。
(运行结果图片并未做修改~)
☆仅仅记录日常编写代码 与 疑问(`・ω・´)
****************************************************************************************************************************************
最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。
****************************************************************************************************************************************
最后
以上就是爱撒娇砖头最近收集整理的关于☆ C/C++中使用结构体数组->排序(姓名+学号+分数)的全部内容,更多相关☆内容请搜索靠谱客的其他文章。
发表评论 取消回复