JAVA/자바 문제
이칙 연산 계산기 (LinkedList)
해병1188기
2018. 3. 21. 20:02
728x90
반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import java.util.*; public class Calculator {// public static void main(String[] args) { LinkedList<Integer> numList = new LinkedList<Integer>(); //제너릭 LinkedList<Character> opList = new LinkedList<Character>(); Scanner sc = new Scanner(System.in);//입력 String s = sc.nextLine(); String num = ""; for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i);//문자중에 인텍스 위치에 해당되는 문자 추출 if(ch == '+') { numList.add(Integer.parseInt(num)); opList.add(ch); num = ""; continue; } else if(ch == '-') { numList.add(Integer.parseInt(num)); opList.add(ch); num = ""; continue; } num += ch; } numList.add(Integer.parseInt(num)); while(!opList.isEmpty()) { int prevNum = numList.poll(); int nextNum = numList.poll(); char op = opList.poll(); if(op == '+') { numList.addFirst(prevNum + nextNum); } else if(op == '-') { numList.addFirst(prevNum - nextNum); } } System.out.println(numList.poll()); } } | cs |
2+3-3+등등 연속해서 계산된다 (+ ,)-
728x90
반응형