4-2. 1~100까지의 정숫값 제곱을 계산하는 프로그램을 작성해보세요. 이 프로그램은 2개 열을 출력해야 합니다. 첫 번째 열은 값을 나열하고 두 번째 열은 해당 값을 제곱한 결과를 나열합니다. 값들을 열에 맞춰 정렬시키는 데는 setw 함수를 사용하도록 출력을 처리해보세요.
#include <iostream> | |
#include <iomanip> | |
const int MAX = 100; | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::setw; | |
int main(void) | |
{ | |
for (int i = 1; i <= MAX; i++) | |
{ | |
cout << i << setw(7); | |
} | |
cout << endl << setw(0); | |
for (int i = 1; i <= MAX; i++) | |
{ | |
cout << i * i << setw(7); | |
} | |
cout << endl; | |
return 0; | |
} |

4-3. [연습문제 4-2]에서 다룬 프로그램을 수정해 1000까지의 제곱값을 계산한다고 생각해봅시다. 이 때 setw 함수의 인수가 제대로 변경되지 않으면 어떤 상황이 일어날까요? setw 함수의 인수 대신에 임의의 변수를 사용해 열에 맞춰 정렬시키는 방법을 사용해보세요.
#include <iostream> | |
#include <iomanip> | |
#include <cmath> | |
const int MAX = 1000; | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::setw; | |
int main(void) | |
{ | |
/* | |
setw 함수의 인수가 제대로 변경되지 않으면 | |
공백이 무시될 수 있음 | |
*/ | |
const int pad = log10(MAX * MAX) + 2; | |
for (int i = 1; i <= MAX; i++) | |
{ | |
cout << i << setw(pad); | |
} | |
cout << endl << setw(0); | |
for (int i = 1; i <= MAX; i++) | |
{ | |
cout << i * i << setw(pad); | |
} | |
cout << endl; | |
return 0; | |
} |

4-4. 여러분이 작성한 제곱 값을 구하는 프로그램에서 int 값 대신 double 값을 사용하도록 바꿔보세요. 또한 값들을 열에 맞춰 정렬시킬 때 조작어를 사용하도록 출력을 처리해보세요.(? 이 부분은 살짝 이해 안갑니다)
#include <iostream> | |
#include <iomanip> | |
#include <cmath> | |
const double MAX = 10.0; | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::setw; | |
using std::setprecision; | |
int main(void) | |
{ | |
cout << setprecision(3); | |
for (double i = 1; i <= MAX; i += 0.1) | |
{ | |
cout << i << setw(6); | |
} | |
cout << endl << setw(0); | |
for (double i = 1; i <= MAX; i += 0.1) | |
{ | |
cout << i * i << setw(6); | |
} | |
cout << endl; | |
return 0; | |
} |

4-5. 입력 스트림에서 단어들을 읽어 벡터에 저장하는 함수를 작성해보세요. 이 함수를 사용하여 입력한 단어의 개수를 세는 프로그램을 작성해보고 각 단어가 몇 번이나 등장하는지 계산해보세요.
#include <vector> | |
#include <iostream> | |
#include <istream> | |
#include <string> | |
#include <algorithm> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::istream; | |
using std::vector; | |
using std::string; | |
using std::sort; | |
istream &readWords(istream &in, vector<string> &words) | |
{ | |
if (in) | |
{ | |
// 이전 내용 제거 | |
words.clear(); | |
string s; | |
while (in >> s) | |
{ | |
if (s == "finish") | |
{ | |
break; | |
} | |
words.push_back(s); | |
} | |
// 다음 작업을 고려해 스트림을 지움 | |
in.clear(); | |
} | |
return in; | |
} | |
int main(void) | |
{ | |
vector<string> words; | |
readWords(cin, words); | |
sort(words.begin(), words.end()); | |
cout << "입력한 단어의 수: " << words.size() << endl; | |
string temp = words[0]; | |
int cnt = 1; | |
for (vector<string>::size_type i = 1; i < words.size(); i++) | |
{ | |
if (temp == words[i]) | |
{ | |
cnt++; | |
continue; | |
} | |
cout << temp << " : " << cnt << "번" << endl; | |
temp = words[i]; | |
cnt = 1; | |
} | |
cout << temp << " : " << cnt << "번" << endl; | |
return 0; | |
} |

