๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[inflearn ๊ฐ์] ๊ตฌ์กฐ์ฒด - ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์ฉํ ํ์๊ด๋ฆฌํ๋ก๊ทธ๋จ 2 ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/C++
[inflearn ๊ฐ์] ๊ตฌ์กฐ์ฒด - ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์ฉํ ํ์๊ด๋ฆฌํ๋ก๊ทธ๋จ 2
์ง์ง์ํ์นด 2023. 7. 4. 20:01728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ ์ด์ํธ๋ฝ ๊ฒ์์์นด๋ฐ๋ฏธ ๋์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> 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
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments