본문 바로가기
Problem Solving/Baekjoon

[백준] 5613 계산기 프로그램 - Implementation / Java

by graycode 2025. 3. 12.

 문제 링크

https://www.acmicpc.net/problem/5613

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {

    static int n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        n = Integer.parseInt(br.readLine());
        String s;
        while ((s = br.readLine()).charAt(0) != '=') calc(s.charAt(0), Integer.parseInt(br.readLine()));

        bw.write(String.valueOf(n));
        bw.flush();
    }

    private static void calc(char opt, int opr) {
        switch (opt) {
            case '+':
                n += opr;
                break;
            case '-':
                n -= opr;
                break;
            case '*':
                n *= opr;
                break;
            default:
                n /= opr;
        }
    }

}

댓글