我有一个数组保存学生数据,如姓名,组和点数。我想为每个组创建另一个数组,因此我可以分别计算每个组的平均点数。我怎样才能做到这一点? 我从一个文本文件中读取了所有学生的数据,所以我不知道我有多少不同的小组。
这是我的任务:
第一行数据文件包含教师的姓名。该 以下行包含学生信息:姓氏,姓名,组, 成绩,成绩。计算每个人的平均成绩 分开组。根据平均值打印结果 按字母顺序递减订单和组名。
这是我的Student.h
#pragma once
#include "Student.h"
class Faculty
{
public:
static const int Cmax = 100;
private:
Student St[Cmax];
int n;
string fname;
public:
Faculty(): n(0) {}
int Get() { return n; }
string GetN() { return fname; }
Student Get(int i) { return St[i]; }
void SetN(string name)
{ fname = name; }
void Set(const Student & ob)
{ St[n++] = ob; }
string GetGG(int i) { return St[i].GetG(); }
};
Faculty.h
#pragma once
#include <string>
using namespace std;
//-------------------------------
class Student
{
public:
static const int Cp = 10;
private:
string surname, name, group;
int nm, Ma[Cp];
public:
Student():nm(0), surname(""), name(""), group("") { }
void Set(string sn, string fn, string gr, int nm, int mr[]);
string Print();
bool operator ! ();
string GetG();
};
Student.cpp
#include "Student.h"
#include <sstream>
#include <iomanip>
using namespace std;
//--------------------------------
// Interface method
void Student::Set (string sn, string fn, string gr, int nm, int mr[])
{
surname = sn;
name = fn;
group = gr;
Student::nm = nm;
for (int i = 0; i < nm; i++)
Ma[i] = mr[i];
}
//--------------------------------
// Printing of values to line
string Student::Print()
{
stringstream ei;
ei << left << setw(12) << surname << setw(10) << name
<< setw(8) << group << right;
for (int i = 0; i < nm; i++)
ei << setw(3) << Ma[i];
ei << endl;
return ei.str();
}
//------------------------------
string Student::GetG() { return group; }
Faculty.cpp
#include "Faculty.h"
的main.cpp
#include <fstream>
#include <iomanip>
using namespace std;
#include "Faculty.h"
//---------------------------------------
const char CDfv[] = "U1.txt";
const char CRfv[] = "Results.txt";
//---------------------------------------
void Read(Faculty & R);
void Print(Faculty & R, const string & header);
//---------------------------------------
int main()
{
ofstream fr(CRfv);
fr.close();
Faculty F;
Read(F);
Print(F, "Initial data");
return 0;
}
//----------------------------------------
// Reading of data into object R
void Read(Faculty & R)
{
ifstream fd(CDfv);
bool yra = true;
string fname, sn, fn, gr;
int nm, mr[Student::Cp];
Student s1;
int ns = 0; // number of students
getline(fd, fname);
R.SetN(fname);
while (!fd.eof() && yra) { // while not end of file and there is room
getline(fd, sn, ','); // several words
fd >> ws; // white spaces
getline(fd, fn, ',');
fd >> ws;
getline(fd, gr, ',');
fd >> nm;
for (int i = 0; i < nm; i++)
fd >> mr[i];
fd.ignore();
s1.Set (sn, fn, gr, nm, mr);
if (ns - 1 < Faculty::Cmax)
R.Set(s1);
else
yra = false;
}
fd.close();
}
//----------------------------------------------------
// Printing of data of object R
void Print(Faculty & R, const string & header)
{
ofstream fr(CRfv, ios::app);
fr << setw(30) << header << endl;
fr << "Faculty: " << R.GetN() << endl;
fr << " Surname Name Group Grades \n";
fr << "------------------------------------------\n";
for (int i = 0; i < R.Get(); i++)
fr << R.Get(i).Print();
fr << "------------------------------------------\n\n\n";
fr.close();
}
//------------------------------------------------------
答案 0 :(得分:0)
您可以尝试使用
等数据结构struct student {
char* FirstName
char* LastName
int nMark
};
等。之后,您可以按任何类型进行排序,例如,通过标记:student.nMark