编程实现简易的学生成绩管理系统。
(1)文件score.dat中存放了30名学生的序号和6次考试成绩,请从该文件中读取数据;
(2)计算每名学生的总分;
(3)输出总分最高和最低的学生序号及分数;
结果测试:
(1)文件score.dat中存放了30名学生的序号和6次考试成绩,请从该文件中读取数据;
(2)计算每名学生的总分;
(3)输出总分最高和最低的学生序号及分数;
(4)根据输入的学生序号,输出该生各次考试成绩和平均成绩;
各要求比较简单,我只实现读数据部分,将结果存在结构体中,也可以存在vector中。
代码如下:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Data //注:也可以使用vector来存数据
{
int A;
int B;
int C;
int D;
int E;
int F;
};
int StringToInt(string str)
{
int score = 0;
int i = 0;
while (str[i] != '\0')
{
score = score * 10 + str[i] - '0';
i++;
}
return score;
}
int main()
{
Data *student = new Data[30];
//Data **student =new Data*[30]; //如果使用指针数组来,但是不能这样赋值:student[0]->A=1;
string fileName = "score.dat";
ifstream in; //ifstream读文件(ofstream写文件)
in.open(fileName);
if (!in)
{
cout << "打开文件出错!" << endl;
return 1;
}
int temp;
int index = 0;
while (in >> temp)
{
in >> temp; //第一个序号不用
student[index].A = temp;
in >> temp;
student[index].B = temp;
in >> temp;
student[index].C = temp;
in >> temp;
student[index].D = temp;
in >> temp;
student[index].E = temp;
in >> temp;
student[index].F = temp;
index++;
}
for (int i = 0; i < 30; i++)
cout << student[i].A<<' '<<student[i].B<<' '<<student[i].C<<' '<<student[i].D<<' '<<student[i].E<<' '<<student[i].F<< endl;
delete[] student;
return 0;
}
结果测试: