[C++] 출력/변수/지역성/조건문/연산자/반복문/함수/스택/포인터/배열

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

728x90

으갸야야야야야야야야야

/*학습목차
1. 출력
2. 변수 (캐스팅, 데이터타입의 역할과 크기)
3. 지역성 (메모리에 변수가 할당되고 해제되는 시점)
4. 조건문
5. 연산자 (연산자, 조건비교연산자, 연산자우선순위)
6. 반복문 (while, for, 다중반복)
7. 함수 (호출과 리턴, 리턴타입과 매개변수, 함수 오버로딩)
8. 스택의 메모리구조, stack overflow
9. 포인터 (포인터 변수, &와 *, call by value, call by adress)
10. 배열 (배열의 정의, sizeof)
*/



//꿀팁1 : Alt키 누르고 드래그 하면 드래그한 특정영역에 작성가능
//꿀팁2 : Alt키 누르고 위 아래로 방향키를 누르면 해당 줄을 이동시킬수있다
//꿀팁3 : Ctrl키+A 누르고 Ctrl+K+F를 누르면 자동정렬이 된다.
// 가속성의 시작은 변수명부터~ 변수명을 대충 지어쓰면 가속성이 떨어지고 좋지않다.

#include <iostream>		//출력과 같은 여러 기능들을 담은 라이브러리라고 생각하면 됨

using namespace std;	//나중에 배우게 될것

// 출력
void main()
{
	cout << "Hello World" << endl; //cout은 출력할 때 쓰임, endl은 줄바꿈
	cout << "Hello World" << 3 << endl;	// <<는 shift연산자이다. 똑같은 함수.
	cout << 30 << endl; // 상수 : 변하지 않는 수
}

// 변수의 선언 : 메모리에 할당한다
	// 데이터타입 변수명;
	// 변수명은 소문자로 시작하지만 두 단어가 쓰일경우 '카멜표기법'에 의해 대문자로 시작

	// int 정수를 담는 데이터타입
	// 참고) unsigned int 값의 범위가 더 늘어나는 정수를 담는 데이터타입
	// float 실수를 담는 데이터타입
	// char 문자를 담는 데이터타입
	// bool 논리를 담는 데이터타입
void main()
{
	float num;	//메모리 4바이트 할당
	int inputValue; //메모리 4바이트 할당
	bool test;	//메모리 1바이트 할당
	char temp;	//메모리 1바이트 할당

	// 변수의 정의
	// num이라는 통 안에 값을 넣어준다
	// 형변환(casting) : 데이터타입이 다를 때 발생
	num = 10.8f;
	cout << num << endl;

	inputValue = 16;
	cout << inputValue << endl;

	int i = 10;
	{
		int x = 30;			// 괄호안에 것을 우선적으로 참조한다.
		cout << x << endl;
	}
	// cout << x << endl;	// 메모리지역성에 따라 해제되어 x를 출력할수없어 에러가 뜸.
}


// 조건문
	// if(조건)
	// {
	// 조건이 만족되었을 때 수행
	// }
	// if나 else if나 else는 분기이기때문에 참인 선택지를 고르는 것과 같다
	// else if는 여러개여도 상관없다.
	// 코딩은 위에서 아래로 순서대로 읽힌다.
void main()
{
	int score = 70;

	if (score > 90)
	{
		cout << "A" << endl;
	}
	else if (score > 70)
	{
		cout << "B" << endl;
	}
	else if (score > 50)
	{
		cout << "C" << endl;
	}
	// if문에서 조건이 만족하지않을 경우를 출력하고 싶을때 else를 쓴다
	else
	{
		cout << "D" << endl;
	}
}


