본문 바로가기
개인 개발 공부/C ++

연산자

by minjoothi 2024. 11. 9.

산술연산자

#include <iostream>

int main()
{
    using namespace std;

    int numOfWatermelons = 3; // 처음 가진 수박
    int bonus = 2; // 보너스로 받은 수박

    cout << "I had " << numOfWatermelons << " watermelons" << endl;

    numOfWatermelons = numOfWatermelons + bonus;

     cout << "I have " << numOfWatermelons << " watermelons" << endl;

   return 0; 
}

#include <iostream>

// 연산자


int main()
{
    using namespace std;

    int a = 10;
    int b = 2;

    cout << "a+b = " << a+b << endl;
    cout << "a-b = " << a-b << endl;
    cout << "a*b = " << a*b << endl;
    cout << "a/b = " << a/b << endl;
    cout << "a%b = " << a%b << endl; // 나머지를 구하는 연산자

    int c, d, e;
    c = d = e = 10; // 10을 e에 넣고 e를 d에 넣고 d를 c에 넣기;
    e += d; // e = e + d; 

    cout << "c= " << c << endl;
    cout << "d= " << d << endl;
    cout << "e= " << e << endl;

    return 0;
}

 

비교연산자

#include <iostream>

int main()
{
    using namespace std;   // shift + alt + F = 코드정리


    int numOfWatermelons = 3;

    cout <<"watermelons: " << numOfWatermelons << endl;
    cout << "numOfWatermelons >= 2 : " << (numOfWatermelons >= 2) << endl;

    return 0;
}

 

컴퓨터는 1은 참이고 0 은 거짓이므로

numOfWatermelons 가 2보다 크냐를 물어봤을때 결과값이 1이 나왔으므로 답은 참이다.

 

논리연산자

#include <iostream>

int main()
{
    using namespace std;

    int watermelons = 3;

    cout << "watermelons == 3: " << (watermelons == 3) << endl; // 진실이면 1출력
    cout << "!(watermelons == 3:)" << !(watermelons ==3) << endl; // 거짓이면 0출력
    cout << "!true : " << !true << endl;  // 거짓값 출력

/*
논리연산자
true && true => true
true && false => false
false && true => false
false && false => true

true || true => true
true || false => true
false || true => true
false || false => false
*/

    cout << "(false && false): " << (false && false) << endl;
    cout << "(false || true): " << (false || true) << endl; // 논리연산자

    return 0;
}

증감연산자

#include <iostream>

int main()
{
    using namespace std;

    int watermelons = 3;

    cout << " watermelons: " << watermelons << endl;
    cout << " watermelons++ " << watermelons++ << endl; // ++를 뒤에 붙이면 다음줄부터 1 증가
    cout << " watemelons: " << watermelons << endl;

    int carrots = 4;

    cout << " carrots: " << carrots << endl;
    cout << " ++carrots " << ++carrots << endl; // ++를 앞에 붙이면 바로 1증가
    cout << " carrots: " << carrots << endl;

과제

#include <iostream>

int main()
{
    using namespace std;

    int year;
    int month;
    int day;

    cout << " 몇년생이야? " << endl;
    cin >> year;

    cout << " 몇월생이야? " << endl;
    cin >> month;

    cout << " 며칠생이야? " << endl;
    cin >> day;

    cout << year << "년 " << month << "월  " << day << "생입니다. " << endl;

'개인 개발 공부 > C ++' 카테고리의 다른 글

1-5 반복문과 다중반복문  (0) 2024.11.09
조건문  (0) 2024.11.09
1-2. 변수와 자료형 그리고 입출력 함수  (0) 2024.11.09
GPT를 통해 알아본 유니티와 언리얼, C#과 C++  (0) 2024.11.08
What is C, C#, C++ ?  (0) 2024.09.17