Shiny Lime Green Star J, [C++] 7์ฃผ์ฐจ. ์ฝ”๋“œ ํ๋ฆ„๋„, ๋ฆฌ๋ทฐ

 

#include <iostream>
#include <string>
using namespace std;

const int ROWS = 10;
const int COLS = 6;

int seats[10][6];   // 10ํ–‰ 6์—ด์˜ ์ขŒ์„ ๋ฐฐ์—ด ์„ ์–ธ 
int adultcount = 0;
int childcount = 0;
int changecount = 0;
int totalprice = 0;

// ์ขŒ์„ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ //
void chogihwaseats() {
	for (int i = 0; i < 10; ++i) {
		for (int j = 0; j < 6; ++j) {
			seats[i][j] = '0';  // ๋ชจ๋“  ์ขŒ์„์„ '0'์œผ๋กœ ์ดˆ๊ธฐํ™” (๋นˆ ์ขŒ์„์„ ์˜๋ฏธ) 
		}
	}
}
// ํ˜„์žฌ ์ขŒ์„ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ 
void displayseats() {
	cout << "\n1 2 3 4 5 6\n";  // ์—ด ๋ฒˆํ˜ธ ์ถœ๋ ฅ 
	cout << "-----------\n\n";
	for (int i = 0; i < ROWS; ++i) {
		for (int j = 0; j < COLS; ++j) {
			cout << seats[i][j] << " ";  // ๊ฐ ์ขŒ์„์˜ ์ƒํƒœ ์ถœ๋ ฅ 
		}
		cout << endl;
	}
}


int main() {

	int choice;
	while (true) { // ๋ฌดํ•œ ๋ฐ˜๋ณต์„ ์œ„ํ•ด while์„ ์จ์„œ ๋ฐ˜๋ณต ์ถœ๋ ฅํ•œ๋‹ค 


		cout << "\n**์˜ํ™” ์˜ˆ์•ฝ ์‹œ์Šคํ…œ**" << endl << endl;
		cout << "1. ์ขŒ์„ ์˜ˆ์•ฝ" << endl;
		cout << "2. ์˜ˆ์•ฝ ๋ณ€๊ฒฝ" << endl;
		cout << "3. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ" << endl;
		cout << "๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ";
		cin >> choice;


		switch (choice) {
		case 1:
			displayseats(); // ํ˜„์žฌ ์ขŒ์„ ์ƒํƒœ ๋‚˜ํƒ€๋‚ด๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ 
			int currentadultcount, currentchildcount;
			cout << "\n์„ฑ์ธ(14000์›) : ";
			cin >> currentadultcount;
			cout << "\n์ฒญ์†Œ๋…„(11000์›) : ";
			cin >> currentchildcount;

			// ์ž…๋ ฅ๋œ ์„ฑ์ธ๊ณผ ์ฒญ์†Œ๋…„ ์ธ์› ์ˆ˜๋ฅผ ๋ˆ„์ ์‹œ์ผœ์ฃผ๋Š” ์ฝ”๋“œ๋‹ค 
			adultcount += currentadultcount;
			childcount += currentchildcount;

			// ์„ฑ์ธ์ด๋ž‘ ์ฒญ์†Œ๋…„ ์ธ์› ์ˆ˜๋ฅผ ์ด ๊ธˆ์•ก์— ๋ˆ„์  ์‹œํ‚จ๋‹ค 
			totalprice += currentadultcount * 14000 + currentchildcount * 11000;


			// ์„ฑ์ธ์ด๋ž‘ ์ฒญ์†Œ๋…„ ์ธ์› ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฐ’๋งŒํผ ์ขŒ์„ ์˜ˆ์•ฝ์„ ๋ฐ˜๋ณตํ•ด์ค€๋‹ค 
			for (int i = 0; i < currentadultcount + currentchildcount; ++i) {
				int col, row;
				cout << "\n๋ช‡์—ด, ๋ช‡๋ฒˆ์งธ ์ขŒ์„์„ ์˜ˆ์•ฝํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?\n";
				cin >> col >> row;

				// ์ž…๋ ฅ ๋ฐ›์€ ์ขŒ์„์ด ์œ ํšจํ•œ์ง€ ํ™•์ธํ•œ๋‹ค 
				if (row >= 1 && row <= ROWS && col >= 1 && col <= COLS) {
					// ์ขŒ์„์ด ์ด๋ฏธ ์˜ˆ์•ฝ๋œ ์ขŒ์„์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ 
					if (seats[row - 1][col - 1] == 0) {
						// ์ขŒ์„ ์˜ˆ์•ฝ ์„ฑ๊ณต ์‹œ ํ•ด๋‹น ์ขŒ์„์„ ์˜ˆ์•ฝ๋จ์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. 
						seats[row - 1][col - 1] = 1;
						cout << "\n์˜ˆ์•ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค." << endl;
					}
					else {
						cout << "\n์ด๋ฏธ ์˜ˆ์•ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž๋ฆฌ๋ฅผ ์„ ํƒํ•˜์„ธ์š”\n";
						--i;
					}
				}
				else {
					cout << "\n์ขŒ์„ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์„ ํƒํ•ด์ฃผ์„ธ์š”." << endl;
					--i; // ์ขŒ์„ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ, ๋‹ค์‹œ ์ž…๋ ฅ ๋ฐ›๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค๋ฅผ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. 
				}
			}
			break;
		case 2:
			displayseats();
			cout << "๋ฐ”๊ฟ€ ์ขŒ์„์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ";
			cin >> changecount;

			//๋ฐ”๊ฟ€ ์ขŒ์„์˜ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ์ขŒ์„ ๋ณ€๊ฒฝ์„ ์ง„ํ–‰ํ•˜๋Š” ์ฝ”๋“œ 
			for (int i = 0; i < changecount; ++i) {
				int currentRow, currentCol, changeRow, changeCol;
				cout << "\nํ˜„์žฌ ์ขŒ์„๊ณผ ๋ฐ”๊ฟ€ ์ขŒ์„์„ ์ž…๋ ฅํ•˜์„ธ์š”";
				cout << "\nํ˜„์žฌ ์ขŒ์„(N์—ด, M๋ฒˆ์งธ): ";
				cin >> currentCol >> currentRow;
				cout << "\n๋ณ€๊ฒฝ ์ขŒ์„(n์—ด, m๋ฒˆ์งธ):";
				cin >> changeCol >> changeRow;

				if (currentRow >= 1 && currentRow <= ROWS && currentCol >= 1 && currentCol <= COLS &&
					changeRow >= 1 && changeRow <= ROWS && changeCol >= 1 && changeCol <= COLS) {
					if (seats[currentRow - 1][currentCol - 1] == 1 && seats[changeRow - 1][changeCol - 1] == 0) {
						seats[currentRow - 1][currentCol - 1] = 0; // ํ˜„์žฌ ์ขŒ์„์„ ์ทจ์†Œ ์ฒ˜๋ฆฌ 
						seats[changeRow - 1][changeCol - 1] = 1;  // ๋ณ€๊ฒฝํ•  ์ขŒ์„์„ ์˜ˆ์•ฝ ์ฒ˜๋ฆฌ 
						cout << "์ขŒ์„ ๋ณ€๊ฒฝ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค." << endl;
					}
					else {
						cout << "์ด๋ฏธ ์˜ˆ์•ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ž๋ฆฌ๋ฅผ ์„ ํƒํ•˜์„ธ์š”" << endl;
						--i; // ๋‹ค์‹œ ์ขŒ์„์„ ์„ ํƒํ•˜๋„๋ก ๋ฐ˜๋ณต ์ธ๋ฑ์Šค๋ฅผ ์กฐ์ • 
					}
				}
				else {
					cout << "์ขŒ์„ ๋ฒˆํ˜ธ๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”." << endl;
					--i; // ์ขŒ์„ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ์„ ๊ฒฝ์šฐ ๋‹ค์‹œ ์ž…๋ ฅ ๋ฐ›๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต ์ธ๋ฑ์Šค๋ฅผ ์กฐ์ • 
				}
			}
			break;
		case 3:
		{
			int totalprice = adultcount * 14000 + childcount * 11000; // ์˜ˆ์•ฝํ•œ ์ธ์›์˜ ์ด ๊ธˆ์•ก์„ ๊ณ„์‚ฐ 
			cout << "์ด " << totalprice << "์› ์ž…๋‹ˆ๋‹ค." << endl; // ์ด ๊ธˆ์•ก์„ ์ถœ๋ ฅ 

			return 0; // ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ 
		}
		break;
		}
		displayseats();
	}
}

 

