U E D R , A S I H C RSS

CPP Study_2005_1/STL성적처리_1_class

No older revisions available

No older revisions available



CPPStudy_2005_1/STL성적처리_1_class

  • 01 남상협
  • 8/9

inputData

~cpp 
이름   국 영 수 과 
김철수 59 98 75 91 
마동탁 66 78 77 84 
박민철 52 50 63 72 
신순이 97 55 52 97 
송영이 78 82 63 73 
양민수 72 66 73 52 
장준구 95 62 94 53 
최호민 78 53 74 75 
태석호 66 82 82 94 
한노라 86 65 62 68 

code

Student.h

~cpp 
//Student.h
#ifndef GUARD_Student
#define GUARD_Student
#include <string>
#include <vector>
using namespace std;
class Student{
private:
	string m_name;
	vector<int> m_subjects;
	int m_total;
	double m_average;
public:
	Student();
	void setName(string name);
	string getName() {return m_name;};
	int getTotal() {return m_total;};
	double getAverage() {return m_average;};
	vector<int> getSubjectScore() {return m_subjects;};
	void addSubjectScore(int score);
	int sum();
	double average();
	void calculate();
};
#endif

Student.cpp

~cpp 
#include <algorithm>
#include <numeric>
#include "Student.h"
Student::Student()
{
	m_total=0;
	m_average=0.0;
}

void Student::setName(string name)
{
	m_name = name;
}
void Student::addSubjectScore(int score)
{
	m_subjects.push_back(score);
}
int Student::sum()
{
	return m_total=accumulate(m_subjects.begin(),m_subjects.end(),0);
}
double Student::average()
{
	return m_average=m_total/m_subjects.size();
}
void Student::calculate()
{
	sum();
	average();
}

ScoreProcess.h

~cpp 
#ifndef GUARD_ScoreProject
#define GUARD_ScoreProject
#include <vector>
#include <iostream>
#include <fstream>
#include "Student.h"
#define		SUBJECT_SIZE	4
class ScoreProcess {
private:
	vector<Student> &m_students;
	ifstream &m_fin;

public:
ScoreProcess(ifstream &fin,vector<Student> &students);
istream& readScores(istream &in);
ostream& displayScores(ostream &out);
void process();

};
bool totalCompare(Student &s1,Student &s2);

#endif


ScoreProcess.cpp

~cpp 
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include "ScoreProcess.h"
ScoreProcess::ScoreProcess(ifstream &fin,vector<Student> &students) : m_fin(fin),m_students(students)
{
}
istream& ScoreProcess::readScores(istream &in)
{

	string name, temp;
	int scoreTemp;
	vector<string> subject;
	m_fin>> name;
	while(m_fin>>temp && subject.size()!=SUBJECT_SIZE)
		subject.push_back(temp);
	do
	{
		Student *student = new Student();	
	
		student->setName(temp);
		
		for(vector<string>::const_iterator it = subject.begin() ; 
			it!=subject.end() && m_fin>>scoreTemp;++it)
				student->addSubjectScore(scoreTemp);
		student->calculate();
		m_students.push_back(*student);
	}while(m_fin>>temp);

	return in;
	
}

ostream& ScoreProcess::displayScores(ostream &out)
{
	for(vector<Student>::iterator it = m_students.begin() ; it!=m_students.end();++it)
	{
		out<<it->getName()<<"\t";
		vector<int>scores = it->getSubjectScore();
		for(vector<int>::const_iterator it2 = scores.begin();it2!=scores.end();++it2)
			out<<*it2<<"\t";
		out<<it->getTotal()<<"\t"<<it->getAverage()<<endl;		
	}
	return out;
}
void ScoreProcess::process()
{
	readScores(cin);
	sort(m_students.begin(),m_students.end(),::totalCompare);
	displayScores(cout);
}

bool totalCompare(Student &s1,Student &s2)
{
	return s1.getTotal()>s2.getTotal();
}


score.cpp (main)

~cpp 
#include <fstream>
#include <vector>
#include "ScoreProcess.h"
#include "Student.h"

int main()
{
	ifstream fin("score.txt");   
	vector<Student> students;
	ScoreProcess scoreProcess(fin,students);
	scoreProcess.process();
	return 0;
}

thread

  • C++ 클래스를 너무 오래간만에 만들었더니 생성자에 리턴값이 없다는것 조차 까먹었다. 황당 -_-;
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:42
Processing time 0.0354 sec