4-6. 이 장에서 살펴본 프로그램의 학생 입력을 읽는 과정에서 학생 각각의 점수들을 계산하고 최종 점수만 저장하도록 Student_info 구조체, read 함수, grade 함수를 다시 작성해보세요.
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <istream> | |
#include <stdexcept> | |
#include <algorithm> | |
using std::istream; | |
using std::vector; | |
using std::domain_error; | |
using std::sort; | |
struct StudentInfo | |
{ | |
std::string name; | |
double midtermExam, finalExam; | |
double finalGrade; | |
}; | |
istream &read(istream &is, StudentInfo &studentInfo) | |
{ | |
is >> studentInfo.name >> studentInfo.midtermExam >> studentInfo.finalExam; | |
vector<double> homeworks; | |
readHomeworks(is, homeworks); | |
studentInfo.finalGrade = grade(studentInfo.midtermExam, studentInfo.finalExam, homeworks); | |
return is; | |
} | |
istream &readHomeworks(istream &in, vector<double> &homeworks) | |
{ | |
// 앞으로 완성시켜야 할 부분 | |
if (in) | |
{ | |
// 이전 내용 제거 | |
homeworks.clear(); | |
double x; | |
while (in >> x) | |
{ | |
homeworks.push_back(x); | |
} | |
// 다음 학생의 점수 입력 작업을 고려해 스트림을 지움 | |
in.clear(); | |
} | |
return in; | |
} | |
double grade(double midtermExam, double finalExam, double homework) | |
{ | |
return 0.2*midtermExam + 0.4*finalExam + 0.4*homework; | |
} | |
double grade(double midtermExam, double finalExam, const vector<double> &homeworks) | |
{ | |
if (homeworks.size() == 0) | |
{ | |
throw domain_error("student has done no homework"); | |
} | |
return grade(midtermExam, finalExam, median(homeworks)); | |
} | |
double median(vector<double> v) | |
{ | |
vector<double>::size_type size = v.size(); | |
if (size == 0) | |
{ | |
throw domain_error("median of an empty vector"); | |
} | |
sort(v.begin(), v.end()); | |
vector<double>::size_type mid = size / 2; | |
return size % 2 == 0 ? (v[mid] + v[mid - 1]) / 2 : v[mid]; | |
} |
4-7. vector<double> 객체에 저장된 수들의 평균을 계산하는 프로그램을 작성해보세요.
#include <iostream> | |
#include <vector> | |
#include <iomanip> | |
#include <stdexcept> | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
using std::vector; | |
using std::setprecision; | |
using std::domain_error; | |
double getAverage(const vector<double> &v) | |
{ | |
if (v.size() == 0) | |
{ | |
throw domain_error("벡터의 크기는 1 이상이여야 합니다."); | |
} | |
double sum = 0.0; | |
int cnt = 0; | |
for (vector<double>::size_type i = 0; i < v.size(); i++) | |
{ | |
sum += v[i]; | |
cnt++; | |
} | |
return sum / cnt; | |
} | |
int main(void) | |
{ | |
vector<double> v; | |
double x; | |
while (cin >> x) | |
{ | |
v.push_back(x); | |
} | |
try | |
{ | |
cout << "평균 : " << getAverage(v) << endl; | |
} | |
catch (domain_error e) | |
{ | |
cout << e.what(); | |
} | |
return 0; | |
} |

4-8. 다음 코드에서 오류가 발생하지 않는다면 f의 반환 타입을 어떻게 추측할 수 있을까요?
double 타입의 포인터를 반환한다는 것을 알 수 있습니다.
<부연 설명: https://www.daniweb.com/programming/software-development/threads/39569/is-this-notation-possible>
'C++ > Accelerated C++' 카테고리의 다른 글
Accelerated C++ 3장 연습문제 (0) | 2019.10.07 |
---|---|
Accelerated C++ 2장 연습문제 (0) | 2019.06.23 |