[ჸჸCPP]5.ᄏᄏᄏᄏᄏᄏ

22884 ワード

Created: June 6, 2021 11:25 AM
Tag: clear, do-while, fail, goto, ignore, random number, switch-case
5.3 switch-case
int main(void)
{
	int x;
	cin >> x;

	switch(x)
	{
	case 0:
		cout << "Zero";
		break; // 의도적으로 break를 넣지 않을 수도 있다
	case 1:
		cout << "One";
		break;
	case 2:
		cout << "Two";
		break;
	default: // else
		cout << "undefined input " << x << endl; // break 필요 없음
	}
	cout << endl;
	return (0);
}
5.4 goto
アセンブリ言語の観点から見ると、すべての複文はgoto文から構成されています.しかし現代では使われない文法です.
#include <iostream>
#include <cmath> // sqrt()

using namespace std;

int main(void)
{
	double x;

tryAgain : // label

	cout << "Enter a non-negative number" << endl;
	cin >> x;

	if (x < 0.0)
		goto tryAgain;

	cout << sqrt(x) << endl;
	return (0);
}
5.6再複文do-while
無条件に1回のループを実行し、論理演算により次のループを実行します.
#include <iostream>

using namespace std;

int main(void)
{
	int selection; // must be declared outside do/while loop
	do
	{
		cout << "1. add" << endl;
		cout << "2. sub" << endl;
		cout << "3. mult" << endl;
		cout << "4. div" << endl;
		cin >> selection;
	} while (selection <= 0 || selection >= 5);

	cout << "you selected " << selection << endl;

	return (0);
}
5.7乱数の生成
// 5.9 난수 만들기 random number generation
#include <iostream>
#include <cstdlib> // std::radn(), std::srand()
#include <ctime> // std::time()
#include <random>

using namespace std;

// 범위를 설정하여 난수를 얻는 함수
int getRandomNumber(int min, int max)
{
	static const double fraction = 1.0 / (RAND_MAX + 1.0);

	return (min + static_cast<int>((max - min + 1) * (std::rand() * fraction)));
}

int main(void)
{
	// std::srand(5323); // seed 설정
	std::srand(static_cast<unsigned int>(std::time(0))); // 시간에 따른 난수

	for (int count = 1; count <= 100; ++count)
	{
		// cout << std::rand() << "\t";
		cout << getRandomNumber(5, 8) << "\t";
		cout << rand() % 4 + 5 << "\t"; // 제수가 큰 경우 난수분포의 문제 발생
		if (count % 5 == 0) cout << endl;
	}

	// Random Library
	std::random_device rd;
	std::mt19937 mesenne(rd()); // create a mesenne twister
	std::uniform_int_distribution<> dice(1, 6); // uniform distribution

	return (0);
}
5.10 std::cinより良いignore()、clear()、fail()
#include <iostream>

using namespace std;

int getInt()
{
	cout << "enter an integer number : ";
	int x;
	cin >> x;
	while (1)
	{
		// int max 이상의 숫자가 입력되는 경우
		if (std::cin.fail())
		{
			// clear buffer
			std::cin.clear();
			// 사용자가 여러개의 숫자를 입력하는 경우 두번째 부터 무시
			std::cin.ignore(32767, '\n'); // 32767: 적당히 큰 숫자
			cout << "invalid number" << endl;
		}
		else
		{
			std::cin.ignore(32767, '\n');
			return (x);
		}
	}
}

char getOperator()
{
	while (1) // 제대로 입력 받을때까지 반복해서 입력받는다
	{
		cout << "enter an operator (+, -) : ";
		char op;
		cin >> op;
		std::cin.ignore(32767, '\n');
		if (op == '+' || op == '-')
			return (op);
		else
			cout << "invalid input" << endl;
	}
}

void printResult(int x, char op, int y)
{
	if (op == '+') cout << x + y << endl;
	else if (op == '-') cout << x - y << endl;
	else
	{
		cout << "invalid operator" << endl;
	}
}

int main(void)
{
	int x = getInt();
	char op = getOperator();
	int y = getInt();

	printResult(x, op, y);

	return (0);
}