Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InfixtoPostfix2
- {
- Stos stos = new Stos();
- String postFix="";
- public boolean isOperator(char c){
- return c == '+'||c== '-'||c=='*'||c=='/';
- }
- public int priorytet(char operator)
- {
- if(operator=='+' || operator=='-') return 1;
- else if(operator=='*' || operator=='/') return 2;
- else return 0;
- }
- public void proccessOperator(char op)
- {
- char topOp ;
- if(stos.isEmpty())
- {
- stos.push(op);
- }else{
- topOp=stos.peek();
- if(priorytet(op)>priorytet(topOp))
- {
- stos.push(op);
- }else{
- while(stos.isEmpty()==false&&priorytet(op)<=priorytet(topOp)){
- postFix+= stos.pop();
- if(stos.isEmpty()==false){
- topOp=stos.peek();
- stos.push(op);
- }
- }
- }
- }
- }
- public void convert(String infix)
- {
- for(int i =0;i<infix.length();i++)
- {
- if(isOperator(infix.charAt(i))==false){
- postFix+=infix.charAt(i);
- }else if(isOperator(infix.charAt(i))==true){
- proccessOperator(infix.charAt(i));
- }else{
- System.out.println("Zle dane");
- }
- }
- System.out.print(postFix);stos.display();
- }
- public static void main(String[] args)
- {
- InfixtoPostfix2 test2 = new InfixtoPostfix2();
- InfixtoPostfix2 test3 = new InfixtoPostfix2();
- test2.convert("1+2/3"); //wynik 123/+
- System.out.println();
- test3.convert("2+3-1"); //wynik 23+1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement