学生成绩(顺序表)
#include<iostream>
#include<string>
#include<iomanip>
#include<windows.h>
using namespace std; const int MAXSIZE = 25; typedef struct A { string Name; string Num; string sex; float EnglishScore;
}StudentList; //重載輸入運算符以輸入struct類型數據
istream & operator >>(istream & in, StudentList &A)
{ cout << "請輸入姓名:"; in >> A.Name; cout << endl; cout << "請輸入學號:"; in >> A.Num; cout << endl; cout << "請輸入性別:"; in >> A.sex; cout << endl; cout << "請輸入英語成績:"; cin >> A.EnglishScore; return in;
} //重載輸出運算符以輸出struct類型數據
ostream & operator<<(ostream &out, StudentList &B)
{ out << B.Name << B.Num << B.sex << B.EnglishScore << endl; return out;
} //數據表的定義
typedef struct B { StudentList elem[MAXSIZE];//內存空間大小 int Last;//表長 }SeqList; int main()
{ int Length; int Option = 0; SeqList L; L.Last = 0; cout << "請輸入線性表長度: "; cin >> Length; system("CLS"); //各種函數定義 void CreateList(SeqList* L, int Length); void PrintList(SeqList* L, int Length); void SequenceList(SeqList* L, int Length); void DeleteList(SeqList* L, int Length); void InsertList(SeqList*L, int Length); void ChangeList(SeqList* L, int Length); void SearchList(SeqList* L, int Length); while (1) { cout << endl << "-----------1. 創建順序表." << endl; cout << " -----------2. 輸出順序表元素." << endl; cout << " -----------3. 順序表元素排序. " << endl; cout << " -----------4. 向順序表中插入元素." << endl; cout << " -----------5. 刪除順序表單個元素. " << endl; cout << " ---------- 6. 修改順序表單個元素. " << endl; cout << " ---------- 7. 查詢順序表單個元素. " << endl; cout << endl << "請輸入你的選擇: "; cin >> Option; switch (Option) { case 0: break; case 1: CreateList(&L, Length); break; case 2: PrintList(&L, Length); break; case 3: SequenceList(&L, Length); break; case 4: InsertList(&L, Length); break; case 5: DeleteList(&L, Length); break; case 6: ChangeList(&L, Length); break; case 7: SearchList(&L, Length); break; default: cout << "輸入錯誤!"; break; } cout << endl; } return 0;
} //創建順序表
void CreateList(SeqList* L, int Length)
{ int i; cout << "請輸入順序表元素:"; for (i = 1; i < Length + 1; i++) { cout << "請輸入第" << i << "個學生的信息:"; cin >> L->elem[i - 1]; L->Last = Length; if (Length >= MAXSIZE) { cout << endl << "表滿!" << endl; } } for (i = 0; i < Length; i++) { cout << L->elem[i] << " "; }
} //輸出表
void PrintList(SeqList* L, int Length)
{ int i; cout << "姓名 " << "---------" << "學號 " << "---------" << "性別 " << "---------" << "英語成績 " << endl; for (i = 0; i < L->Last; i++) { cout << L->elem[i].Name << "--------- " << L->elem[i].Num << "--------" << L->elem[i].sex << "-------- " << L->elem[i].EnglishScore << endl; } cout << endl;
} //對數據按學號元素排序
void SequenceList(SeqList* L, int Length)
{ StudentList Temp; for (int i = 0; i <Length - 1; i++) { for (int j = 0; j <Length - 1 - i; j++) { if (L->elem[j].Num > L->elem[j + 1].Num) { Temp = L->elem[j]; (L->elem[j]) = (L->elem[j + 1]); L->elem[j + 1] = Temp; } } } cout << endl;
} //刪除表的某個元素
void DeleteList(SeqList* L, int Length)
{ int DeData = 0; cout << "要刪除第幾個元素:"; cin >> DeData; for (int i = DeData; i < L->Last; i++) { L->elem[i - 1] = L->elem[i]; } L->Last--; for (int k = 0; k < L->Last; k++) { cout << L->elem[k] << " "; } cout << endl;
} //往表中插入元素
void InsertList(SeqList* L, int Length)
{ StudentList InsertData; int i = 0; int InDataLocation = 0; cout << "請輸入要插入的元素: "; cin >> InsertData; cout << endl; cout << "請輸入要插入的位置: "; cin >> InDataLocation; cout << endl; if (InDataLocation > L->Last + 1) { cout << "插入位置超過表的最大長度,請重新輸入!" << endl << endl; InsertList(L, Length); } else { for (int i = L->Last; i >= InDataLocation - 1; i--)
{ L->elem[i + 1] = L->elem[i]; } L->elem[InDataLocation - 1] = InsertData; L->Last++; cout << "插入后的表為:"; PrintList(L, Length); } cout << endl;
} //修改順序表某個元素
void ChangeList(SeqList* L, int Length)
{ string ChangeNum; cout << "請輸入要修改成績的學生的學號: "; cin >> ChangeNum; for (int i = 0; i < L->Last; i++) { if (L->elem[i].Num == ChangeNum) { cout << "你將把英語成績改為: "; cin >> L->elem[i].EnglishScore; cout << endl << "修改成功!" << endl; } } } //查詢學生信息
void SearchList(SeqList* L, int Length)
{ string SearchName; cout << "請輸入要查詢學生的姓名: "; cin >> SearchName; for (int i = 0; i < L->Last; i++) { if (L->elem[i].Name == SearchName) { cout << "姓名 " << "---------" << "學號 " << "---------" << "性別 " << "---------" << "英語成績 " << endl; cout << L->elem[i].Name << "--------- " << L->elem[i].Num << "--------" << L->elem[i].sex << "-------- " << L->elem[i].EnglishScore << endl; } } }
總結
- 上一篇: 实验一线性表的基本操作实现及其应用(Ja
- 下一篇: 学生成绩(链表)