课程设计之学生成绩管理系统

#include<iostream>
#include<stdlib.h>
#include<ostream>
#define Max_Length 100
using namespace std;
void menuChange();
class stu
{
    friend ostream&operator<<(ostream &out,stu &stu);//全局函数
public:
    string Name;
    string Id;
    int Chinese;
    int Match;
    int English;
    int totalScore;
    stu &operator=(stu &stu);
    bool operator==(stu &stu);
};
stu &stu::operator=(stu &stu)
{
    this->Name=stu.Name;
    this->Id=stu.Id;
    this->Chinese=stu.Chinese;
    this->Match=stu.Match;
    this->English=stu.English;
    this->totalScore=stu.totalScore;
    return *this;
}
bool stu::operator==(stu &stu)
{
    if(this->Name == stu.Name&&this->Id == stu.Id)
        return true;
    return false;
}
ostream &operator<<(ostream &out,stu &stu)
{
    out<<" "<<stu.Id<<"      "<<stu.Name<<"      "<<stu.Chinese<<"      "<<stu.Match<<"      "<<stu.English<<"      "<<stu.totalScore<<endl;
    return out;
}
class List
{
public:
    List(int size);//创建
    ~List();//销毁
    void ClearList();//清空线性表
    int ListLenght();//求线性表的长度
    void idLocate(string *str);//
    bool ListInsert(int i,stu *stu);//在第i个位置插入元素
    void ListDelete(string *str);//删除第i个位置的元素
    void ListTraverse();//遍历线性表
    void ListChange(string *str);//修改数据
    int Partition(int low,int high);
    void QSort(int low,int high);
    void QuickSort();
private:
    stu *m_pList;
    int m_iSize;//线性表大小
    int m_iLength;//线性表长度
};
List::List(int size)
{
    m_iSize=size;
    m_pList=new stu[m_iSize];
    m_iLength=0;
}
List::~List()
{
    delete []m_pList;
    m_pList=NULL;
    cout<<"销毁完成"<<endl;
}
void List::ClearList()
{
    m_iLength=0;
}
int List::ListLenght()
{
    return m_iLength;
}
void List::idLocate(string *str)
{
    bool flag=false;
    for(int i=1;i<m_iLength;i++)
    {
        if(m_pList[i].Id==*str)
        {
            flag=true;
            cout<<"学号---姓名---语文---数学---英语---总分"<<endl;
            cout<<m_pList[i]<<endl;
        }
    }
    if(!flag)
        cout<<"No Data"<<endl;

}
void List::ListTraverse()
{
    //    cout<<m_pList[0]<<endl;//辅助位置
    if(m_iLength>1)
    {
        cout<<"学号---姓名---语文---数学---英语---总分"<<endl;
        for(int i=1;i<m_iLength;i++)
        cout<<m_pList[i]<<endl;
    }
    else
        cout<<"暂无记录,请先添加数据!"<<endl;
}
void List::ListChange(string *str)
{
    int k;
    bool flag=false;
    for(int i=1;i<m_iLength;i++)
    {
        if(m_pList[i].Id==*str)
        {
            flag=true;
            cout<<"学号---姓名---语文---数学---英语---总分"<<endl;
            cout<<m_pList[i]<<endl;
            menuChange();
            cin>>k;
            while(k!=0)
            {
                switch(k)
                {
                case 1:
                    cout<<"新学号:"<<endl;
                    cin>>m_pList[i].Id;
                    cout<<"学号已修改"<<endl;
                    break;
                case 2:
                    cout<<"新姓名:"<<endl;
                    cin>>m_pList[i].Name;
                    cout<<"姓名已修改"<<endl;
                    break;
                case 3:
                    cout<<"新语文:"<<endl;
                    cin>>m_pList[i].Chinese;
                    cout<<"语文已修改"<<endl;
                    break;
                case 4:
                    cout<<"新数学:"<<endl;
                    cin>>m_pList[i].Match;
                    cout<<"数学已修改"<<endl;
                    break;
                case 5:
                    cout<<"新英语:"<<endl;
                    cin>>m_pList[i].English;
                    cout<<"英语已修改"<<endl;
                    break;
                default:
                    cout<<"选择错误,请重新选择:"<<endl;
                }
                if(k>2&&k<6)//更新成绩
                {
                    m_pList[i].totalScore= m_pList[i].Chinese+ m_pList[i].Match+ m_pList[i].English;
                }
                system("pause");
                system("cls");
                menuChange();
                cin>>k;
            }
        }
    }
    if(!flag)
        cout<<"No Data"<<endl;
}
bool List::ListInsert(int i,stu *stu)
{
    if(i<0||i>m_iLength)
    {
        return false;
    }
    for(int k=m_iLength-1;k>=i;k--)
    {
        m_pList[k+1]=m_pList[k];
    }
    m_pList[i]=*stu;
    m_iLength++;
    return true;
}
void List::ListDelete(string *str)
{
    bool flag=false;
    for(int i=1;i<m_iLength;i++)
    {
        if(m_pList[i].Id==*str)
        {
            flag=true;
            cout<<"删除 "<<m_pList[i].Id<<endl;;
            for(int k=i+1;k<m_iLength;k++)
            {
                m_pList[k-1]=m_pList[k];
            }
            m_iLength--;
        }
    }
    if(!flag)
        cout<<""<<endl;
}
int List::Partition(int low,int high)
{
    m_pList[0]=m_pList[low];//用子表的第一个数据做基准数
    int pivotkey = m_pList[low].totalScore;//将基准数保存在pivotkey中
    while(low<high)//长度大于1
    {
        while(low<high&&m_pList[high].totalScore>=pivotkey)--high;//将比基准数小的数移到低位
        m_pList[low]=m_pList[high];//交换两个数的位置
        while(low<high&&m_pList[low].totalScore<=pivotkey) ++low;//将比基准数大的数移到高位
        m_pList[high]=m_pList[low];//交换两个数的位置
    }
    m_pList[low]=m_pList[0];//基准数归位
    return low;//返回基准数位置
}
void List::QSort(int low,int high)
{
    if(low<high)//长度大于1
    {
        int pivotloc = Partition(low,high);//将数据一分为二,pivotloc是基准数位置
        QSort(low,pivotloc-1);//对左子表递归排序
        QSort(pivotloc+1,high);//对右子表递归排序
    }
}
void List::QuickSort()
{
    QSort(1,ListLenght()-1);
}
void menu()
{
    cout<<"\t\t\t\t┏━━━━━━━━━┓"<<endl;
    cout<<"\t\t\t\t┃    1:批量录入    ┃"<<endl;
    cout<<"\t\t\t\t┃    2:查看记录    ┃"<<endl;
    cout<<"\t\t\t\t┃    3:数据排序    ┃"<<endl;
    cout<<"\t\t\t\t┃    4:数据查询    ┃"<<endl;
    cout<<"\t\t\t\t┃    5:批量删除    ┃"<<endl;
    cout<<"\t\t\t\t┃    6:数据修改    ┃"<<endl;
    cout<<"\t\t\t\t┃    0:退出系统    ┃"<<endl;
    cout<<"\t\t\t\t┗━━━━━━━━━┛"<<endl;
    cout<<"\t\t\t\t      选项 : [ ]\b\b";
}
void menuhello()
{
    cout<<"\t\t\t\t┏━━━━━━━━━┓"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃     O(∩_∩)O    ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃     Hello !~    ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┗━━━━━━━━━┛"<<endl;
    cout<<"\t\t\t\t ";
}
void menubyebye()
{
    cout<<"\t\t\t\t┏━━━━━━━━━┓"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃    O(∩_∩)O     ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃     ByeBye!~    ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┃                  ┃"<<endl;
    cout<<"\t\t\t\t┗━━━━━━━━━┛"<<endl;
}
void menuChange()
{
    cout<<"\t\t\t\t┏━━━━━━━━┓"<<endl;
    cout<<"\t\t\t\t┃     1:学号     ┃"<<endl;
    cout<<"\t\t\t\t┃     2:姓名     ┃"<<endl;
    cout<<"\t\t\t\t┃     3:语文     ┃"<<endl;
    cout<<"\t\t\t\t┃     4:数学     ┃"<<endl;
    cout<<"\t\t\t\t┃     5:英语     ┃"<<endl;
    cout<<"\t\t\t\t┃     0:返回     ┃"<<endl;
    cout<<"\t\t\t\t┗━━━━━━━━┛"<<endl;
    cout<<"\t\t\t\t      选项 : [ ]\b\b";
}
int main()
{
    system("color 0F");
    List *list1=new List(Max_Length);
    stu stu;//临时stu类变量
    int i,n;//临时int型变量
    string str;//临时string型变量
    list1->ListInsert(0,&stu);快排辅助空间
    int key;
    menuhello();
    system("pause");
    system("cls");
    menu();
    cin>>key;
    while(key!=0)
    {
        switch(key)
        {
        case 1:
            system("cls");
            cout<<"个数:"<<endl;
            cin>>n;
            for(i=1;i<=n;i++)
            {
                cout<<"数据"<<i<<":"<<endl;
                cout<<"学号:";
                cin>>stu.Id;
                cout<<"姓名:";
                cin>>stu.Name;
                cout<<"语文:";
                cin>>stu.Chinese;
                cout<<"数学:";
                cin>>stu.Match;
                cout<<"英语:";
                cin>>stu.English;
                stu.totalScore=stu.Chinese+stu.Match+stu.English;
                list1->ListInsert(i,&stu);
            }
            break;
        case 2:
            system("cls");
            list1->ListTraverse();
            break;
        case 3:
            system("cls");
            list1->QuickSort();//调用快速排序函数
            list1->ListTraverse();
            break;
        case 4:
            system("cls");
            if(list1->ListLenght()>1)
            {
                cout<<"学号:"<<endl;
                cin>>str;
                list1->idLocate(&str);
            }
            else
                cout<<"暂无记录,请先添加数据!"<<endl;
            break;
        case 5:
            system("cls");
            if(list1->ListLenght()>1)
            {
                cout<<"个数:"<<endl;
                cin>>n;
                for(i=1;i<=n;i++)
                {
                    cout<<"学号:"<<endl;
                    cin>>str;
                    list1->ListDelete(&str);
                }
            }
            else
                cout<<"暂无记录,请先添加数据!"<<endl;
            break;

        case 6:
            system("cls");
            if(list1->ListLenght()>1)
            {
                cout<<"学号:"<<endl;
                cin>>str;
                list1->ListChange(&str);
            }
            else
                cout<<"暂无记录,请先添加数据!"<<endl;
            break;
        default:
            cout<<"选择错误,请重新选择:"<<endl;
        }
        system("pause");
        system("cls");
        menu();
        cin>>key;
    }
    system("cls");
    menubyebye();
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值