본문 바로가기

프로그래밍/C++ language

4. 수학함수, 문자, 문자열

4.2 수학함수

 

- header는 다양한 수학함수를 포함하고 있음

- sin, cos, tan, asin, acos, atan //삼각함수

- exp, log, log10, pow, sqrt //지수함수

- ceil, floor //올림, 내림

- min, max, abs

 

4.6 문자함수

 

- header는 문자를 테스트하고 변환하는 함수를 포함하고 있음

- isdigit, isapha, isalnum, is lower, isupper, isspace, tolower, toupper

 

4.8 string형

 

- string형은 primitive type이 아닌 object type이며, 문자열을 표현함

- string object를 생성하기 위한 class는 header에 정의되어 있음

- instance function은 length(), size(), at(index)

- stringName[index]문법으로 지정 인덱스 문자에 접근가능

- 문자열 연결(concatenate) 위한 + 연산자 사용

- 문자열의 비교는 ==, >=, <=, <, > 사용

- cin으로 console 입력 받을 수 있음, 또는 getline함수 이용

 

ex)

string s1 = "good";

string s2 = "gun";

string s3;

string s4 = very;

s1.length(); // 4

s1.at(3); // d

s1[0]; //g

string s3 = s2 + s1; //good gun

s4 += s3; // very good gun

s1 <= s2; // 첫번째 문자 g는 같고 두번째 문자 o가 더 작으므로, true

cin >> s4;

getline(cin, s4, delimitCharacter); //delimitCharacter가 나올때까지 값을 읽어들임, default값은 '\n'

 

4.10 콘솔 출력 형식

 

- c++은 출력 값을 어떤식으로 표현할지 결정해주는 stream manipulator함수를 제공함

- setprecision(n), fixed, showpoint, setw(width), left, right

 

ex)

double number = 1234.5678

cout << setprecision(7) << number << endl; //1.234567e+03, 표시될 자릿수 결정

 

cout << fixed << number << endl; // 1234.567800, 소수점 이후 숫자를 고정된 수로 표시(default는 6자리)

cout << fixed << setprecision(2) << number << endl; // 1234.56, 소수점 이후 표시할 숫자를 정해서 fixed

 

cout << setprecision(6)

cout << showpoint << number << endl; //1234.56, 설정한 percision만큼 고정된 숫자 표시

 

cout << setw(12) << number << 101 << endl; // ' 1234.5678101', 출력 너비 설정, setw는 바로 다음 출력에만 적용됨

cout << left; // 왼쪽 정렬 설정

cout << setw(8) << 1.23 << 101 << endl; // '1.23 101'