Hello World 출력하기


C


사용하기 어려움

읽기 더 어려움


printf("Hello, %s %d \n","world",123);



C++


사용하기 더 쉬움

읽기도 더 쉬움


std::cout << "Hello, " << "world" << 123 << std::endl;  ( <<은 <- 로 밀어넣는다고 생각하면됨 )




네임스페이스 (namespace) 


어떤 공간을 정의하고 이름을 정해준다 , 동일한 이름의 것을 다른 공간에 정의하면 다른것으로 본다


함수 , 클래스 등의 충돌을 피하기위해 만들어진것




using 지시문


타이핑의 양을 줄이는 방법일뿐


using namespace std;


cout<< "Hello,World! " << endl; // std:: 생략가능


#pragma once // 이렇게하면 헤더가 한번만 #include 됨 #ifndef  ...  #endif 대체




<< 연산자 (operator)


출력연산자 , 밀어넣는 연산자 , push 연산자


+ 혹은 - 등과 같은 연산자 중 하나


C++에서는 프로그래머가 연산자의 동작을 바꿀 수 있다




출력 형식 지정 ( Output  Formatting ) 



16진수 출력


C


int number = 10;


printf( "$#x\n" , number ); // 쉽게 안읽힘


-> Manipulator( 조정자 )  도입  -> 내가 원하는것을 하게만드는것



C++


int number = 10;


cout << showbase << hex << number << endl;




조정자( Manipulator )



showpos / noshowpos - positive (양수) 숫자가 양수이면 + 사인 보여줘


cout << showpos << number; // +123

cout << noshowpos << number; // 123



dec / hex / oct - 10진수 /16진수/8진수


cout << dec << number; // 123

cout << hex << number; // 7b 

cout << oct << number; // 173



uppercase / nouppercase  - 대문자로 바꿔라 / 대문자로 바꾸지마라


cout << uppercase << hex << number; // 7B

cout << nouppercase << hex << number' // 7b



showbase / noshowbase - 몇진법인지 표기해줘라 / 표기하지 말아라


cout << showbase << hex << number << endl; // 0x7b

cout << noshowbase << hex << number << endl; // 7b



left / internal / right - 왼쪽정렬 /  부호는 제일왼쪽 숫자는 맨오른쪽 정렬 / 오른쪽 정렬


number 변수값이 -123이라면 


cout << setw(6) << left << number; // -123

cout << setw(6) << internal << number ; // -   123

cout << setw(6) << right << number; //      -123



showpoint / noshowpoint  - 소수점 이하를 보여준다 / 안보여줄수 있으면 보여주지말아라


decimal1의 값이 100.0 , decimal2의 값이 100.12 라면 


cout << noshowpoint << decimal1 << " " << decimal2; //100 100.12

cout << showpoint << decimal1 << " " << decmal2; // 100.000 100.120



fixed / scientific - 고정적인 실수표기법 / 과학적인 표기법


number 의 값이 123.456789라면


cout << fixed << number; // 123.456789

cout << scientific << number; // 123.4568E +02



boolalpha / noboolalpha  - 불리언값을 알파벳으로표기 / 숫자로표기 (true false 1 0)


bready 값이 true 라면


cout << boolalpha << bReady; // true

cout << noboolalpha << bReady // 1




#include <iomanip> 안에있는 조정자



setw() - 컬럼수 정해준다


number의 값이 123 이라면


cout << setw(5) << number; //  __123



setfill() - 빈공간을 다른캐릭터로 채워달라


cout << setfill ('*') << setw(5) << number; // **123



setprecision() - 유효자리 몇자리까지 보여줄건지  ( 반올림 ) 


number의 값이 123.456789라면


cout<< setprecision(7) << number ; // 123.4568


* fixed가 없는경우엔 정수부 + 소수부 기준으로 반올림하여 출력

  fixed가 있는 경우엔 소수부만을 기준으로  반올림하여 출력함




메뉴를 출력해보자


#include<iomanip>


const float coffeePrice = 1.25f;

const float lattePrice = 4.75f;

const float breakfastComboPrice  = 12.104f;


const size_t nameColumnLength  = 20; // 컬럼 길이설정 size_t는 정수절대값의미하는 자료형

const size_t priceColumnLength = 10; 


cout << left << fixed << showpoint << setprecision (2); // 조정자는 이 한줄에서만 영향을 미치는것이 아니라 뒤에 나오는 모든 출력에 영향을 미친다


cout<< setfill( '-' ) << setw(nameColumnLength  + priceCoulumnLength) << "" << endl << setfill ( ' ' ); 


cout << setw(namecolumnLength) << "Name" << setw( priceColumnLength ) << "Price" << endl;


cout << setw(nameColumnLength) << "Coffee" << "$" << coffeePrice << endl;




cout 멤버 메서드 - 조정자를 함수로 쓰는방법 ( 알고만 ) 


조정자

cout << showpos << number; 

멤버메서드

cout.setf(ios_base::showpos);

cout <<number;


조정자

cout << setw(5) << number;

멤버메서드

cout.width(5);

cout <<number;



네임스페이스 ios_base


setf() , unsetf()

width() , fill() , precision()


그리많이 사용되지 않음



setf(flag) / unsetf(flag)


인자 - boolalpha , showbase , uppercase , showpos


setf( flag , flag )


첫번쨰인자 - dec , oct , hex / fixed , scientific / left , right , internal


두번째인자 - basefield / floatfield / adjustfield





+ Recent posts