~cpp
#include <iostream>
#include <iomanip>
#include <string>
using std::cin;
using std::setprecision;
using std::streamsize;
using std::cout;
using std::string;
using std::endl;
int main() {
// ask for and read the students's name
cout << "Please enter your first name: ";
string name;
cin >> name;
const string greeting = "Hello, " + name + "!";
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << "Enter all your homework grades, "
"follewd by end-of-file: ";
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we hava read count grades so far, and
// sum is the sum of the first count grades
while(cin >> x) {
++count;
sum += x;
}
// write the result
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
~cpp cin >> a >> b; // 이 문장은 다음과 같다. cin >> a; cin >> b; // >> 연산자는 left operand를 리턴한다.
~cpp
string insu("insu");
// 요건 string형 변수 insu에 "insu"라는 문자열이 들어간다.
string insu;
// 요건 디폴트 생성자(그냥 넘어가자. 책에는 Default Initialization이라고 써있다.)에 의해 그냥 비어있게 된다.
int num = 0;
// num을 0으로 초기화해준다.
int num;
// num에는 무슨 값이 들어갈까? 책에는 undefined라고 써있다. 메모리에 있던 쓰레기값이 들어가게 된다.
// -8437535 이거 비슷한 이상한 숫자가 들어가게 되는걸 보게 될 것이다.
~cpp
// 다음과 같은 코드는 cin >> x를 만족할 동안 돌게 된다. 이게 무슨 말인지는 일단 넘어가자.(3.1.1)
while (cin >> x) {
++count;
// count에 1을 더한다.
sum += x;
// sum에 x를 더한다.
}
~cpp // 숫자의 정밀도를 조절해준다. setprecision(3); // 유효숫자는 3자리가 되고, 일반적으로 소숫점 앞의(정수부분의) 2자리, 소수보분의 1자리로 채워지게 된다.
~cpp if (cin >> x) ... // 이 문장은 다음과 같다. cin >> x; if(cin) ... // istream 내부의 복잡한 작업이 있긴 하지만 12장까진 몰라도 된다. 그냥 이것마 알아도 충분히 쓸수 있다.
~cpp
// 다음과 같은 코드를
int count = 0;
double sum = 0;
double x;
while(cin >> x) {
++count;
sum += x;
}
// 다음과 같은 코드로 바꿀수 있다.
vector<double> homework; // double값들을 저장할 vector
double x;
while(cin >> x) // while루프는 값들을 읽어들이면서 homework에 저장한다.
homework.push_back(x);
~cpp typedef vector<double>::size_type vec_sz; vec_sz size = homework.size();
~cpp
if(size == 0) {
cout << endl << "you must enter your grades. "
"Please try again." << endl;
return 1; // main함수가 0을 리턴하면 성공적으로 끝난것이고, 그 외의 숫자는 실패적으로 끝난것이다.
}
~cpp // sort the grades sort(homework.begin(),homework.end());
~cpp median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid]; // 개수가 홀수이면 딱 가운데꺼, 짝수개면 가운데 두개의 평균을 median 변수에 넣어준다.
~cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <ios>
// 원래 책에는 위 소스 처럼 각각 이름공간을 주었지만 이제부터 무난한 std로 쓰겠습니다.
using namespace std;
int main()
{
// ask for and read the students's name
cout << "Please enter your first name: ";
string name;
cin >> name;
const string greeting = "Hello, " + name + "!";
// ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << "Enter all your homework grades, "
"follewd by end-of-file: ";
vector<double> homework;
double x;
// invariant: homework contains all the homework grades read so far
while(cin >> x)
homework.push_back(x);
// check that the student entered some homework
// 바로 밑에 소스가 이상합니다.
// 원래 책에는 "typedef vector<double>::size_type vec_sz;" 이렇게 되어있지만
// 컴파일시 에러가 나서 같은 의미인 unsigned int형을 써서 vec_sz을 표현했습니다.
typedef unsigned int vec_sz;
vec_sz size = homework.size();
if(size == 0) {
cout << endl << "you must enter your grades. "
"Please try again." << endl;
return 1;
}
// sort the grades
sort(homework.begin(),homework.end());
// compute the median homework grade
vec_sz mid = size / 2;
double median;
median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid];
// compute and write the final grade
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * median
<< setprecision(prec) << endl;
return 0;
}