1 #include<iostream>
2 #include<iomanip>
3
4 using namespace std;
5
6
7 typedef struct _DATA_
8 {
9 int iAge;
10 int iGrade;
11 char szName[20];
12 }Data,*pData;
13
14
15
16 typedef struct _NODE_
17 {
18 Data DataTemp;
19 _NODE_* pNext;
20
21 }Node,*pNode;
22
23
24 class CList
25 {
26 public:
27 CList()
28 {
29 m_pHead = m_pTail = NULL;
30
31 m_iNodeCount = 0;
32 }
33 ~CList()
34 {
35
36 }
37
38 pNode CreateNode(Data DataTemp, CList& CFreeListObj);
39 void LinkNode(pNode pNodeTemp);
40
41 void RemoveNode(pNode pNodeTemp,CList& CFreeListObj);
42
43 int GetNodeCount();
44
45 bool RecycleNode(pNode pNodeTemp);
46
47 pNode AllocateNode();
48
49 void InitList()
50 {
51 m_pHead = m_pTail = NULL;
52 m_iNodeCount = 0;
53
54 }
55
56 bool DestroyList();
57 void TravelList()
58 {
59 pNode pNodeTemp = m_pHead;
60 while(pNodeTemp != NULL)
61 {
62 cout<<"Name: "<<setw(12)
63 <<pNodeTemp->DataTemp.szName<<" "
64 <<"Age: "<<pNodeTemp->DataTemp.iAge<<endl;
65 pNodeTemp = pNodeTemp->pNext;
66
67 }
68 }
69
70 /* void operator = (CList& CListTemp)
71 {
72 this->m_pHead = CListTemp.m_pHead;
73 this->m_pTail = CListTemp.m_pTail;
74 this->m_iNodeCount = CListTemp.m_iNodeCount;
75 }
76 */
77 friend class CFile;
78
79 private:
80 pNode m_pHead;
81 pNode m_pTail;
82
83 int m_iNodeCount;
84 };
85
86
87
88 class CFile :public CList
89 {
90 public:
91
92 FILE* m_fp;
93 char m_szFileRoute[256];
94
95 CFile()
96 {
97 m_fp = NULL;
98
99 memset(m_szFileRoute,0,sizeof(char)*256);
100 }
101
102 int GetFileLen();
103
104 pNode GetHead()
105 {
106 return m_pHead;
107 }
108
109
110 void GetFileRoute(char* szRoute);
111
112 void FileRead(char* szRoute,CList& CFreeListObj);
113
114 void FileWrite(char* szRoute,CList& CFreeListObj);
115
116 protected:
117 private:
118 };
119
120 int CFile::GetFileLen()
121 {
122
123 int nLen = 0;
124
125 fseek(m_fp,0L,SEEK_END);
126 nLen = ftell(m_fp); // 获得文件大小
127 rewind(m_fp); //再将文件流指针定位开头
128
129 return nLen;
130 }
131
132 void CFile::GetFileRoute(char* szRoute)
133 {
134 strcpy(m_szFileRoute,szRoute);
135 }
136
137
138 void CFile::FileRead(char* szRoute,CList& CFreeListObj)
139 {
140 GetFileRoute(szRoute);
141
142 m_fp = fopen(m_szFileRoute,"r");
143
144 if (m_fp==NULL)
145 {
146 m_fp = fopen(m_szFileRoute,"w");
147
148 fclose(m_fp);
149
150
151 return;
152
153
154 }
155 else // 代表文件存在
156 {
157
158 int nLen = GetFileLen();
159
160 if (nLen==0) // 文件中没有数据
161 {
162
163 fclose(m_fp);
164
165 return;
166 }
167
168 else // 文件有数据
169 {
170
171
172 //读
173
174 int i = 0;
175
176 Data DataTemp = {0};
177 pNode pNodeTemp = NULL;
178 for (i=0;i<nLen/sizeof(Data);i++)
179 {
180
181 fread(&DataTemp,sizeof(Data),1,m_fp);
182
183 pNodeTemp = CreateNode(DataTemp,CFreeListObj);
184
185 LinkNode(pNodeTemp);
186
187 }
188
189 fclose(m_fp);
190
191 return;
192
193 }
194
195 }
196 }
197
198 void CFile::FileWrite(char* szRoute,CList& CFreeListObj)
199 {
200
201 GetFileRoute(szRoute);
202 m_fp = fopen(m_szFileRoute,"w");
203 pNode pNodeHead = GetHead();
204 pNode pNodeTemp = pNodeHead;
205
206
207 while (pNodeTemp!=NULL)
208 {
209 fwrite(&(pNodeTemp->DataTemp),sizeof(Data),1,m_fp);
210
211 pNodeTemp = pNodeTemp->pNext;
212 }
213
214
215 fclose(m_fp);
216 }
217
218
219
220
221 pNode CList::CreateNode(Data DataTemp, CList& CFreeListObj)
222 {
223 pNode pNodeTemp = NULL;
224
225 int iNodeCount = CFreeListObj.GetNodeCount();
226
227 if(iNodeCount > 0)
228 {
229 pNodeTemp = CFreeListObj.AllocateNode();
230 cout<<"FreeList."<<endl;
231
232 }
233 else
234 {
235 pNodeTemp = new Node;
236 // cout<<"New."<<endl;
237 }
238
239
240 if(pNodeTemp != NULL)
241 {
242 pNodeTemp->DataTemp = DataTemp;
243 pNodeTemp->pNext = NULL;
244
245
246 m_iNodeCount++;
247
248 return pNodeTemp;
249 }
250 else
251 {
252 return NULL;
253 }
254 }
255
256
257 void CList::LinkNode(pNode pNodeTemp)
258 {
259
260 if(m_pHead == NULL)
261 {
262 m_pHead = m_pTail = pNodeTemp;
263 }
264 else
265 {
266 m_pTail->pNext = pNodeTemp;
267 m_pTail = pNodeTemp;
268 }
269 }
270
271 void CList::RemoveNode(pNode pNodeTemp,CList& CFreeListObj)
272 {
273 if(pNodeTemp == m_pHead)
274 {
275 m_pHead = m_pHead->pNext;
276 }
277
278 else if(pNodeTemp==m_pTail)
279 {
280 pNode pNodePre = m_pHead;
281 while(pNodePre->pNext!=pNodeTemp)
282 {
283 pNodePre = pNodePre->pNext;
284 }
285
286 pNodePre->pNext = NULL;
287 m_pTail = pNodePre;
288 }
289
290 else
291 {
292 pNode pNodePre = m_pHead;
293 while(pNodePre->pNext!=pNodeTemp)
294 {
295 pNodePre = pNodePre->pNext;
296 }
297
298 pNodePre->pNext = pNodeTemp->pNext;
299 }
300
301
302 m_iNodeCount--;
303
304 if(m_iNodeCount==0)
305 {
306 m_pTail = NULL;
307 }
308
309
310 if(!CFreeListObj.RecycleNode(pNodeTemp))
311 {
312
313 cout<<"Error"<<endl;
314 }
315
316 }
317
318 int CList::GetNodeCount()
319 {
320 if(m_pHead==NULL)
321 {
322 return 0;
323 }
324
325 else
326 {
327 return m_iNodeCount;
328 }
329 }
330
331
332 bool CList::RecycleNode(pNode pNodeTemp) //头插法
333 {
334 if(pNodeTemp!=NULL)
335 {
336 pNodeTemp->pNext = m_pHead;
337
338 m_pHead = pNodeTemp;
339
340 m_iNodeCount++;
341
342 return true;
343 }
344
345 return false;
346
347 }
348
349 pNode CList::AllocateNode() //从内存池的头部取结点
350 {
351 if(m_pHead == m_pTail)
352 {
353
354 m_pTail = NULL;
355 }
356
357 pNode pNodeTemp = m_pHead;
358
359 m_pHead = m_pHead->pNext;
360
361 pNodeTemp->pNext = NULL;
362
363 m_iNodeCount--;
364
365 return pNodeTemp;
366 }
367
368
369 bool CList::DestroyList()
370 {
371 if(m_pHead == NULL)
372 {
373 return false;
374 }
375
376 pNode pNodeDel = m_pHead;
377
378 while(pNodeDel != NULL)
379 {
380 m_pHead = pNodeDel->pNext;
381
382 free(pNodeDel);
383
384 pNodeDel = m_pHead;
385
386 m_iNodeCount--;
387 }
388
389 return true;
390 }
391
392
393
394
395 int main()
396 {
397
398 CList CFreeListObj;
399
400 CFile CStudent[4];
401
402 CFile CGraduated;
403
404 CStudent[0].GetFileRoute("Student1.txt");
405 CStudent[1].GetFileRoute("Student2.txt");
406 CStudent[2].GetFileRoute("Student3.txt");
407 CStudent[3].GetFileRoute("Student4.txt");
408
409 CGraduated.GetFileRoute("Graduation.txt");
410
411 int i = 0;
412
413 CStudent[0].FileRead("Student1.txt",CFreeListObj);
414 CStudent[1].FileRead("Student2.txt",CFreeListObj);
415 CStudent[2].FileRead("Student3.txt",CFreeListObj);
416 CStudent[3].FileRead("Student4.txt",CFreeListObj);
417
418 i = 0;
419 pNode pNodeTemp = NULL;
420 bool bOK = true;
421
422 char iMethod = 0;
423 int iGrade = 0;
424 int iTemp = 0;
425
426 Data DataTemp = {0};
427
428
429 while(bOK)
430 {
431 cout<<"┏┅┅┅┅┅┅┅┅┅┅┅┅┅┓"<<endl
432 <<"┠ 学生管理系统 ┨"<<endl
433 <<"┠ V1.0 ┨"<<endl
434 <<"┗┅┅┅┅┅┅┅┅┅┅┅┅┅┛"<<endl;
435 cout<<"┏┅┅┅┅┅┅┅┅┅┅┅┅┅┓"<<endl
436 <<"┠ 1.录入学生信息 ┨"<<endl
437 <<"┠ 2.查看全体学生信息 ┨"<<endl
438 <<"┠ 3.大四学生毕业处理 ┨"<<endl
439 <<"┠ 4.查看毕业学生信息 ┨"<<endl
440 <<"┠ 5.保存学生信息 ┨"<<endl
441 <<"┠ 6.退出管理系统 ┨"<<endl
442 <<"┗┅┅┅┅┅┅┅┅┅┅┅┅┅┛"<<endl;
443
444 cin>>iMethod;
445
446
447 switch(iMethod)
448 {
449
450 case '1':
451 cout<<"Input Student's Information:"<<endl;
452 cout<<"Grade:";
453 cin>>iGrade;
454 DataTemp.iGrade = iGrade;
455 cout<<"Name:";
456 cin>>DataTemp.szName;
457 cout<<"Age:";
458 cin>>DataTemp.iAge;
459
460
461 pNodeTemp = CStudent[iGrade-1].CreateNode(DataTemp,CFreeListObj);
462
463 CStudent[iGrade-1].LinkNode(pNodeTemp);
464
465 switch(iGrade)
466 {
467
468 case 1:
469 CStudent[iGrade-1].FileWrite("Student1.txt",CFreeListObj);
470 break;
471 case 2:
472 CStudent[iGrade-1].FileWrite("Student2.txt",CFreeListObj);
473 break;
474 case 3:
475 CStudent[iGrade-1].FileWrite("Student3.txt",CFreeListObj);
476 break;
477 case 4:
478 CStudent[iGrade-1].FileWrite("Student4.txt",CFreeListObj);
479 break;
480 default:
481 break;
482 }
483
484
485 break;
486
487
488 case '2':
489
490 i = 0;
491 CStudent[i].InitList();
492 CStudent[i].FileRead("Student1.txt",CFreeListObj);
493 cout<<"Gread: "<<i+1<<endl;
494 cout<<endl
495 <<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
496 CStudent[i++].TravelList();
497 cout<<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
498
499 CStudent[i].InitList();
500 CStudent[i].FileRead("Student2.txt",CFreeListObj);
501 cout<<"Gread: "<<i+1<<endl;
502 cout<<endl
503 <<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
504 CStudent[i++].TravelList();
505 cout<<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
506
507 CStudent[i].InitList();
508 CStudent[i].FileRead("Student3.txt",CFreeListObj);
509 cout<<"Gread: "<<i+1<<endl;
510 cout<<endl
511 <<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
512 CStudent[i++].TravelList();
513 cout<<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
514
515 CStudent[i].InitList();
516 CStudent[i].FileRead("Student4.txt",CFreeListObj);
517 cout<<"Gread: "<<i+1<<endl;
518 cout<<endl
519 <<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
520 CStudent[i++].TravelList();
521 cout<<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
522 break;
523 case '3':
524
525 CGraduated.FileRead("Graduation.txt",CFreeListObj);
526
527 pNodeTemp = CStudent[3].GetHead();
528
529 while(pNodeTemp!=NULL)
530 {
531 CGraduated.CreateNode(pNodeTemp->DataTemp,CFreeListObj);
532 CGraduated.LinkNode(pNodeTemp);
533 pNodeTemp = pNodeTemp->pNext;
534 }
535
536 CGraduated.FileWrite("Graduation.txt",CFreeListObj);
537 CGraduated.DestroyList();
538
539
540 CStudent[0].FileWrite("Student2.txt",CFreeListObj);
541 CStudent[1].FileWrite("Student3.txt",CFreeListObj);
542 CStudent[2].FileWrite("Student4.txt",CFreeListObj);
543 CStudent[0].InitList();
544 CStudent[0].FileWrite("Student1.txt",CFreeListObj);
545
546 break;
547
548 case '4':
549 cout<<endl
550 <<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
551 CGraduated.InitList();
552 CGraduated.FileRead("Graduation.txt",CFreeListObj);
553 CGraduated.TravelList();
554 cout<<"┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅"<<endl;
555 CGraduated.InitList();
556 break;
557
558 case '5':
559
560
561 CStudent[0].FileWrite("Student1.txt",CFreeListObj);
562 CStudent[1].FileWrite("Student2.txt",CFreeListObj);
563 CStudent[2].FileWrite("Student3.txt",CFreeListObj);
564 CStudent[3].FileWrite("Student4.txt",CFreeListObj);
565
566 system("cls");
567 break;
568
569 case '6':
570
571 CStudent[0].FileWrite("Student1.txt",CFreeListObj);
572 CStudent[1].FileWrite("Student2.txt",CFreeListObj);
573 CStudent[2].FileWrite("Student3.txt",CFreeListObj);
574 CStudent[3].FileWrite("Student4.txt",CFreeListObj);
575
576 cout<<"Successful Exit."<<endl;
577 bOK = false;
578 break;
579
580 default:
581 cout<<"Input Error. "<<endl;
582 break;
583 }
584
585
586 }
587
588 return 0;
589 }