m0_53989813 2021-08-28 13:37 采纳率: 73.7%
浏览 42
已结题

成绩管理程序,我把定义的所有函数都放到一个类里,但是无法成功生成,为什么??

学生成绩管理程序:用面向对象的类设计一个菜单驱动的学生成绩管理程序。学生的基本信息包括学生的学号、姓名、数学、语文、英语等信息。实现如下管理功能:(1)能输入并显示n个学生的基本信息。(2)计算学生的平均分和总分。(3)按总分降序进行排序。(4)任意输入一个学号,能显示该学生的基本信息。(5)可添加学生信息到指定位置,亦可在最后追加一批学生信息。(6)文件的导入和导出(从文件中读取若干条学生信息,或者将学生信息输出到文件中)。

img


#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <string.h>
using namespace std;
int num=0;
class Student   
{
public:
    int id;
    string name;
    int math;
    int Chinese;
    int English;
    int sum;
    int aver;
};
class Function
{
public:
    void ShowMenu();//显示菜单
    void CreatList(Student str[]);//创建
    void AddStu(Student str[]);//添加
    void Gather(Student str[]);//查询
    void Sort(Student str[]);//排序
    bool Compare(Student p,Student q);
    void Search(Student str[]);//查询
    void display(Student str[]);//显示
    void Fileinto(Student str[]);//Wenjian
    void Fileout(Student str[]);//wenjian
};
int main()
{
    Student str[1000];
    Function use;
    use.ShowMenu();
    while(1)
    {
        int n;
        cin >> n;
        switch (n)
        {
        case 1:       //创建
            {
                use.CreatList(str);
                break;
            }
        case 2:        //添加
            {
                use.AddStu(str);
                break;
            }
        case 3:          //汇总
            {
                use.Gather( str);
                break;
            }
        case 4:       //修改
            {
                use.Sort(str);
                break;
            }
        case 5:    //删除
            use.Search(str);
            break;
        case 6:    //显示
            use.display(str);
            break;
        case 7:    //文件导入
            use.Fileinto(str);
            break;
        case 8:        //文件导出
            use.Fileout(str);
            break;
        case 9:
            exit(0);

        default: cout << "输入错误,请重新输入 : "; break;
        }
    }
    return 0;
}
void Function:: ShowMenu()
{
    cout << " 请选择下面的菜单 :" << endl;
    cout << " 1 创建" << endl;//创建
    cout << " 2 添加" << endl;
    cout << " 3 汇总" << endl;
    cout << " 4 排序" << endl;
    cout << " 5 查询" << endl;
    cout << " 6 显示" << endl;
    cout << " 7 文件导入" << endl;
    cout << " 8 文件导出" << endl;
    cout << " 9 退出" << endl;
    cout << " 请选择 :" ;//显示菜单
}
void Function:: CreatList(Student str[])//创建
{
    int i,n;
    cout << "输入创建的学生的个数" << endl;
    cin >> n;
    cout << "请输入信息" << endl;
    for (i = 0; i < n; i++)
    {
        cin >> str[i].id >> str[i].name >> str[i].math >> str[i].Chinese >> str[i].English ;
        str[i].sum=str[i].math+str[i].English+str[i].Chinese;
        str[i].aver=str[i].sum/3;
        num++;
    }
    cout << "学生信息: " << endl;
    for (i = 0; i < num; i++)
    {
        cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English << endl;
    }
}
void Function:: AddStu(Student str[])//添加
{
    cout<<"最后追加or指定位置添加?(1/2)";
    int m;
    cin>>m;
    if(m==1)
    {
        cout << "添加的通讯者的数目 : ";
        int x;
        cin >> x;
        cout << "请输入" << endl;
        for (int i = num; i < num + x; i++)
        {
            cin >> str[i].id >> str[i].name >> str[i].math >> str[i].Chinese >> str[i].English ;
            str[i].sum=str[i].math+str[i].English+str[i].Chinese;
            str[i].aver=str[i].sum/3;
        }
        cout << "以下是你输入的信息: " << endl;
        for (int i = num; i < num + x; i++)
        {
            cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<< endl;
        }
        num += x;
    }
    if(m==2)
    {
        cout << "添加的通讯者的数目 : ";
        int n,place;
        cin >> n;
        cout<<"请输入指定位置";
        cin>>place;
        for(int i=place;i<place+n;i++)
        {
            int id;
            string name;
            int math;
            int Chinese;
            int English;
            str[i+n].id=str[i].id;
            str[i+n].name=str[i].name;
            str[i+n].math=str[i].math;
            str[i+n].English=str[i].English;
            cout<<"请输入信息"<<endl;
            cin >> num >> name >> math >> Chinese >> English ;
            str[i].id=id;
            str[i].name=name;
            str[i].math=math;
            str[i].English=English;
        }
        cout << "以下是你输入的信息: " << endl;
        for (int i = place; i < place+n; i++)
        {
            cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<< endl;
        }
        num+=n;
    }
}
void Function:: Gather(Student str[])//汇总
{
    for (int i = 0; i < num; i++)
    {
        str[i].sum=str[i].math+str[i].English+str[i].Chinese;
        str[i].aver=str[i].sum/3;
        cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
    }
}
void Function:: Sort(Student str[])//排序
{
    for(int i=0;i<num;i++)
    {
        sort(str,str+num,Compare);
    }
    for(int j=0;j<num;j++)
    {
        cout << str[j].id << " " << str[j].name << " " << str[j].math << " " << str[j].Chinese << " " << str[j].English<<" "<<str[j].sum<<" "<<str[j].aver<< endl;
    }
}
bool Function:: Compare(Student p, Student q)
{
    return p.sum>q.sum;
}
void Search(Student str[])//查询
{
    int m(0),  flag(0);

    int ID;

    cout << "请输入所查询的学号 :";
    cin >> ID ;
    for (int i = 0; i < num; i++)
    {
        if (str[i].id == ID )
        {
            cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        cout << "未找到所查学号,请重新查询 " << endl;

}

void Function:: display(Student str[])//显示
{
    for (int i = 0; i < num; i++)
    {
        cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
    }
}
void Function:: Fileinto(Student str[])
{
    ifstream daoru("学生成绩.txt", ios::in);
    if (!daoru)
    {
        cout << "文件导入失败" << endl;
        exit(0);
    }

    cout << "请输入导入学生个数 : ";
    int n, i, j;
    cin >> n;
    for (j = 0; j < n; j++)
    {
        daoru >> str[i].id >> str[i].name >> str[i].math >> str[i].Chinese >> str[i].English ;
    }

    daoru.close();
    cout << "导入结束" << endl;
    system("pause");
    system("cls");

}
void Function:: Fileout(Student str[])
{
    ofstream daochu("f_1.txt", ios::out);
    if (!daochu)
    {
        cout << "文件导出失败" << endl;
        exit(0);
    }


    for (int i = 0; i < num; i++)
    {
        cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
    }
    daochu.close();
    cout<<"成功导出"<<endl;
    system("pause");
    system("cls");

}

  • 写回答

1条回答 默认 最新

  • baidu_30233079 2021-08-28 16:12
    关注

    有两种方法:
    1 把compare放到类外面
    2 重载运算符<

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月5日
  • 已采纳回答 8月28日
  • 创建了问题 8月28日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?