Accel_C++ Chapter04

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 ๋“ฑ์˜ ์ •์ˆ˜ํ˜•๋„ ๊ฐ€๋Šฅํ• ๊ฒƒ์ด๋‹ค.