hkk522793060 2022-05-13 11:51 采纳率: 100%
浏览 606
已结题

遇到std::bad_alloc异常,现在还不理解到底是为什么出错,要怎么处理

遇到std::bad_alloc异常,现在还不理解到底是为什么出错,要怎么处理,请高人指点了


Classone.txt文件内容
张一 20 165 50 1 85 87 90 69 96 87 90 88 64
张二 21 160 37 2 56 87 94 60 78 80 64 77 66
张三 22 159 55 3 86 69 77 66 83 65 75 45 80
张四 20 158 53 4 75 66 90 56 63 78 66 80 93
张五 22 175 60 5 90 85 80 96 74 68 67 87 91
张六 22 177 50 6 100 60 68 87 93 94 63 76 94
张七 21 185 95 7 65 71 84 70 68 77 82 65 69
张八 20 174 80 9 82 76 69 78 88 92 68 79 77
张九 20 177 58 9 85 87 90 86 95 86 90 89 93
张十 20 164 82 10 87 71 73 78 84 90 65 68 92


实现功能:
在void CTeacher::ReadInfo()函数中实现
从Classone.txt文件中读取学生信息,分别赋值CStudent classone[10]中的各个量。分别计算各个学生的平均成绩,并对学生的成绩进行排序(平均成绩高的为第一名),将排名的结果写入名为Sort.txt的文件(写入的内容必须包括学生的各个信息)。


应该是在冒泡排序那几句遇到std::bad_alloc异常,但找不到解决方法



#include<fstream>
#include<iostream>
#include<string>
using namespace std;
class CPerson
{
protected:
    string name;
    int age;
    int height;
    int weight;
public:
    CPerson();
    void ShowInfo();
    void Measure();
};
CPerson::CPerson() {
    height = 180;
    weight = 90;
    age = 40;
    name = "李四";
}
void CPerson::ShowInfo() {
    cout << "name:" << name;
    cout << "age:" << age;
    cout << "height:" << height;
    cout << "weight:" << weight << endl;
}
void CPerson::Measure() {
    float sta;
    sta = height - 105;
    if (weight < sta * 0.6)
        cout << "严重营养不良" << endl;
    else if (weight >= sta * 0.6 && weight < sta * 0.8)
        cout << "中度营养不良" << endl;
    else if (weight >= sta * 0.8 && weight < sta * 0.9)
        cout << "轻度营养不良" << endl;
    else if (weight >= sta * 0.9 && weight < sta * 1.1)
        cout << "正常范围" << endl;
    else if (weight > sta * 1.1)
        cout << "肥胖" << endl;
}
//class CStudent;
class CTeacher;
class CStudent :public CPerson {
protected:
    int num;
    int math;
    int chi;
    int geogra;
    int biolo;
    int chemis;
    int histo;
    int politi;
    int physi;
    float aver;
public:    
    friend CTeacher;
    void ShowInfo();
    CStudent();
    float operator()();
    //friend void CTeacher::SetScore(CStudent& p);
    //friend void CTeacher::ReadInfo();
};
class CTeacher :public CPerson {
protected:
    CStudent classone[10];
public:
    CTeacher();
    //void SetScore(CStudent& p);
    void ReadInfo();

};


//void CTeacher::SetScore(CStudent& p) {
//    cin>>p.math>>p.chi>>p.geogra>>p.biolo>>p.chemis>>p.histo>>p.politi>>p.physi;
//}

CTeacher::CTeacher(): classone() {
    height = 175;
    age = 52;
    name = "张老师";
    weight = 65;
    
}

CStudent::CStudent() {
    height = 0;
    weight = 0;
    age = 0;
    name = "0";
    num = 0;
    math = 0;
    chi = 0;
    geogra = 0;
    biolo = 0;
    chemis = 0;
    histo = 0;
    politi = 0;
    physi = 0;
}
void CStudent::ShowInfo() {
    CPerson::ShowInfo();
    cout << "num:" << num;
    cout << "math_score:" << math;
    cout << "chinese_score:" << chi;
    cout << "geogtaphy_score:" << geogra;
    cout << "biology_score:" << biolo;
    cout << "chemistry_score:" << chemis;
    cout << "history_score:" << histo;
    cout << "politics_score:" << politi;
    cout << "physics_score" << physi;
}
float CStudent::operator()() {
    float aver;
    aver = (math + chi + geogra + biolo + chemis + histo + politi + physi) / 8.0;
    return aver;
}
void CTeacher::ReadInfo()
{
    fstream file;
    file.open("Classone.txt", ios::in | ios::binary);
    if (!file.is_open()) {
        cerr << "error open" << endl;
        exit(1);
    }
    for (int i = 0; i < 10; i++)
        file.read((char*)&classone[i], sizeof(classone[i]));
    file.close();
    for (int i = 0; i < 10; i++)
        ((classone + i))->aver = (*(classone + i))();
    int j = 0;
    CStudent* t = new CStudent;
    for (int i = 0; i < 9; i++)
        for (j = 0; j < 9 - i; j++)
            if (classone[j].aver <  classone[j+1].aver) {
                *t = classone[j];
                classone[j] = classone[j+1];
                classone[j+1] = *t;
            }
    fstream file2;
    file2.open("Sort.txt", ios::out);
    for (int i = 0; i < 10; i++) {
        ((classone + i))->ShowInfo();
        file << "num:" << (classone + i)->num;
        file << "math_score:" << (classone + i)->math;
        file << "chinese_score:" << (classone + i)->chi;
        file << "geogtaphy_score:" << (classone + i)->geogra;
        file << "biology_score:" << (classone + i)->biolo;
        file << "chemistry_score:" << (classone + i)->chemis;
        file << "history_score:" << (classone + i)->histo;
        file << "politics_score:" << (classone + i)->politi;
        file << "physics_score" << (classone + i)->physi;
    }
    file2.close();

}



int main() {
    //CPerson LiSi;
    //LiSi.Measure();
    //CStudent Xiaoming;
    CTeacher zhang;
    //zhang.ShowInfo();
    //zhang.Measure();
    //zhang.SetScore(Xiaoming);
    //cout <<Xiaoming()<< endl;
    zhang.ReadInfo();
    return 0;
}
  • 写回答

3条回答 默认 最新

  • 关注

    file.read((char*)&classone[i], sizeof(classone[i])) -> 二进制方法读普通文本,没崩溃么。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月22日
  • 已采纳回答 5月14日
  • 赞助了问题酬金10元 5月13日
  • 创建了问题 5月13日

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码