본 포스팅은 C++로 시작하는 객체지향 프로그래밍 책을 바탕으로 작성하였습니다.
C++로 시작하는 객체지향 프로그래밍
『C++로 시작하는 객체지향 프로그래밍』은 구문보다는 문제 해결에 중점을 두는 문제 구동 방식을 사용한 프로그래밍에 대해 가르치고 있다. 여러 가지 상황에서 문제를 야기한 개념을 사용함
book.naver.com
key point
- cmath 헤더에서 수학적 기능을 수행하기 위한 함수 제공 (삼각함수, 지수함수, 라운드 함수, min/max.abs 함수)
- 문자 데이터 유형 (char) 은 하나의 문자를 표현한다. ( *문자열 리터럴은 따옴표(" ") / 문자 리터럴 작은따옴표( ' '))
- char형의 크기는 1byte
- ASCII 문자 : 문자를 이진 표현으로 mapping시키는 enconding기법
- 이스케이프 시퀀스 : 특수 문자를 표시하기 위해 특별 기호 사용 (역슬래쉬\ + 특수문자)
- / : 이스케이프 문자
- char형은 숫자 유형으로 변환될 수 있고, 그 반대도 가능
- 문자열(srting)은 연속으로 되어는 문자들을 말한다
- 문자는 정수를 사용하여 부호화된다. 임의의 문자를 생성하는 것은 정수를 생성하는 것이다.
- char형은 1바이트이기 때문에 한 문자
- string형으로 문자열을 표현 -> 객체 유형(object type) / length() size() at(index)
- string 유형을 사용하여 문자열 객체를 선언할 수 있다. 특정 객체로부터 호출된 함수를 인스턴스 함수라고 한다.
- 콘솔에 형식화된 출력을 표시하기 위해서는 스트림 조정자를 사용하면 된다.
check point
(접은글에서 확인)
더보기
4.1 함수 호출의 결과 계산하기
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const double PI = 3.14159;
const double E = 2.71828;
cout << sqrt(4.0) << endl;// sqrt(x) x>=0 에 대한 x의 루트값을 반환
cout << sin(2 * PI) << endl; //sin(radians) 라디안 각도의 사인 삼각 함수 값을 반환
cout << cos(2 * PI) << endl; // 라디안 각도의 코사인 삼각 함수 값을 반환
cout << pow(2.0, 2) << endl; // pow(a,b) a의 b제곱값을 반환
cout << log(E) << endl; // 자연로그 값을 반환
cout << exp(1.0) << endl; // exp(x) 자연 상수 e의 x제곱 값을 반환
cout << max(2, min(3, 4)) << endl;
cout << sqrt(125.0) << endl;
cout << ceil(-2.5) << endl; //x를 가장 가까운 정수로 "올림"
cout << floor(-2.5) << endl; //x를 가장 가까운 정수로 "내림"
cout << asin(0.5) << endl; //역사인의 라디안 각도 값을 반환
cout << acos(0.5) << endl; //역코사인의 라디안 각도 값을 반환
cout << atan(1.0) << endl; //역탄젠트의 라디안 각도 값을 반환
cout << ceil(2.5) << endl;
cout << floor(2.5) << endl;
cout << log10(10.0) << endl; //상용로그 값을 반환
cout << pow(2.0, 3) << endl;
return 0;
}
//2
//-5.30718e-06
//1
//4
//0.999999
//2.71828
//3
//11.1803
//-2
//-3
//0.523599
//1.0472
//0.785398
//3
//2
//1
//8
4.5 ASKII코드 값 / char형변환
static_cast<char>(85)를 하면 10진수 85에 있는 문자 출력
static_cast<char>('85')를 하면 5 출력 (char형은 1바이트만 담기 때문에 일의 자리수만 출력함)
static_cast<int>('1') 은 '1'이라는 아스키코드 문자를 아스키코드 숫자로 변환
#include <iostream>
using namespace std;
int main()
{
cout << static_cast<int>('1') << endl;
cout << static_cast<int>('A') << endl;
cout << static_cast<int>('B') << endl;
cout << static_cast<int>('a') << endl;
cout << static_cast <int>('b') << endl;
//10진코드
cout << endl << static_cast<char>(40) << endl;
cout << static_cast<char>(85) << endl; // (85)와 ('85')는 엄연한 차이가 존재
cout << static_cast<char>(79) << endl;
cout << static_cast<char>(85) << endl;
cout << static_cast<char>(90) << endl;
//16진수 코드
cout << endl << static_cast<char>(0X40) << endl;
cout << static_cast<char>(0X5A) << endl;
cout << static_cast<char>(0X71) << endl;
cout << static_cast<char>(0X72) << endl;
cout << static_cast<char>(0X7A) << endl;
return 0;
}
//49
//65
//66
//97
//98
//(
//U
//O
//U
//Z
//@
//Z
//q
//r
//z
list
(접은글에서 확인)
더보기
4.1 예제 : 삼각형의 각 계산
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double x1, x2, x3, y1, y2, y3;
cout << "삼각형의 꼭짓점 x1, y1을 입력해주세요 : ";
cin >> x1 >> y1;
cout << "삼각형의 꼭짓점 x2, y2을 입력해주세요 : ";
cin >> x2 >> y2;
cout << "삼각형의 꼭짓점 x3, y3을 입력해주세요 : ";
cin >> x3 >> y3;
double a = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
double b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
double c = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
double A = acos((a * a - b * b - c * c) / (-2 * b * c));
double B = acos((b * b - a * a - c * c) / (-2 * a * c));
double C = acos((c * c - b * b - a * a) / (-2 * a * b));
const double PI = 3.14159;
cout << "삼각형의 각도는 " << A * 180 / PI << " " << B * 180 / PI << " " << C * 180 / PI << endl;
return 0;
}
//삼각형의 꼭짓점 x1, y1을 입력해주세요 : 1 1
//삼각형의 꼭짓점 x2, y2을 입력해주세요 : 6.5 1
//삼각형의 꼭짓점 x3, y3을 입력해주세요 : 6.5 2.5
//삼각형의 각도는 15.2551 90.0001 74.7449
4.2 소문자를 입력하면 대문자를 출력하는 프로그램
#include <iostream>
using namespace std;
int main(){
cout << "소문자를 입력하세요: ";
char lower;
cin >> lower;
char upper = static_cast<char>('A' + (lower - 'a'));
cout << lower << "는 대문자로 " << upper << "입니다. " << endl;
return 0;
}
//소문자를 입력하세요: a
//a는 대문자로 A입니다.
4.3 x와 y 사이의 임의의 문자를 출력
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
char start;
cout << "Enter a starting charater: ";
cin >> start;
char end;
cout << "Enter an ending charater: ";
cin >> end;
char random = static_cast<char>(start + rand() % (end - start + 1));
cout << "The random character between " << start << " and " << end << " is "
<< random << endl;
return 0;
}
//Enter a starting charater: c
//Enter an ending charater: s
//The random character between c and s is
4.4 예제 : 생일 맞추기
#include <iostream>
using namespace std;
int main(){
int day = 0; //결정할 날
char answer;
cout << "Is your birthday in Seti1?" << endl;
cout << " 1 3 5 7\n"
<< " 9 11 13 15\n"
<< " 17 19 21 23\n"
<< " 25 27 29 31\n" << endl;
cout << "Enter N/n for NO and Y/y for YES : ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
day += 1;
cout << "Is your birthday in Seti2?" << endl;
cout << " 2 3 6 7\n"
<< " 10 11 14 15\n"
<< " 18 19 22 23\n"
<< " 26 27 30 31\n" << endl;
cout << "Enter N/n for NO and Y/y for YES : ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
day += 2;
cout << "Is your birthday in Seti3?" << endl;
cout << " 4 5 6 7\n"
<< " 12 13 14 15\n"
<< " 20 21 22 23\n"
<< " 26 27 30 31\n" << endl;
cout << "Enter N/n for NO and Y/y for YES : ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
day += 4;
cout << "Is your birthday in Seti4?" << endl;
cout << " 8 9 10 11\n"
<< " 12 13 14 15\n"
<< " 24 25 26 27\n"
<< " 28 29 30 31\n" << endl;
cout << "Enter N/n for NO and Y/y for YES : ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
day += 8;
cout << "Is your birthday in Seti5?" << endl;
cout << " 16 17 18 19\n"
<< " 20 21 22 23\n"
<< " 24 25 26 27\n"
<< " 28 29 30 31\n" << endl;
cout << "Enter N/n for NO and Y/y for YES : ";
cin >> answer;
if (answer == 'y' || answer == 'Y')
day += 16;
cout << "Your birthday is " << day << endl;
return 0;
}
/*
Is your birthday in Seti1?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Enter N/n for NO and Y/y for YES :
n
Is your birthday in Seti2?
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31
Enter N/n for NO and Y/y for YES : y
Is your birthday in Seti3?
4 5 6 7
12 13 14 15
20 21 22 23
26 27 30 31
Enter N/n for NO and Y/y for YES : n
Is your birthday in Seti4?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Enter N/n for NO and Y/y for YES : n
Is your birthday in Seti5?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
Enter N/n for NO and Y/y for YES : y
Your birthday is 18
*/
4.5 문자함수
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;
if (isupper(ch))
{
cout << ch << "는 대문자입니다." << endl;
cout << "소문자로 바꾸면 " << static_cast<char>(tolower(ch)) << endl;
}
if (islower(ch))
{
cout << ch << "는 소문자입니다." << endl;
cout << "대문자로 바꾸면 " << static_cast<char>(toupper(ch)) << endl;
}
else if (isdigit(ch))
cout << "이 문자는 숫자입니다." << endl;
return 0;
}
//Enter a character: A
//A는 대문자입니다.
//소문자로 바꾸면 a
4.6 예제 : 16진수를 10진수로 변환
//교재
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout << "16진수를 입력해주세요 : ";
cin >> ch;
ch = toupper(ch);
if (ch <= 'F' && ch >= 'A')
{
int value = 10 + ch - 'A';
cout << ch << "는 10진수로 " << value << endl;
}
else if (isdigit(ch))
cout << ch << "는 10진수로 " << ch << endl;
else
cout << "잘못 입력하셨습니다." << endl;
return 0;
}
//16진수를 입력해주세요 : d
//D는 10진수로 13
//내가 손본거
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout << "16진수를 입력해주세요 : ";
cin >> ch;
if (isalpha(ch))
{
ch = toupper(ch);
if (ch <= 'F' && ch >= 'A')
{
int value = 10 + (ch - 'A');
cout << "16진수로 " << value << endl;
}
else
cout << "잘못입력하셨습니다." << endl;
}
else if (isdigit(ch))
cout << "16진수로 " << ch << endl;
return 0;
}
4.7 문자열 비교
#include <iostream>
#include <string>
using namespace std;
int main()
{
string city1, city2;
cout << "Enter city1 : ";
getline(cin, city1);
cout << "Enter city2 : ";
getline(cin, city2);
cout << "알파벳 순서로 ";
if (city1 < city2)
cout << city1 << " " << city2 << endl;
else
cout << city2 << " " << city1 << endl;
return 0;
}
//Enter city1 : new york
//Enter city2 : jeju do
//알파벳 순서로 jeju do new york
4.9 예제 : 문자열을 사용하여 복권 프로그램 수정
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
string lottery;
srand(time(0));
int dight = rand() % 10;
lottery += static_cast<char>(dight + '0');
dight = rand() % 10;
lottery += static_cast<char>(dight + '0');
cout << "Enter yout lottery pick (two dights) : ";
string guess;
cin >> guess;
cout << "The lottery number is " << lottery << endl;
if (guess == lottery)
cout << "Exact match : you win $10,000" << endl;
else if (guess[1] == lottery[0] && guess[0] == lottery[1])
cout << "Match all dights : you win $3.000 " << endl;
else if (guess[0] == lottery[1] || guess[0] == lottery[0]
|| guess[1] == lottery[0] || guess[1] == lottery[1])
cout << "Match one dight: you win $1,000" << endl;
else
cout << "Sorry, no match" << endl;
return 0;
}
/*
Enter yout lottery pick (two dights) : 53
The lottery number is 05
Match one dight: you win $1,000
*/
프로그래밍 실습
4.1 기하학 : 오각형의 면적
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
cout << "오각형의 중심으로부터 꼭짓점까지의 거리를 입력해주세요: ";
double r;
cin >> r;
const double PI = 3.14159;
double s = 2 * r * cos(3* PI / 10);
double width = (5 * s * s) / (4 * tan(PI / 5));
cout << "오각형의 면적은 " << fixed << setprecision(2) << width << endl;
return 0;
}
//오각형의 중심으로부터 꼭짓점까지의 거리를 입력해주세요: 5.5
//오각형의 면적은 71.92
4.11 대문자를 소문자로 변환
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter an uppercase letter: ";
char i;
cin >> i;
cout << "The lowercast letter is " << static_cast<char>(tolower(i)) << endl;
return 0;
}
//Enter an uppercase letter: T
//The lowercast letter is t
'Language > C++' 카테고리의 다른 글
[C++_백준] if문 ( 1330, 9498, 2753, 14681, 2884 ) (0) | 2021.11.08 |
---|---|
[C++_제 5장] 반복문 (0) | 2021.10.09 |
[C++_제 3장] 선택문 (0) | 2021.10.03 |
[C++_제 2장] 기본 프로그래밍 (0) | 2021.10.03 |
[C++_백준] 입출력과 사칙연산 ( 10171, 10172, 1000, 1001, 10998, 1008, 10869, 10430, 2588) (0) | 2021.10.01 |