C, C++

20/02/09 C++공부

1can 2021. 2. 10. 10:39

상수형 데이터와 비트 연산, 

난수 발생 함수와 조건문

더보기

#include <iostream>
#include <time.h>
using namespace std;

int main() {
//상수 : 변하지 않는 수, 값을 한번 지정해놓으면 바꿀 수 없다.
// 상수는 선언과 동시에 값을 지정해두어야 한다.
const int iAttack = 0x00000001;
const int iArmor = 0x00000002;
const int iHP = 0x00000004;
const int iMP = 0x00000008;
const int iCritical = 0x00000010;

// 001 | 100 = 00101 | 10000 = 10101
int iBuf = iAttack | iHP | iCritical;

//연산자 축약형 : 연산자를 줄여서 사용할 수 있다.
//아래를 풀어서 쓰면 iBuf = iBuf ^ iHP;
//10101 ^ 00100 = 10001
iBuf ^= iHP;
// 10001 ^ 00100 = 10101
// XOR연산은 껐다 컸다 하는 스위치하는 기능 가능 
iBuf ^= iHP;
cout << "Attack : " << (iBuf & iAttack) << endl;
cout << "Armor : " << (iBuf & iArmor) << endl;
cout << "HP : " << (iBuf & iHP) << endl;
cout << "MP : " << (iBuf & iMP) << endl;
cout << "Critical : " << (iBuf & iCritical) << endl;

/*
쉬프트 연산자 : <<, >> 값 대 값을 연산 값으로 나옴
이진수 단위의 연산을 함
20 << 2 = 80
20 << 3 = 160
20을 2진수로 변환한다
10100 -> 1010000
*/
int iHigh = 187;
int iLow = 13560;

int iNumber = iHigh;
iNumber <<= 16;

iNumber |= iLow;

cout << "High : " << (iNumber >> 16) << endl;
cout << "Low : " << (iNumber & 0x0000ffff) << endl;

//증감 연산자 ++. --

/*
분기문 if, switch문
if(조건식) {코드 블럭}
*/
if (true) {
cout << "if문 조건이 true입니다." << endl;
}

//버프가 있는지 확인한다.
if ((iBuf & iAttack) != 0) {
cout << "Attack 버프가 있습니다." << endl;
}
//if문 아리에 들어갈 코드가 1줄일 경우 코드블럭 생략가능
if ((iBuf & iArmor) != 0)
cout << "Armor 버프가 있습니다." << endl;
if ((iBuf & iHP) != 0)
cout << " HP버프가 있습니다." << endl;
if ((iBuf & iMP) != 0)
cout << "MP버프가 있습니다. " << endl;
if ((iBuf & iCritical) != 0)
cout << "Critical버프가 있습니다. " << endl;


//난수 발생
//(unsigned int)는 형변환
srand((unsigned int)time(NULL));
cout << rand() << endl;
cout << rand() << endl;
cout << rand() << endl;
cout << (rand() % 101 + 100) << endl;
cout << (rand() % 10000 / 100.0f) << endl;
int iUpgrade = 0;
cout << "Upgrade 기본 수치를 입력하세요 : ";
cin >> iUpgrade;
//강화 확률을 구한다.
float fPercent = rand() % 10000 / 100.f;
// 강화 확률 : 업그레이드가 0 ~3 : 100% 성공 4~6 : 40% 7~9: 10%
// 10 ~13 : 1% 14~15: 0.01%
cout << "Upgrade : " << iUpgrade << endl;
cout << "Percent : " << fPercent << endl;

if (iUpgrade <= 3)
cout << "강화 성공" << endl;

else if (iUpgrade >= 4 && iUpgrade <= 6) {
if (fPercent < 40.f)
cout << "강화 성공" << endl;
else
cout << "강화 실패" << endl;
}

else if (iUpgrade >= 7 && iUpgrade <= 9) {
if (fPercent < 10.f)
cout << "강화 성공" << endl;
else
cout << "강화 실패" << endl;
}

else if (iUpgrade >= 10 && iUpgrade <= 13) {
if (fPercent < 1.f)
cout << "강화 성공" << endl;
else
cout << "강화 실패" << endl;
}

else if (iUpgrade >= 14 && iUpgrade <= 15) {
if (fPercent < 0.01f)
cout << "강화 성공" << endl;
else
cout << "강화 실패" << endl;
}

return 0;
}