Chapter04
4-1
ํด๋น ๋ฌธ์ ๋ max์ ์ฌ์ฉ๋ฒ ๋ฐ ์ด๊ธฐํ์ ๋ํด ๋ฌผ์ด๋ณด๋ ๋ฌธ์ ์ด๋ค. ์ฐ์ Student_info๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋์ด์๋ค.
struct Student_info {
	std::string name;
	double midterm, final;
	std::vector<double> homework;
};
์ฐ์ , s.name์ string์ด๊ธฐ๋๋ฌธ์ s.name.size()๋ string::size_typeํ์ด๋ค. ๋ฐ๋ผ์ ์ฐ์  maxlen์ intํ์์ string::size_typeํ์ผ๋ก ๋ฐ๊ฟ์ ์ ์ธํ๋ค.
๊ทธ๋ฆฌ๊ณ , s ๋ฐ maxlen์ ์ด๊ธฐํ๊ฐ ์๋์ด์์ผ๋ฏ๋ก ํด๋น ๋ณ์๋ค์ ์ด๊ธฐํ๊น์ง ์งํํด์ผ max(s.name.size(), maxlen)์ด ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํ ๊ฒ์ด๋ค.
4-2, 4-3
setw๋ฅผ ์ฌ์ฉํ ์ ๊ณฑ์ ์ถ๋ ฅ ํ๋ก๊ทธ๋จ.
#include <iomanip>
/*undefined*/ setw (int n);
istream์ ํตํด ์ถ๋ ฅ๋ ๋ค์ ํ ์ค์ ๋๋น๊ฐ ์ต์ n์ด ๋๊ฒ ๋ง์ถฐ์ฃผ๋ ํจ์. ๋น์ฐํ printf๋ฑ์๋ ์ ์ฉ๋์ง ์๊ณ , ๋ค์ ํ ์ค์๋ง ์ ์ฉ๋๋ฉฐ, n์ค์ด์์ ์ ๋ ฅ์ธ ๊ฒฝ์ฐ ์ ๋ ฅ ๊ทธ๋๋ก ์ถ๋ ฅ๋๋ค.
int main() {
	std::cout << "0123456789 : ์ธ๋ฑ์ค" << std::endl;
	std::cout << std::setw(10);
	std::cout << "0123" << std::endl;
	std::cout << std::setw(10);
	std::cout << "0123\n";				// '\n'๊น์ง ํฌํจ ๋๋น 10
	std::cout << std::setw(10);
	std::cout << 3.141592 << std::endl;	// ๋ค๋ฅธ ํ๋ ๊ฐ๋ฅ
	std::cout << std::setw(10);
	std::cout << "01234567890123\n";
	return 0;
}
setw ํ ์คํธ
0123456789 : ์ธ๋ฑ์ค
      0123
     0123
   3.14159
01234567890123
#include <iostream>
#include <iomanip>
int main() {
	int i = 1, max = 1000, temp_max, digit = 1, max_width;
	std::string result;
	temp_max = max;
	while (temp_max != 1) {
		temp_max /= 10;
		++digit;
	}
	max_width = 4 * digit + 5;
	std::cout << max_width <<std::endl;
	while (i <= max) {
		result = std::to_string(i) + " * " + std::to_string(i)
				+ " = " + std::to_string(i * i);
		std::cout << std::setw(max_width);
		std::cout << result << std::endl;
		++i;
	}
	return 0;
}
4-4
int ๋์ ์ double์ ์ฌ์ฉํ๊ณ , ์กฐ์์ด()๋ฅผ ์ฌ์ฉ.
#include <iostream>
#include <iomanip>
int main() {
	double i = 1, max = 4;
	int temp_max;
	int digit = 1, max_width;
	std::streamsize precision = 5;
	std::string result;
	temp_max = (int)max;
	while (temp_max > 9) {
		temp_max /= 10;
		++digit;
	}
	max_width = precision + 1;
	std::cout << max_width <<std::endl;
	while (i <= max) {
		std::cout << std::setw(max_width);
		std::cout << std::setprecision(precision) <<i * i << '\n' << std::endl;
		i = i + 0.005;
	}
	return 0;
}
4-5
#include <algorithm>
#include <iostream>
#include <vector>
void prt_str(std::string str) {
	std::cout << str << " ";
}
int main() {
	std::string in, temp;
	std::vector<std::string> answer;
	char 	delimiter = ' ';
	while (getline(std::cin, temp, delimiter) != NULL) {
		answer.push_back(temp);
	}
	std::for_each(answer.begin(), answer.end(), prt_str);
}
4-6
pass
4-7
#include <iostream>
#include <vector>
#include <functional>
bool is_stoiable(std::string str) {
	size_t i;
	for (i = 0; i < str.size() ; i++) {
		if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
			return false;
	}
	return true;
}
int main() {
	std::string temp;
	std::vector<int> answer;
	int sum = 0, num;
	char 	delimiter = ' ';
	while (getline(std::cin, temp, delimiter) != NULL) {
		if (is_stoiable(temp)) {
			std::cout << temp << std::endl;
			num = stoi(temp);
			answer.push_back(num);
		}
	}
	for (int i = 0; i < answer.size(); i++) {
		sum += answer[i];
	}
	std::cout << sum << std::endl;
}
4-8
f๋ double์ด ๊ฐ์ฅ ์ ๋ ฅํ๋ค. ๊ทธ๋ฌ๋ ์ปดํ์ผ์ ํน์  ํ๋๊ทธ๋ค์ ์ค์ ํ์ง ์๋๋ค๋ฉด float, int ๋ฑ์ ์ ์ํ๋ ๊ฐ๋ฅํ ๊ฒ์ด๋ค.