문자열을 입력받아서 계산 결과를 출력하는 계산기를 구현하는게 목표이다.
FIFO 인 큐를 이용해서 덧셈 계산하기까지 clear.
괄호는 스택으로 하는게 좋을 것 같은데 일단 오늘은 여기까지.
import java.util.*;
public class Polynomial {
public static void main(String[] args) {}
String expression;
Queue<Integer> numList;
Queue<Character> opList;
Polynomial(String expression) {
this.expression = expression;
numList = new LinkedList<>();
opList = new LinkedList<>();
}
public int calc() {
this.getSplitInfo();
int num = numList.poll();
while (!opList.isEmpty()) {
char op = opList.poll();
switch (op) {
case '+':
num=num+numList.poll();
break;
case '-':
num=num-numList.poll();
break;
case '*':
num=num*numList.poll();
break;
case'/':
num=num/numList.poll();
break;
}
}
return num;
}
public void getSplitInfo() {
String[] tmp = expression.split(" ");
for (String x : tmp) {
if (x.matches("\\d+")) {
numList.offer(Integer.parseInt(x));
}
else opList.offer(x.charAt(0));
}
}
}