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