成绩排序运行不了,总成绩怎么改成平均分~~请大家帮帮忙??!

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
class 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();            
    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 2: ls.print(); break;
            case 1: 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;
}
  • 我喜欢论坛,它不仅让我学到了许多知识及传播知识,同时让我看到了许多不能自圆其说的帖子,让我开心 2019-05-18
  • 夏普收购东芝PC业务重回个人电脑市场 2019-05-14
  • 既能散寒止痛也可抗菌杀毒 艾叶竟还有这些妙用? 2019-05-14
  • 让山里娃感受智慧科技乐趣 2019-05-11
  • 基层公安民警开展无人机警务驾驶培训 2019-05-02
  • 我省首例侵犯公民个人信息案终审宣判 2019-05-01
  • 女性之声——全国妇联 2019-04-23
  • 瑞典民众庆祝世界杯胜利时发生枪击 致4人受伤 2019-04-19
  • 遭遇隐形歧视 就业权益谁来维护 2019-04-10
  • 杭州拱墅区:打造“4+X”巡逻防控体系 2019-04-10
  • 新华网评:流浪犬伤人事件频发,亟待多措并举 2019-04-08
  • 两队防守均是薄弱环节 进攻快VS球风猛谁占优? 2019-04-05
  • 安徽九旬教师守候留守儿童:“鞠躬尽瘁,死而不已” 2019-04-05
  • 中纪委:有干部不信马列信鬼神 触犯纪律信小圈子 2019-04-02
  • 小说文体审视、研究唐蕃关系的新突破 2019-03-31
  • 182| 178| 631| 559| 753| 641| 370| 81| 325| 574|