๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[inflearn ๊ฐ์] ๊ตฌ์กฐ์ฒด - ๊ตฌ์กฐ์ฒด์ ๋ฌธ์์ดํจ์ ๋ณธ๋ฌธ
[inflearn ๊ฐ์] ๊ตฌ์กฐ์ฒด - ๊ตฌ์กฐ์ฒด์ ๋ฌธ์์ดํจ์
์ง์ง์ํ์นด 2023. 7. 4. 00:37<๋ณธ ๋ธ๋ก๊ทธ๋ ์ด์ํธ๋ฝ ๊ฒ์์์นด๋ฐ๋ฏธ ๋์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> C++ Let's Make Games
๐ซง ๊ตฌ์กฐ์ฒด์ ๋ฌธ์์ดํจ์
๊ตฌ์กฐ์ฒด : ๊ด๋ จ์๋ ๋ณ์๋ค์ ๋ชจ์์ ํ๋์ ์๋ก์ด ํ์ ์ ๋ง๋ค์ด์ฃผ๋ ๊ธฐ๋ฅ, ์ฌ์ฉ์ ์ ์ ๋ณ์ ํ์
struct ๊ตฌ์กฐ์ฒด๋ช
{};
๋ฐฐ์ด๊ณผ ๊ตฌ์กฐ์ฒด์ ๊ณตํต์
1. ๋ฐ์ดํฐ ์งํฉ
2. ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ญ์ ํ ๋น๋จ, ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ๋ค์ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ญ์ผ๋ก ์กํ
๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ ๊ทผํ ๋ .์ด์ฉํด์ ์ ๊ทผ
tStudent.iKor = 100;
strcpy_s ํจ์
strcpy_s(tStudent.strName, "๊ฐ๋๋ค๋ผ abcd");
: ๋ฌธ์์ด ๋ณต์ฌํ๊ธฐ
: ์ค๋ฅธ์ชฝ์ ์๋ ๋ฌธ์์ด์ ์ผ์ชฝ์ผ๋ก ๋ณต์ฌ
: ๋ฌธ์์ด์ ๋์ ํญ์ 0(null)๋ก ๋๋์ผ ํ๋ค
: ๊ฐ ๋ฐฐ์ด์์์ ๊ฐ ๋ฃ์ด์ฃผ๋ฉด ๊ทธ ๊ฐ ์ถ๋ ฅ
: ๋ฃ์ด์ฃผ์ง ์์ ๊ฐ ๋ถ๋ถ์ ์ฐ๋ ๊ธฐ๊ฐ ์ถ๋ ฅ
: strcpy_s ๋ ์๋์ผ๋ก ๋์ ํญ์ 0(null) ๋ฃ์ด์ค
strcat_s ํจ์
strcat_s(tStudent.strName, "๋ฌธ์์ด ๋ถ์ด๊ธฐ");
: ๋ฌธ์์ด์ ๋ถ์ฌ์ฃผ๋ ๊ธฐ๋ฅ
strcmp ํจ์
strcmp(tStudent.strName, "ํ๊ธธ๋");
: ๋ ๋ฌธ์์ด์ ๋น๊ตํ์ฌ ๊ฐ์ ๊ฒฝ์ฐ 0 ๋ฐํ
: ๋ค๋ฅผ ๊ฒฝ์ฐ 0์ด ์๋ ๊ฐ ๋ฐํ
strlen ํจ์
strlen(tStudent.strName);
: ๋ฌธ์์ด ๊ธธ์ด
#include <iostream>
using namespace std;
#define NAME_SIZE 64
/*
๊ตฌ์กฐ์ฒด : ๊ด๋ จ์๋ ๋ณ์๋ค์ ๋ชจ์์ ํ๋์ ์๋ก์ด ํ์
์ ๋ง๋ค์ด์ฃผ๋ ๊ธฐ๋ฅ
์ฌ์ฉ์ ์ ์ ๋ณ์ ํ์
struct ๊ตฌ์กฐ์ฒด๋ช
{};
๋ฐฐ์ด๊ณผ ๊ตฌ์กฐ์ฒด์ ๊ณตํต์ :
1. ๋ฐ์ดํฐ ์งํฉ
2. ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ญ์ ํ ๋น๋จ
๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ๋ค์ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ญ์ผ๋ก ์กํ
*/
struct _tagStudent {
char strName[NAME_SIZE];
int iNumber;
int iKor;
int iEng;
int iMath;
int iTotal;
float fAvg;
};
int main() {
_tagStudent tStudent = {};
_tagStudent tStudentArr[100] = {};
// ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ ๊ทผํ ๋ .์ด์ฉํด์ ์ ๊ทผ
tStudent.iKor = 100;
// strcpy_s ํจ์ ์ด์ฉํด์ ๋ฌธ์์ด ๋ณต์ฌํ๊ธฐ
// ์ค๋ฅธ์ชฝ์ ์๋ ๋ฌธ์์ด์ ์ผ์ชฝ์ผ๋ก ๋ณต์ฌ
// ๋ฌธ์์ด์ ๋์ ํญ์ 0(null)๋ก ๋๋์ผ ํ๋ค
// ๊ฐ ๋ฐฐ์ด์์์ ๊ฐ ๋ฃ์ด์ฃผ๋ฉด ๊ทธ ๊ฐ ์ถ๋ ฅ
// ๋ฃ์ด์ฃผ์ง ์์ ๊ฐ ๋ถ๋ถ์ ์ฐ๋ ๊ธฐ๊ฐ ์ถ๋ ฅ
// strcpy_s ๋ ์๋์ผ๋ก ๋์ ํญ์ 0(null) ๋ฃ์ด์ค
strcpy_s(tStudent.strName, "๊ฐ๋๋ค๋ผ abcd");
// strcat_s ํจ์๋ ๋ฌธ์์ด์ ๋ถ์ฌ์ฃผ๋ ๊ธฐ๋ฅ
strcat_s(tStudent.strName, "๋ฌธ์์ด ๋ถ์ด๊ธฐ");
// strcmp ํจ์๋ ๋ ๋ฌธ์์ด์ ๋น๊ตํ์ฌ ๊ฐ์ ๊ฒฝ์ฐ 0 ๋ฐํ
// ๋ค๋ฅผ ๊ฒฝ์ฐ 0์ด ์๋ ๊ฐ ๋ฐํ
strcmp(tStudent.strName, "ํ๊ธธ๋");
// strlen ํจ์๋ ๋ฌธ์์ด ๊ธธ์ด
strlen(tStudent.strName);
cout << "๋น๊ตํ ์ด๋ฆ : ";
char strName[NAME_SIZE] = {};
cin >> strName;
strcpy_s(tStudent.strName, "๊ฐ๋");
if (strcmp(tStudent.strName, strName) == 0) {
cout << "ํ์ ์ฐพ์" << endl;
}
else {
cout << "ํ์ ์์" << endl;
}
cout << "์ด๋ฆ : " << tStudent.strName << endl;
cout << "ํ๋ฒ : " << tStudent.iNumber << endl;
cout << "๊ตญ์ด : " << tStudent.iKor << endl;
cout << "์์ด : " << tStudent.iEng << endl;
cout << "์ํ : " << tStudent.iMath << endl;
cout << "์ด์ : " << tStudent.iTotal << endl;
cout << "ํ๊ท : " << tStudent.fAvg << endl;
return 0;;
}