2์ฃผ์ฐจ ๊ณผ์ œ๋กœ ๋งŒ๋“  ์˜ํ™” ์ขŒ์„ ์˜ˆ๋งค ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์ด๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ๋ฆ„๋„๋ฅผ ๋งŒ๋“ค์–ด๋ดค๋‹ค.

 


# ์ฝ”๋“œ ๋ฆฌ๋ทฐ

 

์ง€์ˆ˜๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ๋ฆฌ๋ทฐ๋ฅผ ํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค.

๋ผ๋ฉด ๊ฐ€๊ฒŒ ์ฃผ๋ฌธ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์ด๋‹ค.

 

์ „์ฒด์ ์œผ๋กœ ์ฃผ์„์ด ๋‹ฌ๋ ค์žˆ์ง€ ์•Š์•„ ์–ด๋–ค ๋ถ€๋ถ„์ด ๋ฌด์Šจ ์—ญํ• ์„ ํ•˜๋Š”์ง€ ํ•œ๋ˆˆ์— ๋“ค์–ด์˜ค์ง€ ์•Š๋Š”๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ฃผ์„์„ ๋‹ฌ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ  clear๋ž‘ ignore์„ ์‚ฌ์šฉํ•ด์„œ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ์ž˜ ์ฒ˜๋ฆฌ ํ•ด์ฃผ์…จ๋Š”๋ฐ ์ฐพ์•„๋ณด๋‹ˆ INT_MAX๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊น”๋”ํžˆ ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ์ง€์šธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋‚˜๋„ ์ด๋ฒˆ์— ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์–ด ์ด ๋ถ€๋ถ„๋„ ์ˆ˜์ •์„ ํ•œ๋ฒˆ ํ•ด๋ดค๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ์ž˜๋ชป๋œ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ ์กฐ๊ฑด์—์„œ ๋งˆ์ด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ ํ•  ์ผ์€ ์ •๋ง ์—†๊ฒ ์ง€๋งŒ.. ใ…Žใ…Ž ํ˜น์‹œ๋‚˜ ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด 1๋ณด๋‹ค ์ž‘์€ ์กฐ๊ฑด๋„ ์ถ”๊ฐ€ํ•ด๋ดค๋‹ค.

nudle์ด๋ผ๋˜์ง€ ํ•จ์ˆ˜ ๋ช…์ด ์ง๊ด€์ ์ด์—ฌ์„œ ํ•œ๋ˆˆ์— ๋ณด๊ธฐ ํŽธํ–ˆ๊ณ  ์ˆ˜์ • ์ „ ์ฝ”๋“œ๋„ ์ถฉ๋ถ„ํžˆ ๊น”๋”ํ–ˆ๋˜ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

 

# ์ˆ˜์ • ์ „ ์ฝ”๋“œ

#include <iostream>
#include <typeinfo>
using namespace std;

int total(int* total_add, int cost) {
	*total_add = *total_add + cost;
	return *total_add;
}

int main() {
	string menu[] = { "์ผ๋ฐ˜ ๋ผ๋ฉด", "๊น€์น˜ ๋ผ๋ฉด", "์น˜์ฆˆ ๋ผ๋ฉด", "๋–ก ๋ผ๋ฉด", "๋–ก ๋งŒ๋‘ ๋ผ๋ฉด" };
	int prices[] = { 3500, 4000, 4200, 4300, 4500 };
	int total_cost = 0;

	while (true) {
		int nudle;
		cout << "***** ์Šˆ๋‹ˆ์˜ ๋ผ๋ฉด ๊ฐ€๊ฒŒ์— ์˜ค์‹  ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
		for (int i = 0; i < sizeof(menu) / sizeof(string); i++) {
			cout << i + 1 << ". " << menu[i] << "(" << prices[i] << ")" << endl;
		}
		cout << "๊ตฌ๋งคํ•˜์‹ค ๋ผ๋ฉด ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”(์ข…๋ฃŒ๋Š” 0): ";
		cin >> nudle;

		if (cin.fail()) {
			cout << "์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜์„ธ์š”.\n\n";
			cin.clear();
			cin.ignore();
			continue;
		}

		if (nudle == 0) {
			cout << "์ด ๊ฐ€๊ฒฉ์€ " << total_cost << "์› ์ž…๋‹ˆ๋‹ค.\n";
			cout << "์ฃผ๋ฌธ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n\n";
			break;
		}
		else if (nudle > 5) {
			cout << "์ œ๋Œ€๋กœ ๋œ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.";
			continue;
		}

		string flag;
		string flag_array[] = { "Y", "N" };
		int index = nudle - 1;
		cout << menu[index] << "์„(๋ฅผ) ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?(Y/N) ";
		cin >> flag;
		auto it = find(begin(flag_array), end(flag_array), flag);
		if (it == end(flag_array)) {
			cout << "Y/N ์ค‘์— ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n\n";
			continue;
		}
		if (flag == "N") {
			continue;
		}

		int count = 0;
		cout << "๊ตฌ๋งคํ•˜์‹ค ๋ผ๋ฉด ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š” : ";
		cin >> count;

		if (cin.fail()) {
			cout << "์ˆซ์ž๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. ์ดˆ๊ธฐํ™”๋ฉด์œผ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.\n\n";
			cin.clear();
			cin.ignore();
			continue;
		}

		cout << menu[index] << " " << count << "๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜์…จ์Šต๋‹ˆ๋‹ค.";
		int price = prices[index] * count;
		cout << "๊ฐ€๊ฒฉ์€ " << price << "์› ์ž…๋‹ˆ๋‹ค.\n\n";

		total(&total_cost, price);

	}


}

