카테고리 없음

객체지향언어 1차시

한 면만 쓴 종이 2022. 3. 10. 01:12

2-1>

#include <iostream>

int main()
{
 std::cout << "이 프로그램은 프로그램의 구조를 알아보기 위한 ";
 std::cout << "간단한 C++ 프로그램입니다." << std::endl;
 std::cout << "이번 장과 이후의 내용을 통해 "; 
 std::cout << "C++ 프로그래밍 언어와 관련된 다양한 것을 살펴보겠습니다.";
 return 0;
}

std::cout    ->    모니터

<<           ->    출력

endl         ->     \n

 

2-2>

/**************************************************************
 * 별로 사각형을 출력하는 프로그램 
 **************************************************************/
#include <iostream>
using namespace std;

int main()
{
  // 별 기호로 정사각형 출력
  cout << "******" << endl;
  cout << "******" << endl;
  cout << "******" << endl;
  cout << "******" << endl;
  cout << "******" << endl;
  cout << "******";
  return 0;
}

using namespace std;    ->    std라는 이름의 공간을 사용하겠다는 의미 (같은 이름을 가진 함수가 여러 개의 namespace에 존재하는 경우 이 함수를 사용할 때 충돌이 발생하기 때문에 이를 방지하기 위함)

std를 선언해줘서 std::를 쓰지 않고 cout과 cin을 쓸 수 있음

 

2-3>

/************************************************************* 
 * 키보드로부터 2개의 숫자 값을 입력받은 뒤에               *
 * 두 값을 더한 후 출력하는 프로그램                          * 
 *************************************************************/
#include <iostream>
using namespace std;

int main()
{
  // 선언
  int num1;
  int num2;
  int sum;
  // 입력받기   
  cout << "첫 번째 숫자 입력: "; 
  cin >> num1;
  cout << "두 번째 숫자 입력: ";
  cin >> num2;
  // 계산과 결과 저장  
  sum = num1 + num2;
  // 출력  
  cout << "두 숫자의 합: " << sum;
  return 0;
}

cout  <<   ->    컴퓨터로 출력 명령

cin >>      ->    변수에 저장

 

2-4>

/*************************************************************
 * 동전과 지폐들의 금액 합계를 구하는 프로그램               *
 *************************************************************/
#include <iostream>
using namespace std;

int main()
{
  // 상수 정의
  const unsigned int pennyValue = 1;
  const unsigned int nickelValue = 5;
  const unsigned int dimeValue = 10;
  const unsigned int quarterValue = 25;
  const unsigned int dollarValue = 100;
  // 변수 정의(각 코인의 수)
  unsigned int pennies;
  unsigned int nickels;
  unsigned int dimes;
  unsigned int quarters;
  unsigned int dollars;
  // 전체 값을 나타내는 변수 선언
  unsigned long totalValue;
  // 코인 입력받기
  cout << "페니의 수: ";
  cin >> pennies;
  cout << "니켈의 수: ";
  cin >> nickels;
  cout << "다임의 수: ";
  cin >> dimes;
  cout << "쿼터의 수: ";
  cin >> quarters;
  cout << "달러의 수: ";
  cin >> dollars;
  // 전체 금액 계산
  totalValue = pennies * pennyValue + nickels * nickelValue + 
               dimes * dimeValue + quarters * quarterValue + dollars * dollarValue;
  // 결과 출력
  cout << "전체 값은 " << totalValue << "페니입니다." ;
  return 0;
}

 

2-5>

/**************************************************************
 * 3회 거래 후의 계좌 잔액을 구하는 프로그램                  *
 **************************************************************/
#include <iostream>
using namespace std;

int main()
{
  // 변수 선언
  int balance = 0;
  int transaction;
  // 첫 번째 거래 후에 잔액 조정
  cout << "첫 번째 거래 금액 입력: ";
  cin >> transaction;
  balance = balance + transaction;
  // 두 번째 거래 후에 잔액 조정
  cout << "두 번째 거래 금액 입력: ";
  cin >> transaction;
  balance = balance + transaction;
  // 세 번째 거래 후에 잔액 조정
  cout << "세 번째 거래 금액 입력: ";
  cin >> transaction;
  balance = balance + transaction;
  // 최종 잔액 출력    
  cout << "계좌의 최종 잔액은 " << balance << "달러입니다."; 
  return 0;
}

 

2-6>

/*
3가지 정수 자료형의 크기를 확인하는 프로그램
*/

#include <iostream>
using namespace std;

