그냥 좃대딩이야 형들...
내가 연산자 우선순위 까지는 구현 했는데
여기서 그 입력받은 값들을 합산해서 sum을 출력하는 걸 못하겠어... 여기에 어떤 메소드 추가해서 넣어야하는지 아니면 클래스를 하나 더 구현해야 하는지...
class Stack {
int top;
char[] stack = new char[100];
Stack() { top = -1; }
public void push(char data) { // push
if(top == stack.length)
System.out.println("Stack full");
else stack[++top] = data;
}
public char pop() { // pop
if(top < 0) {
System.out.println("empty");
return 'a';
}
else return stack[top--];
}
}
class Postfix extends Stack {
Stack s = new Stack();
int PIS(char c) { // 연산자 우선순위
switch(c) {
case '(' :
case ')' :
return 0;
case '+' :
case '-' :
return 1;
case '*' :
case '/' :
return 2;
}
return -1;
}
void itop(String exp) {
char[] c = new char[20];
char ch;
for(int i = 0; i < exp.length(); i++)
c = exp.toCharArray(); // 문자열을 배열로 만드는 함수
for(int j = 0; j < c.length; j++) {
ch = c[j];
if(ch == '+' || ch == '-' || ch == '*' || ch == '/') {
while(s.top != -1 && PIS(s.stack[s.top]) >= PIS(ch))
System.out.print(s.pop());
s.push(ch);
}
else if(ch == '(') s.push(ch);
else if(ch == ')') {
while(s.stack[s.top] != '(')
System.out.print(s.pop());
s.top = s.top - 1;
}
else System.out.print(ch);
}
while(s.top != -1)
System.out.print(s.pop());
}
}
public class Assignment2 {
public static void main(String[] args) {
String exp = "2.5+1.2*(3.0-(4.0-5.0)*2.0)+3.5";
Postfix a = new Postfix();
a.itop(exp);
}
}
좀 허접하지만....
조금만 시간내서 도와줘 형들
댓글 0