#์ˆ˜์ • ํ›„ ์ฝ”๋“œ

#include <iostream>
#include <typeinfo>
using namespace std;

// ์ด ๊ฐ€๊ฒฉ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•จ์ˆ˜
int total(int* total_add, int cost) {
    *total_add += cost;
    return *total_add;
}

int main() {
    string menu[] = { "์ผ๋ฐ˜ ๋ผ๋ฉด", "๊น€์น˜ ๋ผ๋ฉด", "์น˜์ฆˆ ๋ผ๋ฉด", "๋–ก ๋ผ๋ฉด", "๋–ก ๋งŒ๋‘ ๋ผ๋ฉด" };
    int prices[] = { 3500, 4000, 4200, 4300, 4500 };
    int total_cost = 0;

    while (true) {
        int nudle;
        // ์ดˆ๊ธฐ ๋ฉ”๋‰ด ์ถœ๋ ฅ
        cout << "***** ์Šˆ๋‹ˆ์˜ ๋ผ๋ฉด ๊ฐ€๊ฒŒ์— ์˜ค์‹  ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
        for (int i = 0; i < sizeof(menu) / sizeof(string); i++) {
            cout << i + 1 << ". " << menu[i] << " (" << prices[i] << "์›)" << endl;
        }
        cout << "๊ตฌ๋งคํ•˜์‹ค ๋ผ๋ฉด ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”(์ข…๋ฃŒ๋Š” 0): ";
        cin >> nudle;

        // ์ž…๋ ฅ์ด ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
        if (cin.fail()) {
            cout << "์ˆซ์ž๋กœ ์ž…๋ ฅํ•˜์„ธ์š”.\n\n";
            cin.clear();
            cin.ignore(INT_MAX, '\n'); // ์ž…๋ ฅ ๋ฒ„ํผ๋ฅผ ๊น”๋”ํžˆ ์ง€์›Œ์ฃผ๊ธฐ
            continue;
        }

        // ์ข…๋ฃŒ
        if (nudle == 0) {
            cout << "์ด ๊ฐ€๊ฒฉ์€ " << total_cost << "์› ์ž…๋‹ˆ๋‹ค.\n";
            cout << "์ฃผ๋ฌธ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n\n";
            break;
        }
        // ์ž˜๋ชป๋œ ๋ฒˆํ˜ธ ์ž…๋ ฅํ–ˆ์„ ๊ฒฝ์šฐ
        else if (nudle < 1 || nudle > 5) {
            cout << "์˜ฌ๋ฐ”๋ฅธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.\n\n";
            continue;
        }

        string flag;
        string flag_array[] = { "Y", "N" };
        int index = nudle - 1;
        cout << menu[index] << "์„(๋ฅผ) ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?(Y/N): ";
        cin >> flag;

        // Y/N ํ™•์ธ
        if (find(begin(flag_array), end(flag_array), flag) == end(flag_array)) {
            cout << "Y/N ์ค‘์— ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n\n";
            continue;
        }
        if (flag == "N") {
            continue;
        }

        int count = 0;
        cout << "๊ตฌ๋งคํ•˜์‹ค ๋ผ๋ฉด ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”: ";
        cin >> count;

        // ์ž…๋ ฅ์ด ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
        if (cin.fail()) {
            cout << "์ˆซ์ž๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. ์ดˆ๊ธฐํ™”๋ฉด์œผ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.\n\n";
            cin.clear();
            cin.ignore(INT_MAX, '\n');
            continue;
        }

        // ์ด ๊ฐ€๊ฒฉ ๋ฐ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ
        cout << menu[index] << " " << count << "๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜์…จ์Šต๋‹ˆ๋‹ค.\n";
        int price = prices[index] * count;
        cout << "๊ฐ€๊ฒฉ์€ " << price << "์› ์ž…๋‹ˆ๋‹ค.\n\n";

        // ์ด ๊ฐ€๊ฒฉ ์—…๋ฐ์ดํŠธ
        total(&total_cost, price);
    }
}

 


 