int main()
{
    cout << "short int의 크기는 " <<sizeof(short int) << "바이트입니다." << endl;
    cout << "int의 크기는 " <<sizeof(int) << "바이트입니다." << endl;
    cout << "long int의 크기는 " << sizeof(long int) << "바이트입니다." << endl;
    return 0;
}

short int : 2byte

int : 4byte

long int : 4byte

 

2-7>

/*
변수를 초기화할 때 리터럴을 사용하는 프로그램
*/

#include <iostream>
using namespace std;

int main()
{
    //변수 선언과 초기화
    int x = -1245;
    unsigned int y = 1245;
    unsigned int z = -2367;
    unsigned int t = 14.56;
    //초기화된 값 출력
    cout << x << endl;
    cout << y << endl;
    cout << z << endl;
    cout << t;
    return 0;
}

실행결과>

-1245

1245

4294964929    // 음수 값이 양수 값으로 바뀜 (unsigned라서)

14    // 소수점 아래 부분 잘림

 

2-8>

/*
리터럴 값을 단독으로 사용하는 프로그램
*/

#include <iostream>
using namespace std;

int main()
{
    //변수 선언
    int x;
    unsigned long int y;
    //할당
    x = 1456;
    y = -14567;
    //출력
    cout << x << endl;
    cout << y << endl;
    cout << 1234 << endl;
    cout << 143267L << endl;    //이게되네
    return 0;
}

실행결과>

1456

4294952729    // unsigned로 정의한 변수인데 음수를 할당해서 오류 발생

1234

143267    // long int

 

2-9>

/*
char 자료형의 변수를 선언하고 초기화하는 프로그램
*/

#include <iostream>
using namespace std;

int main()
{
    char first = 'A';
    char second = 65;
    char third = 'B';
    char fourth = 66;
    //값 출력
    cout << "first의 값: " << first << endl;
    cout << "second의 값: " << second << endl;
    cout << "third의 값: " << third << endl;
    cout << "fourth의 값: " << fourth;
    return 0;
}

 

2-10>

/*
이스케이프 문자를 사용하는 프로그램
*/
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello\n";
    // \t : 탭 문자 삽입
    cout << "Hi\t friends." << endl;
    // \b : 이전 글자 삭제
    cout << "Buenos dias  \bamigos." << endl;   //중간에 2칸 띄어쓰기
    // \r : 줄의 앞부분으로 커서를 옮기고 다시 입력 -> 앞의 내용 삭제
    cout << "Hello\rBonjour mes amis." << endl;
    cout << "This is a single quote\'." << endl;
    cout << "This is a double quote\"." << endl;
    cout << "This is how to print a backslash \\.";
}

\b    :    백스페이스

 

2-11>

/*
불 변수와 값을 이용하는 프로그램

****************************************************
0은 flse로, 0이 아닌 값은 true로 해석
*/

#include <iostream>
using namespace std;

int main()
{
    //변수 선언
    bool x = 123;
    bool y = -8;
    bool z = 0;
    bool t = -0;
    bool u = true;
    bool v = false;
    //값 출력
    cout << "x의 값: " << x << endl;
    cout << "y의 값: " << y << endl;
    cout << "z의 값: " << z << endl;
    cout << "t의 값: " << t << endl;
    cout << "u의 값: " << u << endl;
    cout << "v의 값: " << v << endl;
    return 0;
}

 

실행결과>
x의 값: 1   // 123은 1로 변환
y의 값: 1
z의 값: 0
t의 값: 0
u의 값: 1
v의 값: 0

 

불 리터럴

    - 0은 false로, 0이 아닌 값은 true로 해석

 

2-12>

/*
원의 반지름을 기반으로 둘레와 면적을 구하는 프로그램
*/
#include <iostream>
using namespace std;

int main()
{
    //상수선언
    const double PI = 3.14159;
    //변수 3개 선언
    double radius;
    double perimeter;
    double area;
    //반지름 입력받기
    cout << "원의 반지름 입력: ";
    cin >> radius;
    //둘레와 면적을 계산하고 변수에 저장
    perimeter = 2 * PI * radius;    // 2는 상수
    area = PI * PI * radius;
    //반지름, 둘레 , 면적 출력
    cout << "반지름: " << radius << endl;
    cout << "둘레: " << perimeter << endl;
    cout << "면적: " << area;
    return 0;
}

부동 소수점 자료형

부동 소수점 자료형 접미사
float f 또는 F 12.23F, 12345.45F, -1436F
double 없음 1425.36, 1234.34, 123454
long double l 또는 L 2456.23L, 143679.00004L, -0.02345L