~cpp cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl;이렇게 생겼다. 둘째줄의 등급 계산하는 부분을 다음과 같이 함수로 뽑아낼 수가 있다.
~cpp double grade(double midterm, double final, double homework) { return 0.2 * midterm + 0.4 * final + 0.4 * homework; } /* ... */ int main() { /* ... */ cout << "Your final grade is " << setprecision(3) << grade(midterm, final, sum / count) << setprecision(prec) << endl; return 0; }
~cpp return_type function_name(parameter lists...) { 함수 내에서 할 일들 }이렇게 하면 된다.
~cpp double median(vector<double> vec) { typedef vector<double>::size_type vec_sz; vec_sz size = vec.size(); if (size == 0) throw domain_error("median of an empty vector."); sort(vec.begin(), vec.end()); vec_sz mid = size/2; return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid]; }
~cpp double grade(double midterm, double final, const vector<double>& hw) { if(hw.empty()) // 책에서는 hw.size()==0 이라고 되어 있지만 // empty()메소드를 호출하는 것이 더 효율적이라고 하더군요. throw domain_error("student has done no homework"); return grade(midterm, final, median(hw)); }
~cpp vector<double> homework; vector<double>& hw = homework; // hw는 homework와 같다. 즉, 이름은 다르지만, hw를 고치면 homework도 같이 고쳐진다. 왜냐? 같으니까
~cpp istream& read_hw(istream& in, vector<double>& hw) { double x; while(in >> x) // 차차 살펴볼테지만 이건 잘못되었다. hw.push_back(x); } read_hw(cin, homework); // 호출
~cpp istream& read_hw(istream& in, vector<double>& hw) { if(in) { hw.clear(); double x; while(in >> x) hw.push_back(x); in.clear(); } return in; }
~cpp try { // 하다가 예외 발생하면, 중단하고 } catch(domain_error) { // 이리로 온다. 만약에 try 안에서 예외 안 뜨면 catch 안은 수행안한다. }
~cpp #include <ios> #include <iomanip> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stdexcept> using namespace std; double grade(double midterm, double final, double homework); double grade(double midterm, double final, const vector<double>& hw); double median(vector<double> vec); istream& read_hw(istream& in, vector<double>& hw); int main() { cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!"; cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; cout << "Enter all your homework grades, " "follewd by end-of-file: "; vector<double> homework; read_hw(cin, homework); try { double final_grade = grade(midterm, final, homework); streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << final_grade << setprecision(prec) << endl; } catch(domain_error) { cout << endl << "You must enter your grades. " "Please try again." << endl; return 1; } return 0; } double grade(double midterm, double final, double homework) { return 0.2 * midterm + 0.4 * final + 0.4 * homework; } double grade(double midterm, double final, const vector<double>& hw) { if(hw.size() == 0) throw domain_error("Student has done no homework"); return grade(midterm, final, median(hw)); } double median(vector<double> vec) { typedef vector<double>::size_type vec_sz; vec_sz size = vec.size(); if(size == 0) throw domain_error("median of an empty vector"); sort(vec.begin(),vec.end()); vec_sz mid = size / 2; return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid]; } istream& read_hw(istream& in, vector<double>& hw) { if(in) { hw.clear(); double x; while(in >> x) hw.push_back(x); in.clear(); } return in; }
~cpp struct Student_info { string name; double midterm, final; vector<double> homework; };
~cpp istream& read(istream& is, Student_info& s) { is >> s.name >> s.midterm >> s.final; read_hw(is, s.homework); return is; }
~cpp double grade(const Student_info& s) { return grade(s.midterm, s.final, s.homework); }
~cpp sort(vec.begin(), vec.end());
~cpp sort(students.begin(), students.end()); // 과연? #!$%#@^#@$#
~cpp bool compare(const Student_info& x, const Student_info& y) { return x.name < y.name; } sort(students.begin(), students.end(), compare);
~cpp #ifndef GURAD #define GUARD /* ... ... ... */ #endif