# ๊ณ„์‚ฐ๊ธฐ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ ๋ฆฌ๋ทฐ

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    while (true) {
        double a, b, r; int c; cout << "=============================\n  ๊ณตํ•™์šฉ ๊ณ„์‚ฐ๊ธฐ ํ”„๋กœ๊ทธ๋žจ\n=============================\n์‚ฌ์šฉํ•  ๊ธฐ๋Šฅ์„ ์„ ํƒํ•˜์„ธ์š”:\n1. ๋”ํ•˜๊ธฐ\n2. ๋นผ๊ธฐ\n3. ๊ณฑํ•˜๊ธฐ\n4. ๋‚˜๋ˆ„๊ธฐ\n5. ๊ฑฐ๋“ญ์ œ๊ณฑ\n6. ์ œ๊ณฑ๊ทผ\n7. ์‚ฌ์ธ\n8. ์ฝ”์‚ฌ์ธ\n9. ํƒ„์  ํŠธ\n"; cin >> c;
        if (c == 1 || c == 2 || c == 3 || c == 4 || c == 5) { cout << "์ฒซ ๋ฒˆ์งธ ์ˆซ์ž ์ž…๋ ฅ: "; cin >> a; cout << "๋‘ ๋ฒˆ์งธ ์ˆซ์ž ์ž…๋ ฅ: "; cin >> b; }
        else if (c == 6 || c == 7 || c == 8 || c == 9) { cout << "์ˆซ์ž ์ž…๋ ฅ: "; cin >> a; }

        switch (c) {
            case 1: r = a + b; break;
            case 2: r = a - b; break;
            case 3: r = a * b; break;
            case 4: if (b == 0) { cout << "0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n"; continue; } r = a / b; break;
            case 5: r = pow(a, b); break;
            case 6: if (a < 0) { cout << "์Œ์ˆ˜์˜ ์ œ๊ณฑ๊ทผ์€ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n"; continue; } r = sqrt(a); break;
            case 7: r = sin(a); break;
            case 8: r = cos(a); break;
            case 9: r = tan(a); break;
            default: cout << "์œ ํšจํ•˜์ง€ ์•Š์€ ์„ ํƒ์ž…๋‹ˆ๋‹ค.\n"; continue;
        }

        cout << "-----------------------------\n ๊ฒฐ๊ณผ: " << r << endl << "-----------------------------\n";
        char cont; cout << "๊ณ„์† ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y/n): "; cin >> cont;
        if (cont == 'n' || cont == 'N') break;
        cout << "=============================\n\n";
    }
    return 0;
}

์ œ๊ณต๋œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์šฐ์„  if๋ฌธ ์กฐ๊ฑด์ด ๊ฒฝ์šฐ์˜ ์ˆ˜ ํ•˜๋‚˜์”ฉ ์ž…๋ ฅํ•ด์„œ ๊ธธ์–ด์ง„ ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ „์ฒด์ ์œผ๋กœ ๋Œ€๊ด„ํ˜ธ ํ•˜๊ณ  ๋ฌธ๋‹จ์ด ์•ˆ๋‚˜๋ˆ ์ ธ ์žˆ์–ด์„œ ๊ฐ€๋กœ๋กœ ์ฝ”๋“œ๊ฐ€ ๊ธธ์ญ‰ํ•ด์ ธ์„œ ํ•œ๋ˆˆ์— ๋“ค์–ด์˜ค๋Š” ์ฝ”๋“œ๋Š” ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฃผ์„๋„ ์—†์–ด์„œ ๊ตฌ๋ถ„์„ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์ฃผ์„๋„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

