小草的Trouble学生信息管理系统

小草最近上课学C++,在图书馆纠结了好久,决定做这个小东西,没想到遇到了好多困难,好吧,功夫不负有心人,小草也在敲代码中提高了不少。

小草硬是学了好几天,才搞完这个东西,也算是了结了小草的一个心结。

小草的Trouble学生信息管理系统写得不咋样,就是一个学习C++的笔记吧。

1、类。

2、继承与派生。

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <list>

using namespace std;

class tagScore
{
public:
    tagScore();
    tagScore(int ch,int ma,int en):chinese(ch),math(ma),english(en) {}
protected:
    int chinese;
    int math;
    int english;
};

tagScore::tagScore()
{
    chinese=-1;
    math=-1;
    english=-1;
}

class tagStudent:protected tagScore
{
public:
    tagStudent(int ch,int ma,int en,string na,int i,char s):tagScore(ch,ma,en)
    {
        name=na;
        id=i;
        sex=s;
        score=ch+ma+en;
    }
    void setdata();
    void display()
    {
        //score=chinese+math+english;
        printf("%d ",id);
        cout<<name;
        score=chinese+math+english;
        printf("  %c %6d %6d %6d %6d
",sex,score,chinese,math,english);
    }
    int getDataScore()
    {
        return score;
    }
    int getDataChinese()
    {
        return chinese;
    }
    int getDataMath()
    {
        return math;
    }
    int getDataEnglish()
    {
        return english;
    }
    int getDataId()
    {
        return id;
    }
    string getDataName()
    {
        return name;
    }
private:
    string name;
    int id;
    int score;
    char sex;
};

void tagStudent::setdata()
{
    cin>>id>>name>>sex>>chinese>>math>>english;
}

typedef tagScore Score;
typedef tagStudent Student;

Student a[3]
{
    tagStudent(43,35,35,"xiaoming",20153480,'M'),
    tagStudent(23,35,35,"liyang",20153435,'W'),
    tagStudent(43,35,35,"kjdfi",20153847,'M')
};

void print()///显示学生数据
{
    cout << "                                 学生成绩信息                                    " << endl;
    cout << "================================================================================" << endl;
    cout <<"ID	Name	Sex  Scores  chinese  math  english"<<endl;
}

/*HomePage A*/

void screenA()
{
    cout << "                                学生成绩管理系统                                 " << endl;
    cout << "================================================================================" << endl;
    cout << "         1----------按姓名查询        2----------按ID查询" << endl;
    cout << "         3----------按学号排序并输出  4----------按成绩排序并输出" << endl;
    cout << "         0----------退出程序"<<endl;
    cout << "================================================================================" << endl;
}

///按姓名查询
void FindByName()
{
    bool flag=false;
    system("cls");
    cout<<"请输入姓名"<<endl;
    string n;
    cin>>n;
    int i;
    for(i=0; i<3; i++)
    {
        if(a[i].getDataName()==n)
        {
            flag=true;
            break;
        }
    }
    if(flag==true)
    {
        print();
        a[i].display();
    }
    else
        printf("It is nonexistent!
");
}

///按ID查询
void FindById()
{
    bool flag=false;
    system("cls");
    printf("请输入学的ID
");
    int i;
    int pos;
    cin>>pos;
    for(i=0; i<3; i++)
    {
        if(a[i].getDataId()==pos)
        {
            flag=true;
            break;
        }
    }
    if(flag==true)
    {
        print();
        a[i].display();
    }
    else printf("It is nonexistent!
");
}

int CmpById(const void *a,const void *b)
{
    Student p1=*((Student *)a);
    Student p2=*((Student *)b);
    if(p1.getDataId()!=p2.getDataId())
        return p1.getDataId()-p2.getDataId();
}
///按学号排序输出
void SortById()
{
    system("cls");
    print();
    qsort(a,3,sizeof(a[0]),CmpById);
    int i;
    for(i=0; i<3; i++)
    {
        a[i].display();
    }
}

int CmpByScore(const void *a,const void *b)
{
    Student p1=*((Student *)a);
    Student p2=*((Student *)b);
    if(p1.getDataScore()!=p2.getDataScore())///成绩按降序排列
        return p2.getDataScore()-p1.getDataScore();
    else if(p1.getDataChinese()!=p2.getDataChinese())
        return p2.getDataChinese()-p1.getDataChinese();
    else if(p1.getDataMath()!=p2.getDataMath())
        return p2.getDataMath()-p1.getDataMath();
    else if(p1.getDataEnglish()!=p2.getDataEnglish())
        return p2.getDataEnglish()-p1.getDataEnglish();
    else return p1.getDataId()-p2.getDataId();
}

///按成绩排序并输出
void SortByScore()
{
    system("cls");
    print();
    qsort(a,3,sizeof(a[0]),CmpByScore);
    for(int i=0; i<3; i++)
    {
        a[i].display();
    }
}

/*void DelStudent()
{
    system("cls");
    printf("请输入要删除学生的ID
");
    int pos;
    bool flag=false;
    cin>>pos;
    for(int i=0; i<3; i++)
    {
        if(pos==i)
        {
            flag=true;
            for(int j=i; j<3; j++)
                a[j]=a[j+1];
        }
    }
    if(flag==false)
    {
        cout<<"It is nonexistent!
";
    }
}*/
int main()
{
    screenA();
    int order;

    scanf("%d",&order);
        switch(order)
        {
        case 1:
            FindByName();
            system("pause");
            break;
        case 2:
            FindById();
            system("pause");
            break;
        case 3:
            SortById();
            break;
        case 4:
            SortByScore();
            system("pause");
            break;
        case 0:
            system("pause");
            break;
        default:
            cout<<"错误命令
"<<endl;
        }
    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/5269547.html