雖然比較水 =。= 但是寫了兩節課+一個中午 都是強迫症的鍋
http://www.cnblogs.com/wenruo/p/4940182.html
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
class student // student 類為管理系統的一個節點
{
friend class studentMessage;
student *next;
char name[30];
char num[30];
double score[6];
public:
student()
{
strcpy(name, "null");
strcpy(num, "null");
for (int i = 0; i < 6; ++i) score[i] = 0;
}
student(const student &a)
{
strcpy(name, a.name);
strcpy(num, a.num);
for (int i = 0; i < 6; ++i)
score[i] = a.score[i];
}
student &input()
{
cout << "姓名:"; cin >> name;
cout << "學號:"; cin >> num;
for (int i = 0; i < 5; ++i)
{
cout << "輸入第" << (i + 1) << "門成績:";
cin >> score[i];
score[5] += score[i];
}
return *this;
}
student* get_next()
{
return next;
}
void print()
{
printf("%8s %10s ", name, num);
for (int i = 0; i < 6; ++i) printf(" %6.2f ", score[i]);
printf("\n");
}
void Swap() // 交換當前結點和下一個
{
char temp[30];
strcpy(temp, name);
strcpy(name, next->name);
strcpy(next->name, temp);
strcpy(temp, num);
strcpy(num, next->num);
strcpy(next->num, temp);
for (int i = 0; i < 6; ++i)
{
int tmp = score[i];
score[i] = next->score[i];
next->score[i] = tmp;
}
}
};
class studentMessage
{
protected:
student *first;
student *last;
public:
studentMessage()
{
first = last = new student;
}
~studentMessage()
{
Clear();
delete first;
}
studentMessage &Append(); // 在鏈表尾部插入節點
student *Find(const char *x); // 查找
void Query(); // 查詢
studentMessage &Delete(); // 刪除查找結點
studentMessage &Clear(); // 刪除所有結點
studentMessage &Sort(); // 按第i門課排序
void print(); // 顯示所有節點
int menu(); // 顯示菜單
};
studentMessage &studentMessage::Append()
{
student x;
x.input();
student *ptr = last;
*ptr = x;
last = new student;
ptr->next = last;
return *this;
}
studentMessage &studentMessage::Delete()
{
char x[30];
cout << "輸入要刪除的信息(學號或姓名)\n";
cin >> x;
student *ptr = Find(x);
if (ptr == last)
{
cout << "未找到相關信息\n";
return *this;
}
ptr->print();
cout << "是否刪除此信息?(是輸入1,否輸入0)";
cin >> x;
if (*x == '1')
{
if (ptr == first)
{
first = first->next;
delete first;
}
else
{
student *temp = first;
while (temp->next != ptr) temp = temp->next;
temp->next = ptr->next;
delete ptr;
}
cout << "已刪除\n";
}
else
{
cout << "已取消\n";
}
return *this;
}
studentMessage &studentMessage::Clear()
{
char ch[30];
cout << "將刪除所有信息。確認請按Y";
cin >> ch;
if (*ch != 'Y' && *ch != 'y')
{
cout << "已取消\n";
return *this;
}
student *ptr = first;
while (ptr != last)
{
student *next = ptr->next;
delete ptr;
ptr = next;
}
first = last;
return *this;
}
void studentMessage::print()
{
if (first == last)
{
cout << "暫無信息\n";
return ;
}
cout << "---------------------------------成績列表------------------------------" << endl;
cout << "序號 姓名 學號 成績1 成績2 成績3 成績4 成績5 總成績" << endl;
int no = 1;
student *ptr = first;
while (ptr != last)
{
printf("%3d:", no++);
ptr->print();
ptr = ptr->next;
}
}
student *studentMessage::Find(const char *x)
{
student *ptr = first;
while (ptr != last)
{
if (strcmp(ptr->name, x) == 0 || strcmp(ptr->num, x) == 0)
break;
ptr = ptr->next;
}
return ptr;
}
void studentMessage::Query()
{
char x[30];
cout << "輸入你要查詢的信息(學號或姓名)";
cin >> x;
student *ptr = Find(x);
if (ptr == last)
{
cout << "沒有查詢到相關信息\n";
}
else
{
ptr->print();
}
}
// 冒泡排序
studentMessage &studentMessage::Sort()
{
int x;
cout << "輸入你想按照哪門課的成績來排序?((1-5)總成績輸入6):";
cin >> x;
x--;
student *ptr, *cnt = last;
while (cnt != first)
{
ptr = first;
while (ptr->next != cnt)
{
if (ptr->next->score[x] > ptr->score[x])
{
ptr->Swap();
}
ptr = ptr->next;
}
cnt = ptr;
}
return *this;
}
int studentMessage::menu()
{
cout << "===============================\n";
cout << " 學生成績管理系統\n\n";
cout << " 1.顯示所有學生成績\n";
cout << " 2.添加信息\n";
cout << " 3.查詢信息\n";
cout << " 4.刪除信息\n";
cout << " 5.成績排序\n";
cout << " 6.刪除所有信息\n";
cout << " 0.退出\n";
cout << "===============================\n";
int ch;
cin >> ch;
return ch;
}
int main()
{
studentMessage ls;
int ch;
while (ch = ls.menu())
{
switch(ch)
{
case 1: ls.print(); break;
case 2: ls.Append(); break;
case 3: ls.Query(); break;
case 4: ls.Delete(); break;
case 5: ls.Sort(); break;
case 6: ls.Clear(); break;
}
}
return 0;
}
