Hi, I need some help on my stack i could not input the infix because it will generate error when i compile,and it says that non-static method priority (char)cannot be reference from a static context....Thank you in avdvance.
public InfixToPostfix(int size)
{
stack=new Object[size];
top=0;
}
public boolean isEmpty()
{
return top==0;
}
public boolean isFull()
{
return top==stack.length;
}
public boolean push(Object item)
{
boolean ok=!isFull();
if(ok)
{
stack[top++]=item;
ok= true;
}
return ok;
}
public Object peek()
{
return stack[top-1];
}
public Object pop()
{
Object item=peek();
if(item!=null)
{
stack[top-1]=null;
top--;
}
return item;
}
public int priority(char c)
{
int p=999;
switch(c)
{
case '*': case '/': p=0; break;
case '+': case '-': p=1; break;
}
return p;
}
public static void main(String...args)
{
InfixToPostfix s =new InfixToPostfix(5);
Object[] Infix={0};
Object[] postfix={0
};
int count=0;
char ch='u0000';
System.out.print("Enter infix: ");
for(int i=0;i<s.stack.length;i++){
Infix[i]=new java.util.Scanner(System.in).next();
}
for(int i=0;i<Infix.length;i++)
{
ch=(char)Infix[i];
if(Character.isDigit(ch))
postfix[count++]=ch;
else
{
while(priority((char)s.peek())<=priority(ch))
{
postfix[count++]=s.pop();
}
s.push(ch);
}
}
while(!isEmpty())
{
postfix[count++]=s.pop();
}
postfix[count]=null;
System.out.print(postfix);
}
public String toString()
{
StringBuffer sb=new StringBuffer();
for(int i=top-1;i>-1;--i)
{
sb.append(stack[i]+",");
}
return sb.toString();
}
}