๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[inflearn ๊ฐ•์˜] ๊ตฌ์กฐ์ฒด - ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•œ ํ•™์ƒ๊ด€๋ฆฌํ”„๋กœ๊ทธ๋žจ 2 ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/C++

[inflearn ๊ฐ•์˜] ๊ตฌ์กฐ์ฒด - ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•œ ํ•™์ƒ๊ด€๋ฆฌํ”„๋กœ๊ทธ๋žจ 2

์ง•์ง•์•ŒํŒŒ์นด 2023. 7. 4. 20:01
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ์–ด์†ŒํŠธ๋ฝ ๊ฒŒ์ž„์•„์นด๋ฐ๋ฏธ ๋‹˜์˜ ์œ ํŠœ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

=> C++ Let's Make Games

 

๐Ÿซง ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•œ ํ•™์ƒ๊ด€๋ฆฌํ”„๋กœ๊ทธ๋žจ 2

#include <iostream>
using namespace std;

#define NAME_SIZE		64
#define STUDENT_MAX		10
#define ADDRESS_SIZE	238
#define PHONE_SIZE		14

struct _tagStudent {
	char	strName[NAME_SIZE];
	char	strAddress[ADDRESS_SIZE];
	char	strPhoneNumber[PHONE_SIZE];
	int		iNumber;
	int		iKor;
	int		iEng;
	int		iMath;
	int		iTotal;
	float	fAvg;
};

enum MENU {
	MENU_NEXT,
	MENU_INSERT,
	MENU_DELETE,
	MENU_SEARCH,
	MENU_OUTPUT,
	MENU_EXIT
};

