본 포스팅은 C++로 시작하는 객체지향 프로그래밍 책을 바탕으로 작성하였습니다.
C++로 시작하는 객체지향 프로그래밍
『C++로 시작하는 객체지향 프로그래밍』은 구문보다는 문제 해결에 중점을 두는 문제 구동 방식을 사용한 프로그래밍에 대해 가르치고 있다. 여러 가지 상황에서 문제를 야기한 개념을 사용함
book.naver.com
key point
- 부울 식(Boolean expression) : 참(True)과 거짓(False)의 부울 값으로 결과가 나오는 수식
- if문은 프로그램에서 선택적으로 실행 경로를 지정할 수 있게 해 준다.
- 임의의 정수를 발생시키기 위해서 rand() 함수를 사용할 수 있다.
- 복합적인 부울 식을 생성하기 위해서 !, &&, || 의 논리 연산자를 사용할 수 있다.
- switch 문은 변수나 수식의 값에 따라 문장을 실행하게 된다.
- 조건식은 조건을 기초로 식을 평가한다.
check point
(접은글에서 확인)
3.1 관계연산자
관계 연산자 : > >= < <= == !=
3.2. x가 1이라고 가정했을 때, 다음 부울 식의 결과는?
#include <iostream>
using namespace std;
int main() {
int x = 1
cout << x > 0 << endl; //1
cout << X < 0 << endl; //0
cout << x != 0 << endl; //1
cout << x >= 0 << endl; //1
cout << x !=1 << endl; //0
return 0;
}
3.3
#include <iostream>
using namespace std;
int main()
{
bool b = true;
int i = b;
cout << b << endl;
cout << i << endl;
return 0;
}
//1
//1
3.4. y가 0보다 크면 x에 1을 대입하는 if문을 작성하여라.
#include <iostream>
using namespace std;
int main()
{
int y;
cout << "y의 값을 입력해주세요 : ";
cin >> y;
if (y > 0)
{
int x = 1;
cout << "x = " << x << endl;
}
else
{
cout << "y의 값은 음수입니다." << endl;
}
return 0;
}
//y의 값을 입력해주세요 : 3
//x = 1
//y의 값을 입력해주세요 : -5
//y의 값은 음수입니다.
3.5 socre가 90보다 크면 3%만큼 월급을 인상하는 if문을 작성하여라.
#include <iostream>
using namespace std;
int main()
{
cout << "score값을 입력해주세요: ";
double score; //int로 하면 몫만 나오기 때문에 거의 똑같은 값이 나옴
cin >> score;
if (score > 90)
{
score *= 0.03;
cout << score << "만큼 월급을 인상하겠습니다." << endl;
}
else
cout << "월급이 제자리입니다." << endl;
return 0;
}
//score값을 입력해주세요: 98
//2.94만큼 월급을 인상하겠습니다.
3.7. socre가 90보다 크면 3%만큼 월급을 인상하고, 그렇지 않다면 1%만큼 인상하는 if문을 작성하여라
#include <iostream>
using namespace std;
int main()
{
cout << "score값을 입력해주세요: ";
double score;
cin >> score;
if (score > 90)
{
score *= 0.03;
cout << score << "만큼 월급을 인상하겠습니다." << endl;
}
else
{
score *= 0.01;
cout << score << "만큼 월급을 인상하겠습니다." << endl;
}
return 0;
}
//score값을 입력해주세요: 89
//0.89만큼 월급을 인상하겠습니다.
//score값을 입력해주세요: 91
2.73만큼 월급을 인상하겠습니다.
3.9 중첩 if 문
#include <iostream>
using namespace std;
int main()
{
int x;
int y;
cin >> x >> y;
if (x > 2)
{
if (y > 2)
{
int z = x + y;
cout << "z is " << z << endl;
}
}
else
cout << "x is " << x << endl;
return 0;
}
//x=3, y=2
//
//x=3, y=4
//z is 7
//x=2, y=2
//x is 2
3.19
(0 <= i < 20) _ rand() % 20
(10 <= i < 20) _ 10 + rand() % 10
(10 <= i < 50) _ 10 + rand() % 40
0 또는 1을 임의로 반환하는 수식 rand() % 2
list
(접은글에서 확인)
3.1 정수를 입력하도록 하는 프로그램
#include <iostream>
using namespace std;
int main(){
cout << "정수를 입력해주세요: " << endl;
int a;
cin >> a;
if (a % 5 == 0)
cout << "HiFive" << endl;
if (a % 2 == 0)
cout << "HiEven" << endl;
return 0;
}
//정수를 입력해주세요:8
//HiEven
3.2 예제 : 체질량지수 계산
#include <iostream>
using namespace std;
int main(){
double pound;
double inch;
cout << "파운드 단위의 몸무게를 입력하세요 : " << endl;
cin >> pound;
cout << "인치 단위의 키를 입력하세요 : " << endl;
cin >> inch;
double KG = 0.45359237 * pound;
double METER = 0.0254 * inch;
double BMI = KG / (METER * METER);
cout << "BMI is " << BMI << endl;
if (BMI > 30.0)
cout << "비만" << endl;
else if (BMI > 25.0)
cout << "과제중" << endl;
else if (BMI > 18.5)
cout << "정상" << endl;
else
cout << "체중 미달" << endl;
return 0;
}
//파운드 단위의 몸무게를 입력하세요 :
//146
//인치 단위의 키를 입력하세요 :
//70
//BMI is 20.9486
//정상
3.4 난수 생성
#include <iostream>
#include <ctime> //time 함수로 인해 삽입
#include <cstdlib> //rand와 srand 함수로 인해 삽입
using namespace std;
int main(){
srand(time(0));
int num1 = rand() % 10;
int num2 = rand() % 10;
if (num1 < num2)
{
int change = num1;
num1 = num2;
num2 = change;
}
cout << "what is " << num1 << " - " << num2 << " ? ";
int answer;
cin >> answer;
if (num1 - num2 == answer)
cout << "you are correct";
else
cout << "your answer is wrong. Answer is " << num1 - num2 << endl;
return 0;
}
//what is 9 - 0 ? 5
//your answer is wrong. Answer is 9
//what is 5 - 1 ? 4
//you are correct
3.5 논리연산자
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an interger : ";
cin >> number;
if (number % 2 == 0 && number % 3 == 0)
cout << number << " is divisible by 2 and 3. " << endl;
else if (number % 2 == 0 || number % 3 == 0)
cout << number << " is divisible by 2 or 3. " << endl;
else if ((number % 2 == 0 || number % 3 == 0) && !(number % 2 == 0 && number % 3 == 0))
cout << number << " is divisible by 2 and 3, but not both." << endl;
else
cout << number << " is not divisible by 2 and 3" << endl;
return 0;
}
//Enter an interger : 56
//56 is divisible by 2 or 3.
3.6 예제: 윤년 계산
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a year : ";
int year;
cin >> year;
bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (isLeapYear)
cout << year << " is a leap year." << endl;
else
cout << year << " is a npt leap year." << endl;
return 0;
}
//Enter a year : 2021
//2021 is a npt leap year.
3.7 복권 계산
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int lottery = rand() % 100;
cout << "Enter yout lottery pick *two dights : ";
int guess;
cin >> guess;
int lottery1 = lottery / 10;
int lottery2 = lottery % 10;
int guess1 = guess / 10;
int guess2 = guess % 10;
cout << "The lottery number is " << lottery << endl;
if (guess == lottery)
cout << "Exact match : you win $10,000" << endl;
else if (guess1 == lottery2 && guess2 == lottery1)
cout << "Match all dights : you win $3,000" << endl;
else if (guess1 == lottery1 || guess1 == lottery2 || guess2 == lottery1 || guess2 == lottery2)
cout << "Match one dights : you win $1,000." << endl;
else
cout << "Sorry, no match." << endl;
return 0;
}
//Enter yout lottery pick *two dights : 45
//The lottery number is 35
//Match one dights : you win $1,000.
3.8 연도의 띠 계산
#include <iostream>
using namespace std;
int main()
{
cout << "연도의 띠를 결정해주는 프로그램입니다. " << endl;
cout << "연도를 입력해주세요 : ";
int year;
cin >> year;
switch (year % 12)
{
case 0: cout << "원숭이띠 입니다." << endl; break;
case 1: cout << "닭띠 입니다." << endl; break;
case 2: cout << "개띠 입니다." << endl; break;
case 3: cout << "돼지띠 입니다." << endl; break;
case 4: cout << "쥐띠 입니다." << endl; break;
case 5: cout << "소띠 입니다." << endl; break;
case 6: cout << "호랑이띠 입니다." << endl; break;
case 7: cout << "토끼띠 입니다." << endl; break;
case 8: cout << "용띠 입니다." << endl; break;
case 9: cout << "뱀띠 입니다." << endl; break;
case 10: cout << "말띠 입니다." << endl; break;
case 11: cout << "양띠 입니다." << endl; break;
default: cout << "그럴리가 없는데";
}
}
//연도의 띠를 결정해주는 프로그램입니다.
//연도를 입력해주세요 : 1936
//쥐띠 입니다.
프로그래밍 실습
3.1 대수학 : 2차 방적식 계산
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a, b, c : ";
double a, b, c;
cin >> a >> b >> c;
double total = (b * b) - (4 * a * c);
if (total > 0)
{
double r1 = ((-1 * b) + pow((b * b) - (4 * a * c), 0.5)) / (2 * a);
double r2 = ((-1 * b) - pow((b * b) - (4 * a * c), 0.5)) / (2 * a);
cout << "The roots are " << r1 << " and " << r2 << endl;
}
else if (total == 0)
{
double rr = -b / 2 * a;
cout << "The root is " << rr << endl;
}
else
cout << "The equation has no real roots" << endl;
return 0;
}
//Enter a, b, c : 1.0 3 1
//The roots are -0.381966 and -2.61803
3.2 숫자 점검
int main()
{
cout << "Enter two integers : ";
int num1, num2;
cin >> num1 >> num2;
if (num1 % num2 == 0)
cout << num1 << " is divisible by " << num2 << endl;
else
cout << num1 << " is not divisible by " << num2 << endl;
return 0;
}
//Enter two integers : 2 3
//2 is not divisible by 3
3.3 대수학 : 2X2 1차 방정식 계산
int main()
{
cout << "Enter a, b, c, d, e, f : ";
double a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
if ((a * d) - (b * c) != 0)
{
double x = (((e * d) - (b * f)) / ((a * d) - (b * c)));
double y = (((a * f) - (e * c)) / ((a * d) - (b * c)));
cout << "x is " << x << " and y is " << y << endl;
}
else
cout << "The equation has no solution" << endl;
return 0;
}
//Enter a, b, c, d, e, f : 9.0 4.0 3.0 -5.0 -6.0 -21.0
//x is -2 and y is 3
//Enter a, b, c, d, e, f : 1.0 2.0 2.0 4.0 4.0 5.0
//The equation has no solution
3.4 온도 점검
#include <iostream>
using namespace std;
int main()
{
cout << "섭씨온도를 입력해주세요: ";
int tem;
cin >> tem;
if (tem < 30)
cout << "too cold" << endl;
else if (tem > 100)
cout << "too hot" << endl;
else
cout << "just right" << endl;
return 0;
}
//섭씨온도를 입력해주세요: 98
//just right
'Language > C++' 카테고리의 다른 글
[C++_제 5장] 반복문 (0) | 2021.10.09 |
---|---|
[C++_제 4장] 수학 함수, 문자, 문자열 (0) | 2021.10.09 |
[C++_제 2장] 기본 프로그래밍 (0) | 2021.10.03 |
[C++_백준] 입출력과 사칙연산 ( 10171, 10172, 1000, 1001, 10998, 1008, 10869, 10430, 2588) (0) | 2021.10.01 |
[C++_제 1장] 컴퓨터, 프로그램 및 C++ 입문 (0) | 2021.09.22 |