[코드트리] - 사칙연산을 이행하는 함수

LSDrug·2024년 8월 13일
0

문제풀이

목록 보기
14/21

사칙연산을 이행하는 함수 ⭕

문제

https://www.codetree.ai/training-field/search/problems/functions-that-carry-out-four-factor-operations?&utm_source=clipboard&utm_medium=text


풀이

아주 쉬운 문제. 각 연산하는 수를 받고 연산자를 받은 뒤 연산자의 종류에 따라서 조건문으로 만들어서 계산만 하면 된다.


코드

#include <iostream>
using namespace std;

int x, y;
char a;

void cal() {
    if(a == '+') {
        cout << x << " + " << y << " = " << x + y << endl;
    } else if(a == '-') {
        cout << x << " - " << y << " = " << x - y << endl;
    } else if(a == '*') {
        cout << x << " * " << y << " = " << x * y << endl;
    } else {
        cout << x << " / " << y << " = " << x / y << endl;
    }
}

int main() {
    // 여기에 코드를 작성해주세요.
    cin >> x >> a >> y;

    cal();
    return 0;
}

profile
마약같은 코딩, 마약같은 코딩러

0개의 댓글

관련 채용 정보