U E D R , A S I H C RSS

C 스터디_2005여름/학점계산프로그램/김태훈김상섭


1. class.h

~cpp
#ifndef CLASSH
#define CLASSH

#include <string>
#include <vector>
using namespace std;

class Score
{
public:
	Score() {};
	Score(string n, vector<double> s)
	{
		MAX_SUB = 4;
		setname(n);
		setscore(s);
		setgrade();
	}

	void setname(string n)
	{ 
		name = n;
	}

	void setscore(vector<double> s)
	{
		score = s;
	}

	void setgrade()
	{
		avg = 0.0;
		for(vector<double>::iterator i=score.begin();i !=score.end();++i)
		{
			avg += *i;
		}

		avg /= MAX_SUB;
	}

	string getname() { return name; }
	vector<double> getscore() { return score;}
	double getavg() { return avg;}
	

private:
	string name;
	vector<double> score;
	double avg;
	int MAX_SUB;
};



#endif

2. main.cpp

~cpp
#include <fstream>
#include <vector>
#include <algorithm>
#include <iostream>
#include "class.h"

using namespace std;
const int MAX_SUB = 4;

void getdata(vector<Score>& ban,const char* filename);
double changescore(string score);
bool zcompare(Score& a,Score& b);

int main()
{
	vector<Score> ban;
	getdata(ban,"input.txt");
	sort(ban.begin(),ban.end(),zcompare);

	
	for(vector<Score>::iterator i=ban.begin();i!=ban.begin()+ban.size()/10;++i)
	{
		cout << i->getname() << "\t" << i->getavg() << endl;
	}	
	
	cout << endl;
	for(i=ban.begin();i!=ban.end();++i)
	{
		if(i->getavg() < 1.5)
		{
			cout << i->getname() << "\t" << i->getavg() << endl;
		}
	}
	
	return 0;
}

void getdata(vector<Score>& ban,const char* filename){
	ifstream fin(filename);
	
	string tmp;
	string name;
	Score temp;

	vector<double> scoretmp;
	while(fin>>tmp) // 학생
	{
		fin >> name; // 이름
		fin >> tmp; // :
		
		for(int i = 0; i < MAX_SUB; i++)
		{
			fin >> tmp; 
			scoretmp.push_back(changescore(tmp));
		}
		temp = Score(name,scoretmp);
		ban.push_back(temp);
		scoretmp.clear();		
	}	
}
double changescore(string score)
{
	if(!score.compare("A+"))
		return 4.5;
	else if(!score.compare("A"))
		return 4.0;
	else if(!score.compare("B+"))
		return 3.5;
	else if(!score.compare("B"))
		return 3.0;
	else if(!score.compare("C+"))
		return 2.5;
	else if(!score.compare("C"))
		return 2.0;
	else if(!score.compare("D"))
		return 1.0;
	else if(!score.compare("F"))
		return 0.0;

	return 0.0;
}

bool zcompare(Score& a,Score& b)
{
	return a.getavg() > b.getavg();
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:44
Processing time 0.0254 sec