chucky3   10달 전

흠..완전 맞다고 생각하다가 틀릴때마다 답답합니다..


import java.util.Scanner;

class StackNode{
char data;
StackNode link;
}
class LinkedStack{
private StackNode top;
public void push(char num){
StackNode newNode=new StackNode();
newNode.data=num;
newNode.link=top;
top=newNode;
}
public char pop(){
if(empty()){
return 0;
}
char item=top.data;
top=top.link;
return item;
}
public boolean empty(){
if(top==null){
return true;
}
else{
return false;
}
}
}

public class Test{
public static void main(String arg[]){
LinkedStack s = new LinkedStack();
Scanner input = new Scanner(System.in);
String str;
char c;
int count=0;
str=input.nextLine();
int len=str.length();
for(int i=0;i<len;i++){
if(count!=-1){
c=str.charAt(i);
}else{
c=')';
count+=2;
}
switch(c){
case '(':
count++;
break;
case '+':
case '-':
s.push(c);
break;
case '*':
case '/':
if(count!=0){
s.push(c);
}else{
s.push(c);
System.out.print(str.charAt(i+1));
count--;
}
break;
case ')':
c=s.pop();
System.out.print(c);
count--;
break;
default:
System.out.print(c);
break;
}
}
while(!s.empty()){
c=s.pop();
System.out.print(c);
}
}
}

댓글을 작성하려면 로그인해야 합니다.