I. 출력
1. 기본 출력
c++에서의 표준 함수들은 함수 앞에 std:: 를 붙여주어야 사용할 수 있다.
std::cout << "Hello";
코드 상단에 using namespace std; 를 적어주면 매번 std:: 를 함수 앞에 적어주지 않아도 된다.
using namespace std;
또한 cout 함수는 iostream 이라는 헤더를 코드 상단에 포함시켜줘야 사용할 수 있다.
#include <iostream>
1) 단어 출력
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello";
return 0;
}
2) 문장 출력
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello World!";
return 0;
}
3) 따옴표 출력
#include <iostream>
using namespace std;
int main() {
std::cout << "He says \"It's a really simple sentence\".";
return 0;
}
4) 2줄 출력
(1) 줄바꿈
- cout << endl;
- cout << "Hello" << endl << "World!";
- cout << "\n";
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl << "World";
return 0;
}
5) 숫자 출력
#include <iostream>
using namespace std;
int main() {
cout << 3;
return 0;
}
6) 공백을 사이에 두고 숫자 2개 출력
#include <iostream>
using namespace std;
int main() {
cout << 3 << " " << 5;
return 0;
}
7) 1줄 출력
#include <iostream>
using namespace std;
int main() {
cout << "Let's go LeebrosCode!";
return 0;
}
8) 2줄 출력
#include <iostream>
using namespace std;
int main() {
cout << "Hello students!";
cout << endl;
cout << "Welcome to LeebrosCode!";
return 0;
}
9) 다양하게 출력
#include <iostream>
using namespace std;
int main() {
cout << "Total days in Year\n";
cout << 365;
cout << endl;
cout << "Circumference rate\n";
cout << "3.1415926535";
return 0;
}
3.1415926535를 숫자로 출력하려고 하면 자리수가 잘려서 출력됨.
c++ 실수형 출력은 자리수를 직접 지정해주어야 함.
#include <iostream>
#include <iomanip> // for std::fixed and std::setprecision
using namespace std;
int main() {
cout << "Total days in Year\n";
cout << 365;
cout << endl;
cout << "Circumference rate\n";
cout << fixed << setprecision(10) << 3.1415926535 << endl;
return 0;
}
2. 변수와 자료형
1) c++에서 자주 쓰는 자료형
- 정수 : int, long long
- 실수 : double
- 문자 : char
- 문자열 : string
2) 코딩 컨벤션" (Coding Convention) 이라고 하는
- 일종의 코드 작성 규약
3) 정수 선언하고 뺄셈
#include <iostream>
using namespace std;
int main() {
int a = 97;
int b = 13;
int sub = a - b;
cout << a << " - " << b << " = " << sub;
return 0;
}
4) 변수 선언하기
#include <iostream>
using namespace std;
int main() {
int a = 3;
char b = 'C';
cout << a << endl;
cout << b << endl;
return 0;
}
5) 정수 선언하고 곱 출력
#include <iostream>
using namespace std;
int main() {
int a = 26;
int b = 5;
int mul = a * b;
cout << a << " * " << b << " = " << mul;
return 0;
}
3. 출력 형식
1) 세 정수형 변수 선언
#include <iostream>
using namespace std;
int main() {
int a = 7;
int b = 23;
int c = 30;
cout << a << " + " << b << " = " << c;
return 0;
}
2) 변수 출력하기
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 3;
char b = 'C';
string c = "...";
cout << a << c << b;
return 0;
}
3) 변수 출력하기 2
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 3;
char b = 'C';
string c = "!.....!";
cout << b << c << a;
return 0;
}
4) 변수 출력하기 3
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 1;
int b = 2;
char c = 'C';
string d = "->";
cout << a << d << b << d << c;
return 0;
}
4. 소수점 맞춰 출력
- cout << fixed: 소수점 자리를 고정
- cout.precision(n): 소수점 n째자리까지 표현
#include <iostream>
using namespace std;
int main() {
cout << fixed;
double a = 23.567268;
cout.precision(3);
cout << a << endl;
return 0;
}
1) 달에서 무게 구하기
#include <iostream>
using namespace std;
int main() {
cout << fixed;
int weight = 13;
double rate = 0.165;
double moon = weight * rate;
cout.precision(6);
cout << weight << " * " << rate << " = " << moon << endl;
return 0;
}
2) 소수점 반올림하기
#include <iostream>
using namespace std;
int main() {
cout << fixed;
double a = 25.352;
cout.precision(1);
cout << a;
return 0;
}
3) 길이 단위 변환하기
#include <iostream>
using namespace std;
int main() {
cout << fixed;
double ft = 30.48;
int mi = 160934;
cout.precision(1);
cout << "9.2ft = " << 9.2 * ft << "cm" << endl;
cout << "1.3mi = " << 1.3 * mi << "cm";
return 0;
}
4) 출력결과 42
cout << fixed;
double a = 2.8437;
cout.precision(2);
cout << a << endl;
5) 두 실수의 곱
#include <iostream>
using namespace std;
int main() {
double a = 5.26;
double b = 8.27;
cout << fixed;
cout.precision(3);
cout << a * b;
return 0;
}
5. 변수 값 변경
1) 변수 값 교체하기
#include <iostream>
using namespace std;
int main() {
int a = 3;
a = 6;
cout << a;
return 0;
}
2) 변수 값 교체하기 4
#include <iostream>
using namespace std;
int main() {
int a = 7;
a = 4;
cout << a;
return 0;
}
3) 문자 변경하기
#include <iostream>
using namespace std;
int main() {
char a = 'C';
a = 'T';
cout << a;
return 0;
}
6. 다른 변수로부터 값 변경
1) 변수 값 교체하기 2
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 3;
b = 3;
a = b;
cout << a << endl;
cout << b << endl;
return 0;
}
2) 변수 값 교체하기 5
#include <iostream>
using namespace std;
int main() {
int a = 2;
int b = 6;
a = b;
cout << a << endl;
cout << b << endl;
return 0;
}
3) 정수 복사
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = 4;
b = a;
cout << a << ' ' << b << endl;
cout << a * b;
return 0;
}
7. 두 변수 값을 교환
1) 변수 값 교체하기 3
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = 5;
int temp;
temp = a;
a = b;
b = temp;
cout << a << endl;
cout << b << endl;
return 0;
}
2) 변수 값 교체하기 6
#include <iostream>
using namespace std;
int main() {
int a = 2;
int b = 5;
int temp;
temp = a;
a = b;
b = temp;
cout << a << endl;
cout << b << endl;
return 0;
}
3) 데이터 교환
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 6;
int c = 7;
int temp1, temp2;
temp1 = b;
temp2 = c;
b = a;
c = temp1;
a = temp2;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
8. 변수값 동시에 복사
chain 형식
a = b = c 코드의 경우, 오른쪽에서 부터 먼저 b에 값 c를 넣어주고, 그 다음 a에 값 b를 넣어주게 됨.
전부 c와 동일한 값을 갖게 됨.
1) 변수 값 복사하기
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = 2;
int c = 3;
a = b = c;
cout << a << ' ' << b << ' ' << c;
return 0;
}
2) 변수 값 복사하기 2
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 6;
int c = 7;
a = b = c;
cout << a << ' ' << b << ' ' << c;
return 0;
}
3) 합을 복사하기
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = 2;
int c = 3;
int sum = a + b + c;
a = b = c = sum;
cout << a << ' ' << b << ' ' << c;
return 0;
}
'코드트리' 카테고리의 다른 글
[코드트리 조별과제] C++ 기초 : 단순 반복문 (2) (0) | 2024.08.19 |
---|---|
[코드트리 조별과제] C++ 기초 : 단순 반복문 (1) (0) | 2024.08.14 |
[코드트리 조별과제] C++ 기초 : 조건문 (0) | 2024.08.06 |
[코드트리 조별과제] C++ 기초 : 연산자 (0) | 2024.07.29 |
[코드트리 조별과제] C++ 기초 : 입출력 (0) | 2024.07.22 |