#include <iostream>
#include <string>
using std::cin;
using std::endl;
using std::cout;
using std::string;
int triangle() { // 삼각형 그리기
const int bottom = 5;
const int top = (5 / 2) + 1;
cout << endl;
for (int row = bottom; row > 0; row = row - 2) {
for (int col = 0; col < row / 2; col++) {
cout << " ";
}
for (int col = 0; col < bottom - row + 1; col++) {
cout << "*";
}
for (int col = 0; col < row / 2; col++) {
cout << " ";
}
cout << endl;
}
return 0;
}
int rect() { // 정사각형 그리기
cout << endl;
const int num = 5;
for (int row = 0; row < num; row++) {
for (int col = 0; col < num; col++) {
if (row == num - 1|| row == 0) {
cout << "*";
}
else
if (col == 0 || col == num - 1) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
triangle();
return 0;
}
int main() {
cout << "please enter your first name : ";
string name;
cin >> name;
const string greeting = "hello, " + name + "!";
const int pad = 1;
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
// 부호없는 타입 모든 문자열의 크기를 저장 가능
cout << endl;
for (int row = 0; row < rows; row++) {
string::size_type col = 0;
while (col < cols) {
if (row == pad + 1 && col == pad + 1) {
cout << greeting;
col += greeting.size();
}
if (row == 0 || row == rows - 1 || col == 0 || col == cols - 1)
cout << "*";
else
cout << " ";
col++;
}
cout << endl;
}
rect();
return 0;
}
#include <iostream>
#include <string>
using std::cin;
using std::endl;
using std::cout;
using std::string;
C++ 은 namespace
를 using으로서 생략 해 줄 수 있다. 이 뜻은 std 라는 namespace를 더이상 선언하지 않는다는 것을 포함한다.
using std
로 그 안에 있는 기능 (?) 들 모두 using
처리 가능하다.
string::size_type
처럼 string에 type을 정해 줄 수 있다. size_type은, 부호가 없는 타입이며 모든 문자열의 크기를 다룬다.if (row == 0 || row == rows - 1
|| col == 0 || col == cols - 1)
cout << "*";
else
cout << " ";
{}
중괄호를 사용 하지 않아도 된다. else 또한 마찬가지.대충 using을 사용하면 type명을 선언하는 자바스크립트랑 비슷 한 거 같다.
(아직은 typescript랑은 다른거 같다.)