์šฐ์„  if๋ฌธ ์กฐ๊ฑด์„ && ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ 1์ด๋ž‘ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๊ณ  5๋ž‘ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ์ˆซ์ž๋ฅผ ๋ฐ›๋Š” ์กฐ๊ฑด์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ๋ฐ‘์—๋„ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ•ด์คฌ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ๋‹จ๋„ ๋‚˜๋ˆ ์ค˜์„œ ๊ธธ์ด๊ฐ€ ํ›จ์”ฌ ์ค„์–ด๋“ค์–ด ํ•œ๋ˆˆ์— ๋ณด๊ธฐ ํŽธํ•ด์กŒ๋‹ค.

๊ทธ๋ฆฌ๊ณ  case๋ฅผ ๋ณด๊ณ  ๋‚˜๋Š” ํ•œ์ค„๋กœ ์žˆ๋Š”๊ฒŒ ๋” ๋ณด๊ธฐ ํŽธํ•œ๊ฑฐ ๊ฐ™์€๋ฐ ์‚ฌ๋žŒ๋งˆ๋‹ค ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ ๋†”๋‘์—ˆ๋‹ค.

์ฃผ์„์„ ๋‹ฌ๊ณ  if๋ฌธ ์กฐ๊ฑด ๊ณ ์น˜๊ธฐ๋งŒ ํ•ด์คฌ๋‹ค.

 

#์ˆ˜์ • ํ›„ ์ฝ”๋“œ

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    while (true) {
        double a, b, r;
        int c;

        // ๊ธฐ๋Šฅ ์ถœ๋ ฅ
        cout << "=============================\n๊ณตํ•™์šฉ ๊ณ„์‚ฐ๊ธฐ ํ”„๋กœ๊ทธ๋žจ\n=============================\n";
        cout << "์‚ฌ์šฉํ•  ๊ธฐ๋Šฅ์„ ์„ ํƒํ•˜์„ธ์š” : \n1.๋”ํ•˜๊ธฐ\n2.๋นผ๊ธฐ\n3.๊ณฑํ•˜๊ธฐ\n4.๋‚˜๋ˆ„๊ธฐ\n5.๊ฑฐ๋“ญ์ œ๊ณฑ\n6.์ œ๊ณฑ๊ทผ\n7.์‚ฌ์ธ\n8.์ฝ”์‚ฌ์ธ\n9.ํƒ„์  ํŠธ\n";
        cin >> c;

        // ๊ธฐ๋Šฅ ์ž…๋ ฅ ๋ฐ›๊ธฐ
        if (c >= 1 && c <= 5) {
            cout << "์ฒซ ๋ฒˆ์งธ ์ˆซ์ž ์ž…๋ ฅ: ";
            cin >> a;
            cout << "๋‘ ๋ฒˆ์งธ ์ˆซ์ž ์ž…๋ ฅ: ";
            cin >> b;
        }
        else if (c >= 6 && c <= 9) {
            cout << "์ˆซ์ž ์ž…๋ ฅ: ";
            cin >> a;
        }

        // ์ž…๋ ฅ ๋ฐ›์€ ์—ฐ์‚ฐ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰
        switch (c) {
        case 1: r = a + b; break;
        case 2: r = a - b; break;
        case 3: r = a * b; break;
        case 4: if (b == 0) { cout << "0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n"; continue; } r = a / b; break;
        case 5: r = pow(a, b); break;
        case 6: if (a < 0) { cout << "์Œ์ˆ˜์˜ ์ œ๊ณฑ๊ทผ์€ ๊ณ„์‚ฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n"; continue; } r = sqrt(a); break;
        case 7: r = sin(a); break;
        case 8: r = cos(a); break;
        case 9: r = tan(a); break;

        default:
            cout << "์œ ํšจํ•˜์ง€ ์•Š์€ ์„ ํƒ์ž…๋‹ˆ๋‹ค.\n";
            continue;
        }

        // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        cout << "-----------------------------\n ๊ฒฐ๊ณผ: " << r << endl << "-----------------------------\n";
        char cont;
        cout << "๊ณ„์† ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y/n): ";
        cin >> cont;
        if (cont == 'n' || cont == 'N') break;
        else if (cont == )
    }
    return 0;
}
user-img J
wogml
ํ˜„์žฌ๊ธ€
[C++] 7์ฃผ์ฐจ. ์ฝ”๋“œ ํ๋ฆ„๋„, ๋ฆฌ๋ทฐ
ํ•จ๊ป˜ ๋ณด๋ฉด ์ข‹์„ ํฌ์ŠคํŠธ