int main() {
	_tagStudent tStudentArr[STUDENT_MAX] = {};

	// ๋ฐฐ์—ด์— ์ถ”๊ฐ€๋œ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ
	int iStudentCount = 0;
	int iStdNumber = 0;

	char strName[NAME_SIZE] = {};

	while (true) {
		system("cls");

		// ๋ฉ”๋‰ด ์ถœ๋ ฅ
		cout << "1. ํ•™์ƒ๋“ฑ๋ก" << endl;
		cout << "2. ํ•™์ƒ์‚ญ์ œ" << endl;
		cout << "3. ํ•™์ƒํƒ์ƒ‰" << endl;
		cout << "4. ํ•™์ƒ์ถœ๋ ฅ" << endl;
		cout << "5. ์ข…๋ฃŒ" << endl;
		cout << "๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜์„ธ์š” : ";
		int iMenu;
		cin >> iMenu;

		//๋ฒ„ํผ : ์ž„์‹œ์ €์žฅ๊ณต๊ฐ„
		//cin ๋‚ด๋ถ€์— ์ž…๋ ฅ๋ฒ„ํผ๊ฐ€ ์žˆ๋Š”๋ฐ ์ž…๋ ฅ๋ฒ„ํผ๋Š” ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ 
		//๊ทธ ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋„ฃ์–ด์ฃผ๋Š” ์—ญํ• 
		//์—๋Ÿฌ ์ฒดํฌํ•˜๊ณ , cin.fail() ํ–ˆ์„ ๋•Œ ์ž…๋ ฅ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด true ๋ฐ˜ํ™˜
		if (cin.fail()) {
			// ์—๋Ÿฌ๋ฒ„ํผ ๋น„์šฐ๊ธฐ
			cin.clear();

			// ์ž…๋ ฅ๋ฒ„ํผ์— \n๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฏ€๋กœ ์ž…๋ ฅ๋ฒ„ํผ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ \n ์ง€์šฐ๊ธฐ
			// ์ฒซ๋ฒˆ์งธ๋Š” ๊ฒ€์ƒ‰ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฒ„ํผ ํฌ๊ธฐ ์ €์žฅ (๋„‰๋„‰ํ•˜๊ฒŒ 1024 byte)
			// ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž ๋„ฃ๊ธฐ
			cin.ignore(1024, '\n');
			continue;
		}

		if (iMenu == MENU_EXIT)
			break;

		switch (iMenu) {
		case MENU_INSERT:
			system("cls");
			cout << "#################### ํ•™์ƒ์ถ”๊ฐ€ ###################" << endl;
			// ์ตœ๋Œ€ ๋“ฑ๋ก์น˜ ๊นŒ์ง€๋งŒ ์ถ”๊ฐ€ํ•˜๊ธฐ
			if (iStudentCount == STUDENT_MAX)
				break;
			// ํ•™์ƒ ์ •๋ณด ์ถ”๊ฐ€
			// ์ด๋ฆ„, ์ฃผ์†Œ, ์ „ํ™”๋ฒˆํ˜ธ
			// ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์ ์ˆ˜๋Š” ์ž…๋ ฅ๋ฐ›๊ณ  ํ•™๋ฒˆ, ์ด์ , ํ‰๊ท ์€ ์—ฐ์‚ฐ
			// ์ด๋ฆ„ ์ž…๋ ฅ๋ฐ›๊ธฐ
			cout << "์ด๋ฆ„ : ";
			cin >> tStudentArr[iStudentCount].strName;

			cin.ignore(1024, '\n');
			cout << "์ฃผ์†Œ : ";
			cin.getline(tStudentArr[iStudentCount].strAddress, ADDRESS_SIZE);

			cout << "์ „ํ™”๋ฒˆํ˜ธ : ";
			cin.getline(tStudentArr[iStudentCount].strPhoneNumber, PHONE_SIZE);

			cout << "๊ตญ์–ด : ";
			cin >> tStudentArr[iStudentCount].iKor;

			cout << "์˜์–ด : ";
			cin >> tStudentArr[iStudentCount].iEng;

			cout << "์ˆ˜ํ•™ : ";
			cin >> tStudentArr[iStudentCount].iMath;

			tStudentArr[iStudentCount].iTotal =
				tStudentArr[iStudentCount].iKor +
				tStudentArr[iStudentCount].iEng +
				tStudentArr[iStudentCount].iMath;
			tStudentArr[iStudentCount].fAvg =
				tStudentArr[iStudentCount].iTotal / 3.f;
			tStudentArr[iStudentCount].iNumber = iStdNumber;

			++iStdNumber;
			++iStudentCount;

			cout << "ํ•™์ƒ ์ถ”๊ฐ€ ์™„๋ฃŒ" << endl;
			break;
		case MENU_DELETE:
			system("cls");
			cout << "#################### ํ•™์ƒ์‚ญ์ œ ###################" << endl;

			cin.ignore(1024, '\n');
			cout << "์‚ญ์ œํ•  ์ด๋ฆ„ ์ž…๋ ฅ : ";
			cin.getline(strName, NAME_SIZE);

			// ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ํ•™์ƒ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ํ•™์ƒ ์ฐพ๊ธฐ
			for (int i = 0; i < iStudentCount; i++) {
				// ํ•™์ƒ ์ฐพ์•˜๋‹ค๋ฉด
				if (strcmp(tStudentArr[i].strName, strName) == 0) {
					for (int j = 0; j < iStudentCount - 1; j++) {
						tStudentArr[i] = tStudentArr[i + 1];
					}
					--iStudentCount;

					cout << "ํ•™์ƒ ์‚ญ์ œํ•จ" << endl;
					break;
				}
			}
			break;
		case MENU_SEARCH:
			system("cls");
			cout << "#################### ํ•™์ƒํƒ์ƒ‰ ###################" << endl;
			
			cin.ignore(1024, '\n');
			cout << "ํƒ์ƒ‰ํ•  ์ด๋ฆ„ ์ž…๋ ฅ : ";
			cin.getline(strName, NAME_SIZE);

			// ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ํ•™์ƒ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ํ•™์ƒ ์ฐพ๊ธฐ
			for (int i = 0; i < iStudentCount; i++) {
				if (strcmp(tStudentArr[i].strName, strName) == 0) {
					cout << "์ด๋ฆ„ : " << tStudentArr[i].strName << endl;
					cout << "์ „ํ™”๋ฒˆํ˜ธ : " << tStudentArr[i].strPhoneNumber << endl;
					cout << "์ฃผ์†Œ : " << tStudentArr[i].strAddress << endl;
					cout << "ํ•™๋ฒˆ : " << tStudentArr[i].iNumber << endl;
					cout << "๊ตญ์–ด : " << tStudentArr[i].iKor << endl;
					cout << "์˜์–ด : " << tStudentArr[i].iEng << endl;
					cout << "์ˆ˜ํ•™ : " << tStudentArr[i].iMath << endl;
					cout << "์ด์  : " << tStudentArr[i].iTotal << endl;
					cout << "ํ‰๊ท  : " << tStudentArr[i].fAvg << endl;
				}
				break;
			}
		case MENU_OUTPUT:
			system("cls");
			cout << "#################### ํ•™์ƒ์ถœ๋ ฅ ###################" << endl;
			// ๋“ฑ๋ก๋œ ํ•™์ƒ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ถœ๋ ฅ
			for (int i = 0; i < iStudentCount; i++) {
				cout << "์ด๋ฆ„ : " << tStudentArr[i].strName << endl;
				cout << "์ „ํ™”๋ฒˆํ˜ธ : " << tStudentArr[i].strPhoneNumber << endl;
				cout << "์ฃผ์†Œ : " << tStudentArr[i].strAddress << endl;
				cout << "ํ•™๋ฒˆ : " << tStudentArr[i].iNumber << endl;
				cout << "๊ตญ์–ด : " << tStudentArr[i].iKor << endl;
				cout << "์˜์–ด : " << tStudentArr[i].iEng << endl;
				cout << "์ˆ˜ํ•™ : " << tStudentArr[i].iMath << endl;
				cout << "์ด์  : " << tStudentArr[i].iTotal << endl;
				cout << "ํ‰๊ท  : " << tStudentArr[i].fAvg << endl;
			}
			break;
		default:
			cout << "๋ฉ”๋‰ด๋ฅผ ์ž˜๋ชป ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค." << endl;
		}
		system("pause");
	}
	return 0;;
}

728x90
๋ฐ˜์‘ํ˜•
Comments