[java][이칙연산 계산기 (+,-)]
the space coding
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++)// s의 길이 만 큼
{
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());
}
}