纯纯的小白dd 2023-03-17 18:09 采纳率: 70.6%
浏览 99
已结题

c++利用类与对象进行学生文件读写


#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstring>
using namespace std;
class CStudent {
private:
    char No[11], Name[7];
    int score[4];
    float ave;
public:
    CStudent() {}
    void SetData(char *NO,char *NAME,int one,int two,int three);
    void Display();
    float Average() {
        ave = score[3] / 3.0;
        return ave;
    }
    int GetScore_one() {
        return score[0];
    }
    int GetScore_two() {
        return score[1];
    }
    int GetScore_three() {
        return score[2];
    }
    int GetScore_four() {
        return score[3];
    }
};
void CStudent::SetData(char *NO,char *NAME,int one,int two,int three) {
    strcpy(No,NO);
    strcpy(Name,NAME);
    score[0]=one;
    score[1]=two;
    score[2]=three;
    score[3]=one+two+three;
}
void CStudent::Display() {
    cout << endl << endl << No << " " << Name << " " <<
        score[0] << " " << score[1] << " " <<
        score[2] << " " << Average()<<"  ";
}


class CStatistic {
private:
    int Nums;
    float Ave[4];
    CStudent* StuArray;
public:
    CStatistic();
    void Average();
    void Display_one();
    void Sort();
};
CStatistic::CStatistic() {
    FILE *fp;
    Nums=0;
    int k=0;
    char NO[11],NAME[7];
    int one,two,three;
    fp=fopen("D:\\dddd.txt","r");
    while(!feof(fp)){
        fscanf(fp,"%s %s %d %d %d",NO,NAME,&one,&two,&three);
        Nums++;
    }
    fclose(fp);
    StuArray=new CStudent[Nums+1];
    fp=fopen("D:\\dddd.txt","r");
    while(!feof(fp)){
        fscanf(fp,"%s %s %d %d %d",NO,NAME,&one,&two,&three);
        StuArray[k].SetData(NO,NAME,one,two,three);
        k++;
    }
    fclose(fp);
}
void CStatistic::Sort() {
    for (int i = 0; i < Nums; i++) {
        for (int j = 0; j < (Nums - i); j++) {
            if (StuArray[j].GetScore_four() < StuArray[j + 1].GetScore_four()) {
                StuArray[Nums] = StuArray[j];
                StuArray[j] = StuArray[j + 1];
                StuArray[j + 1] = StuArray[Nums];
            }
        }
    }

}
void CStatistic::Average() {
    for (int s = 0; s < 4; s++) {
        Ave[s] = 0;
    }
    for (int j = 0; j < Nums; j++) {
        Ave[0] += StuArray[j].GetScore_one();
        Ave[1] += StuArray[j].GetScore_two();
        Ave[2] += StuArray[j].GetScore_three();
        Ave[3] += StuArray[j].Average();
    }
}
void CStatistic::Display_one() {
    cout << "学号" << "姓名" << "语文" << "数学" << "英语" << "平均分" << endl;
    for (int i = 0; i < Nums; i++) {
        StuArray[i].Display();
        cout << i + 1 << endl;
    }
    cout << "总评" << Ave[0]/3.0  << " "
        << Ave[1] / 3.0 << " " << Ave[2] / 3.0 << " " << Ave[3] / 3.0 << endl;
}
int main() {
    CStatistic* pStatistic = new CStatistic();
    pStatistic->Sort();
    pStatistic->Average();
    pStatistic->Display_one();
    return 0;
}

做一个从文件中读取学生成绩的类组合,代码能运行,但点运行后就闪退,不知道哪里出了问题。其中读的文件是学号加姓名加三科成绩。

  • 写回答

6条回答 默认 最新

  • dahe0825 2023-03-17 18:17
    关注

    参考GPT和自己的思路,闪退的原因可能是因为以下几个问题:

    1.在进行文件读取时,需要判断文件是否成功打开,以及文件读取是否成功,否则可能导致程序崩溃。在代码中没有进行文件打开和读取的错误处理,可能会导致程序异常结束。

    2.在CStatistic类的构造函数中,动态分配内存时申请了Nums+1个CStudent对象,可能会导致数组越界,应该申请Nums个对象。

    3.在CStatistic类的Sort()函数中,冒泡排序的内部循环应该是j < (Nums - i - 1),而不是j < (Nums - i),否则可能会越界。

    4.在CStatistic类的Sort()函数中,交换数组元素的代码可能会导致越界访问,应该将StuArray[Nums]改为一个临时的CStudent对象。

    修改后的代码如下:

    #include<iostream>
    #include<iomanip>
    #include<cmath>
    #include<cstring>
    using namespace std;
    
    class CStudent {
    private:
        char No[11], Name[7];
        int score[4];
        float ave;
    public:
        CStudent() {}
        void SetData(char* NO, char* NAME, int one, int two, int three);
        void Display();
        float Average() {
            ave = score[3] / 3.0;
            return ave;
        }
        int GetScore_one() {
            return score[0];
        }
        int GetScore_two() {
            return score[1];
        }
        int GetScore_three() {
            return score[2];
        }
        int GetScore_four() {
            return score[3];
        }
    };
    
    void CStudent::SetData(char* NO, char* NAME, int one, int two, int three) {
        strcpy_s(No, NO);
        strcpy_s(Name, NAME);
        score[0] = one;
        score[1] = two;
        score[2] = three;
        score[3] = one + two + three;
    }
    
    void CStudent::Display() {
        cout << endl << endl << No << " " << Name << " " <<
            score[0] << " " << score[1] << " " <<
            score[2] << " " << Average() << "  ";
    }
    
    class CStatistic {
    private:
        int Nums;
        float Ave[4];
        CStudent* StuArray;
    public:
        CStatistic();
        void Average();
        void Display_one();
        void Sort();
        ~CStatistic() {
            delete[] StuArray;
        }
    };
    
    CStatistic::CStatistic() {
        FILE* fp;
        Nums = 0;
        int k = 0;
        char NO[11], NAME[7];
        int one, two, three;
    
        // 打开文件
        if (fopen_s(&fp, "D:\\dddd.txt", "r") != 0) {
            cout << "文件打开失败" << endl;
            exit(1);
        }
    
        // 计算学生人数
        while (!feof(fp)) {
            fscanf_s(fp, "%s %s %d %d %d", NO, 11, NAME, 7, &one, &two, &three);
            Nums++;
        }
    
        fclose(fp);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 3月25日
  • 已采纳回答 3月17日
  • 创建了问题 3月17日

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件