以線性鏈表實作一套學生成績管理系統,功能涵蓋:輸入、輸出、插入、刪除、查找、追加、讀入、顯示、保存、拷貝、排序、索引、分類合計與退出,並在螢幕上輸出操作前後的結果。
具体要求:
1. 寫出系統需求分析並建模。
2. 編程實作,介面友善。
3. 輸出操作前後的結果以便核對。
每個學生節點記錄學號、數學、英語、語文與總分,節點之間以 nextstu 串成單向鏈表,頭節點不存資料,純作哨兵。所有操作都圍繞這個鏈表展開:插入採頭插法,刪除與查找則需要「前驅 + 當前」兩個指標並行前進。排序用雙層迴圈搭配一個 StuCopy 交換資料,刻意不交換節點本身,避免 next 指標錯位導致鏈表斷裂。持久化則靠 ifstream / ofstream 與 StudentList.txt 完成。
#define MAX_NUM 10
#include <String>
#include <fstream>
using namespace std;
bool quit = false;
struct StuNode{
int num;
int math, eng, yuwen;
int sum;
StuNode *nextstu;
};
class SInfo{
StuNode *StuListHead;
public:
SInfo(); //建構子
~SInfo(); //解構子
void CreatSinfo(); //建立學生資訊
void StuInsert(int snum, int smath, int seng, int syuwen); //插入學生資訊
void StuDelete(int snum); //刪除學生資訊
StuNode *StuFind(int snum); //以學號查找學生
void StuModify(int snum, int smath, int seng, int syuwen); //修改學生資訊
void StuCopy(StuNode *ptemp, StuNode *p); //學生資訊拷貝
void StuSort(char ch);
void StuClassfy(); //分類合計
void StuRead(); //從檔案讀入
void StuSave(); //保存到檔案
int IsRead();
void StuQuit();
void ShowInfo(); //遍歷輸出
};
int Systemdoor() //登入驗證,三次機會
{
string username = "Hecoz", password = "password";
string name, temp;
int number = 3;
while (1)
{
cout << " 用 戶 名:";
cin >> name;
cout << " 密 碼:";
cin >> temp;
if (name != username || temp != password)
{
number--;
if (number > 0)
{
cout << " 用戶名/密碼錯誤! 你還有" << number << "次機會" << endl;
}
else
cout << "用戶名/密碼錯誤!" << endl, exit(0);
}
else
{
cout << "********************密碼正確********************" << endl << endl;
return 1;
}
}
}
void ShowMenu()
{
cout << "********************************************" << endl;
cout << "****** 學 生 資 訊 系 統 ******" << endl;
cout << "****** 0.安全退出系統 ******" << endl;
cout << "****** 1.檔案讀入學生資訊 ******" << endl;
cout << "****** 2.錄入新的學生資訊 ******" << endl;
cout << "****** 3.添加新的學生資訊 ******" << endl;
cout << "****** 4.刪除已有學生資訊 ******" << endl;
cout << "****** 5.查找已有學生資訊 ******" << endl;
cout << "****** 6.修改已有學生資訊 ******" << endl;
cout << "****** 7.已有學生資訊排序 ******" << endl;
cout << "****** 8.分類合計學生資訊 ******" << endl;
cout << "****** 9.輸出所有學生資訊 ******" << endl;
cout << "****** 10.保存現有學生資訊 ******" << endl;
cout << "\n\t\n\t\t請選擇:";
}
SInfo::SInfo() //建構子
{
StuListHead = new StuNode;
StuListHead->nextstu = NULL;
}
SInfo::~SInfo() //解構子
{
StuNode *p;
while (StuListHead)
{
p = StuListHead;
StuListHead = StuListHead->nextstu;
delete p;
}
StuListHead = NULL;
}
void SInfo::CreatSinfo() //建立學生資訊表
{
int n;
StuNode *p, *s;
p = StuListHead;
cout << "請輸入學生人數:";
cin >> n;
for (int i = 1; i <= n; i++)
{
s = new StuNode;
cin >> s->num >> s->math >> s->eng >> s->yuwen;
s->sum = s->math + s->eng + s->yuwen;
s->nextstu = p->nextstu;
p->nextstu = s;
p = p->nextstu;
}
if (p == NULL) //判斷是否建立成功
{
cout << "建立失敗,請重新建立!" << endl;
CreatSinfo();
}
}
void SInfo::ShowInfo() //遍歷輸出
{
StuNode *p;
cout << "學號" << '\t' << "數學" << '\t' << "英語" << '\t' << "語文" << '\t' << "總分" << endl;
for (p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
cout << p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << endl;
}
}
void SInfo::StuInsert(int snum, int smath, int seng, int syuwen) //插入(頭插法)
{
StuNode *s,*p;
s = new StuNode;
s->num = snum;
s->math = smath;
s->eng = seng;
s->yuwen = syuwen;
s->sum = s->math + s->eng + s->yuwen;
p = StuListHead;
s->nextstu = p->nextstu;
p->nextstu = s;
}
void SInfo::StuDelete(int snum)
{
StuNode *p, *ptemp;
p = StuListHead;
ptemp = p;
while (p->nextstu && p->num != snum) //走到尾端或找到目標為止
{
ptemp = p;
p = p->nextstu;
}
if (p->num == snum)
{
ptemp->nextstu = p->nextstu;
delete p;
}
else
{
cout << "未找到該學生資訊!" << endl;
}
}
StuNode *SInfo::StuFind(int snum)
{
StuNode *p;
p = StuListHead->nextstu;
while (p->nextstu && p->num != snum)
{
p = p->nextstu;
}
if (p->num == snum)
{
return p;
}
else
{
cout << "未找到該學生資訊!" << endl;
return NULL;
}
}
void SInfo::StuModify(int snum, int smath, int seng, int syuwen)
{
StuNode *ItemStu = StuFind(snum); //直接呼叫查找函式
if (ItemStu != NULL)
{
ItemStu->num = snum;
ItemStu->math = smath;
ItemStu->eng = seng;
ItemStu->yuwen = syuwen;
ItemStu->sum = ItemStu->math + ItemStu->eng + ItemStu->yuwen;
}
}
void SInfo::StuCopy(StuNode *ptemp, StuNode *p) //把 p 的資料拷貝到 ptemp
{
if (p == NULL)
{
cout << "拷貝目標為空!" << endl;
}
else
{
ptemp->num = p->num;
ptemp->math = p->math;
ptemp->eng = p->eng;
ptemp->yuwen = p->yuwen;
ptemp->sum = p->sum;
//ptemp->nextstu = p->nextstu; //只拷資料,next 不能拷,否則鏈表會斷
}
}
void SInfo::StuSort(char ch) //依總分或學號排序
{
if (ch == '>' || ch == '<')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
bool needSwap = (ch == '>') ? (p->sum > q->sum) : (p->sum < q->sum);
if (needSwap)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else if (ch == 'o') //依學號排序
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->num < q->num)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else
{
cout << "排序條件出錯!" << endl;
}
}
void SInfo::StuClassfy() //依數學成績分級統計
{
int grade[5] = {0};
StuNode *p = StuListHead->nextstu;
while (p != NULL)
{
if (89 < p->math) grade[0]++;
else if (79 < p->math && p->math < 90) grade[1]++;
else if (69 < p->math && p->math < 80) grade[2]++;
else if (59 < p->math && p->math < 70) grade[3]++;
else grade[4]++;
p = p->nextstu;
}
cout << "A" << '\t' << "B" << '\t' << "C" << '\t' << "D" << '\t' << "E" << endl;
for (int i = 0; i < 5; i++)
{
cout << grade[i] << '\t';
}
cout << endl;
}
void SInfo::StuRead() //從檔案讀入
{
ifstream in("StudentList.txt");
if (!in) { cout << "沒有學生資訊,請先錄入學生資訊!" << endl; return; }
while (!in.eof())
{
int num, math, eng, yuwen, sum;
in >> num >> math >> eng >> yuwen >> sum;
StuInsert(num, math, eng, yuwen);
}
}
void SInfo::StuSave() //保存學生資訊
{
StuNode *p = StuListHead->nextstu;
ofstream out("StudentList.txt");
if (!out) { cout << "不能開啟檔案!" << endl; return; }
while (p != NULL)
{
out << p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << '\n';
p = p->nextstu;
}
}
void SInfo::StuQuit() //退出前詢問是否保存
{
char choice;
cout << "是否保存學生資訊?(Y/N)";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
StuSave();
cout << "學生資訊已保存..." << endl;
}
}
#include<iostream>
#include "SInfo.h"
#include<cstdlib>
using namespace std;
int main()
{
Systemdoor();
int x = 100, pnum, pmath, peng, pyuwen;
StuNode *pfind;
SInfo stu;
cout << "******************************************" << endl;
cout << "****** 歡迎進入學生資訊管理系統 ******" << endl;
cout << "******************************************" << endl;
while (x != 0)
{
system("pause");
system("cls"); //清屏
ShowMenu();
cin >> x;
switch (x)
{
case 0:
stu.StuQuit();
break;
case 1:
stu.StuRead();
cout << "讀入學生資訊表:" << endl;
stu.ShowInfo();
break;
case 2:
stu.CreatSinfo();
cout << "請核對輸入學生資訊!" << endl;
stu.ShowInfo();
break;
case 3:
cout << "請輸入添加學生資訊:";
cin >> pnum >> pmath >> peng >> pyuwen;
stu.StuInsert(pnum, pmath, peng, pyuwen);
cout << "更新學生資訊表..." << endl;
stu.ShowInfo();
break;
case 4:
cout << "請輸入要刪除學生學號:";
cin >> pnum;
stu.StuDelete(pnum);
cout << "更新學生資訊表..." << endl;
stu.ShowInfo();
break;
case 5:
cout << "請輸入要查找學生學號:";
cin >> pnum;
pfind = stu.StuFind(pnum);
if (pfind) cout << "查找學生學號:" << pfind->num << " 數學 " << pfind->math << " 英語 " << pfind->eng << " 語文 " << pfind->yuwen << " 總分 " << pfind->sum << endl;
break;
case 6:
cout << "請輸入要修改學生學號:";
cin >> pnum;
cout << "請重新輸入學生分數:";
cin >> pmath >> peng >> pyuwen;
stu.StuModify(pnum, pmath, peng, pyuwen);
cout << "修改成功!" << endl;
stu.ShowInfo();
break;
case 7:
cout << "升序排序(1) 降序排序(0) 學號排序(10):";
cin >> pnum;
if (pnum == 1) stu.StuSort('<');
else if (pnum == 0) stu.StuSort('>');
else if (pnum == 10) stu.StuSort('o');
else cout << "請輸入正確選擇!" << endl;
stu.ShowInfo();
break;
case 8:
stu.StuClassfy();
break;
case 9:
stu.ShowInfo();
break;
case 10:
stu.StuSave();
break;
}
}
system("pause");
return 0;
}
整份系統的骨架就是「鏈表 + 檔案」。鏈表負責記憶體中的動態增刪,檔案負責跨執行期的資料保存,兩者靠 StuSave 與 StuRead 串接。練習時可以嘗試把 StuSort 的氣泡式雙層迴圈換成快速排序或合併排序,並為鏈表加上索引(例如以學號建立哈希表),這兩個改動最能體會資料結構選擇對效能的影響。