//연산자 우선순위
void main()
{
	int a = 5;
	int b = 30;
	int c = 2;
	int result;

	result = a + b * c;

	// 조건A && 조건B
	// 참 && 참 = 참
	// 참 && 거짓 = 거짓
	// 거짓 && 참 = 거짓
	// 거짓 && 거짓 = 거짓
	if (0 < a && a < 10)
	{
		cout << a << endl;
	}

	// 조건A || 조건B
	// 참 || 참 = 참
	// 참 || 거짓 = 참
	// 거짓 || 참 = 참
	// 거짓 || 거짓 = 거짓
	if (b == 0 || b == 30)
	{
		cout << b << endl;
	}
}


// 반복문
	// while (조건)
	// {
	// 조건이 만족하는 동안 반복해서 수행
	// }
void main()
{
	int num = 0;				//초기식
	while (num < 10)			//조건식
	{
		int x = 0;
		cout << num << endl;
		cout << x << endl;
		num++;					//증감식
		x++;
	}
	cout << "마지막의 num의 값 : " << num << endl;
	cout << endl;

	// for (초기식; 조건식; 증감식)
	// {
	// 조건이 만족하는 동안 반복해서 수행
	// }
	for (int i = 0; i < 10; i++)
	{
		int x = 0;
		cout << i << endl;
		cout << x << endl;
		x++;
	}
	cout << "마지막의 num의 값 : " << num << endl;
	cout << endl;

	// 다중반복문(*도형그리기)
	// *****
	// *****
	// *****
	// *****
	// *****
	for (int j = 0; j < 5; j++)
	{
		for (int i = 0; i < 5; i++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cout << endl;

	// *
	// **
	// ***
	// ****
	// *****
	for (int j = 0; j < 5; j++)
	{
		for (int i = 0; i <= j; i++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cout << endl;

	//	 *
	//  ***
	// *****
	int lineNum = 3;
	// 띄어쓰기 모양부터 만들기(띄어쓴것은 lineNum으로 표기)
	for (int i = 0; i < lineNum; i++)
	{
		for (int j = lineNum - 1; j > i; j--)
		{
			cout << " ";
		}
		for (int j = 0; j < 2 * i + 1; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}

// 함수의 선언
	// 리턴타입 함수명(매개변수...)
	// {
	// 함수의 내용
	// }
void PrintMenu()
{
	cout << "------------" << endl;
	cout << "----메뉴----" << endl;
	cout << "------------" << endl;
	// void는 돌려주는게 없으니까 return은 생략됨
	// return;은 '자기를 부른곳을 일단돌아가~'이다.
}

int GetNumberOne()
{
	cout << "1을 가져옵니다" << endl;
	return 1.54f; // 리턴타입은 일종의 약속이기때문에 호출했다면 돌려줘야하는 return이 꼭 있어야 함.
}

int GetIntValue(float num)
{
	return num;
}

int Sum(int inputA, int inputB)
{
	return inputA + inputB;
}

int Sum(int inputA, int inputB, int inputC)
{
	return inputA + inputB + inputC;
}

void main()
{
	cout << "함수 호출 전" << endl;
	PrintMenu();	// 함수의 호출
	cout << GetNumberOne() << endl; // 1을 호출하는것은 아무것도 의미없기때문에 cout을 써줘야함.
	cout << GetIntValue(50.4f) << endl;	// 매개변수가 있을때 틀에 맞춰서 써줘야 호출할수있다.

	//함수의 오버로딩 : 함수의 이름이 동일해도 매겨변수의 갯수나 타입에 따라 다르게 처리할수있다.
	cout << Sum(30, 50) << endl;
	cout << Sum(30, 50, 40) << endl;
	cout << "함수 호출 후" << endl;
}



//스택의 메모리 구조
void Func3()
{
	cout << "함수3 실행 시작" << endl;
	cout << "함수3 실행 끝" << endl;
}

void Func2()
{
	cout << "함수2 실행 시작" << endl;
	Func3();
	cout << "함수2 실행 끝" << endl;
}

void Func1()
{
	cout << "함수1 실행 시작" << endl;
	Func2();
	cout << "함수1 실행 끝" << endl;
}

void main()
{
	cout << "메인 시작" << endl;
	Func1();
	cout << "메인 끝" << endl;
}



// 재귀함수 : 자기자신을 호출하는 함수
// stack overflow : 가용할수있는 범위를 넘어선 반복이 되어 스택의 영역을 침범함
void Func()
{
	cout << "함수 호출 됨" << endl;
	Func();
}

// 1! = 1
// 2! = 2*1!
// 3! = 3*2!
// 4! = 4*3!
// n! = n*(n-1)!
int Factorial(int value)
{
	if (value == 1)
	{
		return 1;
	}
	return value * Factorial(value - 1);
}

void main()
{
	cout << Factorial(6) << endl;
}



// 피보나치수열
// F0 = 0
// F1 = 1
// F2 = 1
// F3 = 2
// F4 = 3
// F5 = 5
// F6 = 8
// F7 = 13
int Fibo(int value)
{
	if (value == 0)
	{
		return 0;
	}
	if (value == 1 || value == 2)
	{
		return 1;
	}
	return Fibo(value - 1) + Fibo(value - 2);
}

void main()
{
	cout << Fibo(7) << endl;
}



void main()
{
	int num = 10;

	cout << num << endl;	// num의 값 = 10
	cout << &num << endl;	// num의 주소
	//여기서 *은 변수의 주소값을 말함
	cout << *&num << endl;	// num주소의 값 = 10

	// 포인트함수 선언
	// 데이터타입* 변수명;
	int* ptr;	// 어딘가에 존재하는 메모리를 가리키고있음 (nullptr)
	ptr = &num;

	cout << ptr << endl; // 포인터함수 = 주소를 담은 값
	cout << *ptr << endl; // *ptr = *&num = 10
}



// 포인터함수를 사용한 Swap함수
// call by value : 값에 의한 참조
void Swap(int inputA, int inputB)
{
	int temp;
	temp = inputA;
	inputA = inputB;
	inputB = temp;
}

// call by address : 주소에 의한 참조
void Swap(int* aPtr, int* bPtr)
{
	int temp;
	temp = *aPtr;
	*aPtr = *bPtr;
	*bPtr = temp;
}

void main()
{
	int a = 10;
	int b = 30;

	cout << a << "," << b << endl;
	Swap(&a, &b);	//a의 주소와 b의 주소를 함수에 보내준다
	cout << a << "," << b << endl;
}

// Sum함수
int Sum(int inputA, int inputB)
{
	return inputA + inputB;
}

void main()
{
	int a = 10;
	int b = 30;

	cout << Sum(a, b) << endl;
}

// Clamp함수
float Sum(float a, float b)
{
	return a + b;
}

float Clamp(float* valuePtr, float min, float max)
{
	if (*valuePtr > max)
	{
		*valuePtr = max;
	}
	else if (*valuePtr < min)
	{
		*valuePtr = min;
	}
	return *valuePtr;
}


void main()
{
	float num = -450;

	cout << Sum(10, Clamp(&num, -100, 100)) << endl;
	cout << num << endl;
}


// 배열
	// index라는 개념이 있기때문에 바로 바로 참조 가능
void main()
{
	int scoreA = 70;
	int scoreB = 20;
	int scoreC = 90;
	int scoreD = 0;
	int scoreE = 80;

	int scores[] = { 70, 20, 90, 0, 80};
	int maxValue = 0;
	int maxIndex = 0;

	//sizeof
	cout << sizeof(int) << endl;
	cout << sizeof(scores) << endl;
	cout << sizeof(scores[0]) << endl;
	cout << sizeof(scores) / sizeof(scores[0]) << endl;

	cout << endl;

	for (int i = 0; i < 5; i++)
	{
		if (scores[i] > maxValue)
		{
			maxValue = scores[i];
			maxIndex = i + 1 ;
		}
		cout << scores[i] << endl ;
	}
	cout << "최고 값 : " << maxValue << endl;
	cout << "최고 값의 위치 : " << maxIndex << endl;
}
728x90