Java vs Ruby vs C++: 성적표 프로그램 #2

Tags:

자바와 루비 버젼은 여기에, 이전에 C++로 코딩 했었던 코드는 여기에 있습니다.

C++ 경우만 고쳐봤습니다.

1. score_card.hpp

#ifndef __SCORE_CARD
#define __SCORE_CARD

#include <iostream>
#include <vector>

using namespace std;

class ScoreCard
{
    string _name;
    vector< pair<string, int> > _subjects;

public:
    ScoreCard(string name, vector< pair<string, int> > subjects):
        _name(name), _subjects(subjects) { }

    string getStudentName() const { return _name; }

    long getTotal() const;
    double getAverage() const;
    pair<string, int> findBestSubject() const;
};

#endif

2. score_card.cpp

#include
#include
#include
#include
#include
#include “score_card.hpp”

#define get_score(p) (bind(&pair::second, p))

using namespace std;
using namespace boost::lambda;

long ScoreCard::getTotal() const
{
return accumulate(_subjects.begin(), _subjects.end(), 0, _1 + get_score(_2));
}

double ScoreCard::getAverage() const
{
return static_cast(getTotal()) / _subjects.size();
}

pair ScoreCard::findBestSubject() const
{
return *max_element(_subjects.begin(), _subjects.end(), get_score(_1) < get_score(_2)); } [/code] 3. app.cpp [code lang="cpp"] #include
#include
#include “score_card.hpp”

using namespace std;

int main()
{
vector< pair > subjects;
subjects.push_back(make_pair(“Math”, 90));
subjects.push_back(make_pair(“English”, 86));
subjects.push_back(make_pair(“Physics”, 88));
subjects.push_back(make_pair(“Biology”, 97));

ScoreCard sCard(“철수”, subjects);

cout << sCard.getStudentName() << " 학생의 총점은 " << sCard.getTotal() << "입니다." << endl; cout << sCard.getStudentName() << " 학생의 평균점수는 " << sCard.getAverage() << "입니다." << endl; cout << "제일 잘한 과목은 " << sCard.findBestSubject().first << "로 " << sCard.findBestSubject().second << "입니다." << endl; return EXIT_SUCCESS; } [/code]