[C++] 배열/2차원배열/구조체/멤버함수/입력/랜덤

2022. 6. 3. 17:43돌다리도 두드려보고 건너라 <복만살>

728x90
/* 학습목차
1. 배열
2. 2차원배열
3. 구조체
4. 멤버함수
5. 입력
6. 랜덤(난수생성)
*/


#include <iostream>

#include <stdlib.h>   //srand , rand
#include <time.h>   //time

using namespace std;

// 배열의 정의
// 같은 데이터타입의 집합 
// 크기와 주소를 같은 메모리상에서 연속적으로 배치를 시켜줌
// 배열의 시작주소를 알면 데이터타입의 크기를 통해 다른 원소들의 주소와 크기를 명확히 알수있음

void main()
{
	int scoreA;
	int scoreB;
	int scoreC;

	cout << &scoreA << endl;
	cout << &scoreB << endl;
	cout << &scoreC << endl;

	// 값 대칭 초기화방법1
	// int scores[3];
	// scores[0] = 10;
	// scores[1] = 20;
	// scores[2] = 30;
	
	// 값 대칭 초기화방법2
	int scores[] = { 10, 20, 30 }; 

	cout << "배열의 주소" << endl;
	cout << scores + 0 << endl;
	cout << scores + 1 << endl; // 배열 + 1의 의미는 시작주소에 int(4byte)만큼 더한다는 뜻 = 다음 원소의 주소
	cout << scores + 2 << endl;

	// 위와 아래는 동일한 결과를 얻는다
	cout << "배열의 주소" << endl;
	cout << &scores[0] << endl;
	cout << &scores[1] << endl;
	cout << &scores[2] << endl;

	// 배열의 원소들의 값
	cout << "배열의 주소들의 값" << endl;
	cout << *(scores + 0) << endl;
	cout << *(scores + 1) << endl; 
	cout << *(scores + 2) << endl;
}

// 2차원 배열
// 인덱스는 [] 안에있는 숫자를 의미하며 인덱스를 통해 내가 알고 싶은 원소의 크기와 주소를 구할 수 있다
void main()
{
	int scores[6] = { 10, 20, 30, 40, 50, 60 };
	int scores2[2][3] =
	{
		{10, 20, 30},
		{40, 50, 60}
	};

	cout << "배열의 주소" << endl;
	cout << scores2 + 0 << endl;
	cout << scores2[0] << endl;
	cout << scores2[0] + 1 << endl; // 4byte만큼 더한 주소
	cout << &scores2[0][0] << endl;

	cout << scores2 + 1 << endl; // 12byte만큼 더한 주소
	cout << scores2[1] << endl;
	cout << &scores2[1][1] << endl;

	// 배열의 원소들의 값
	cout << "배열의 주소들의 값" << endl;
	cout << scores2[0][0] << endl;	// 10
	cout << scores2[0][1] << endl;	// 20
	cout << *(*(scores2 + 0) + 1) << endl;	// 20 (위 코드가 더 직관적이여서 좋다)
	cout << *(*(scores2 + 1) + 1) << endl;	// 50
	cout << scores2[1][2] << endl;	// 60
}

// 구조체
// 일종의 '틀'
// 사용자 정의 데이터 타입
// 예시) struct Student
// {
// 타입의 속성들(멤버) 열거
// }
struct Monster
{
	string name;
	int hp;
	int atk;
};
// Monster printTarget;
// printTarget.name = monsterA.name; 
// printTarget.hp = monsterA.hp;
// printTarget.atk = monsterA.atk;
void PrintInfo(Monster printTarget)
{
	cout << "몬스터 이름 : " << printTarget.name << endl;
	cout << "체력 : " << printTarget.hp << endl;
	cout << "공격력 : " << printTarget.atk << endl;
}
// Monster* setTarget = &monsterA;
// setTarget->name = monsterA.name; 
// setTarget->hp = monsterA.hp;
// setTarget->atk = monsterA.atk;
// 0으로 초기화하는 과정
void SetDefaultInfo(Monster* setTarget)
{
	setTarget->name = " ";
	setTarget->hp = 0;
	setTarget->atk = 0;
}
void main()
{
	Monster monsterA;
	monsterA.name = "드래곤"; // 멤버들을 참조할때 '.'을 쓴다.
	monsterA.hp = 100;
	monsterA.atk = 10;

	PrintInfo(monsterA);
	SetDefaultInfo(&monsterA);
	PrintInfo(monsterA);

	Monster monsterB; // monsterA와는 서로 다르기 때문에 monsterB도 초기화 시켜야 한다
	monsterB.name = "슬라임";
	monsterB.hp = 10;
	monsterB.atk = 1;

	cout << monsterB.name << endl;
	cout << monsterB.hp << endl;
	cout << monsterB.atk << endl;
	
}


// 멤버함수
struct Monster
{
	//멤버 변수(속성)
	string name;
	int hp;
	int atk;

	//멤버 함수
	void PrintInfo()
	{
		cout << "멤버함수" << endl;
		cout << "몬스터 이름 : " << name << endl;
		cout << "체력 : " << hp << endl;
		cout << "공격력 : " << atk << endl;
	}
};

void main()
{
	Monster monsterA;
	monsterA.name = "드래곤";
	monsterA.hp = 100;
	monsterA.atk = 10;

	monsterA.PrintInfo(); // 멤버함수는 매개변수가 필요 없음

						  // 위와 아래는 동일한 결과를 얻는다
	PrintInfo(monsterA);
}


// 입력
struct Monster
{
	//멤버 변수(속성)
	string name;
	int hp;
	int atk;

	//멤버 함수
	void PrintInfo()
	{
		cout << "멤버함수" << endl;
		cout << "몬스터 이름 : " << name << endl;
		cout << "체력 : " << hp << endl;
		cout << "공격력 : " << atk << endl;
	}
};

void main()
{
	Monster monsterA;
	monsterA.name = "드래곤";
	monsterA.hp = 100;
	monsterA.atk = 10;

	Monster monsterB;

	cout << "이름 입력 : ";
	cin >> monsterB.name;
	cout << "체력 입력 : ";
	cin >> monsterB.hp;
	cout <<	"공격력 입력 : ";
	cin >> monsterB.atk;

	monsterB.PrintInfo();
}

// 랜덤
struct Monster
{
	//멤버 변수(속성)
	string name;
	int hp;
	int atk;

	//멤버 함수
	void PrintInfo()
	{
		cout << "몬스터 이름 : " << name << endl;
		cout << "체력 : " << hp << endl;
		cout << "공격력 : " << atk << endl;
	}
};

void main()
{
	srand(time(NULL));	// 시드값 변경을 위해 시간을 시드로 사용

	Monster monsterA;
	monsterA.name = "드래곤";
	monsterA.hp = 100;
	monsterA.atk = 10;

	Monster monsterB;

	cout << "이름 입력 : ";
	cin >> monsterB.name;
	cout << "체력 입력 : ";
	cin >> monsterB.hp;
	cout << "공격력 입력 : ";
	cin >> monsterB.atk;


	cout << rand() % 10 << endl;

	if (rand() % 2 == 0)
	{
		cout << "럭키찬스) 공격력에 추가 데미지를 더한다 (랜덤한 확률)" << endl;
		monsterB.atk += rand() % 10;
	}

	monsterB.PrintInfo();
}
728x90