Jump to content

postfix to infix

- - - - -

  • Please log in to reply
4 replies to this topic

#1
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
hi again... and thanks for everyone who helped meh until now...
i've got another bit of problem... it says incompatible types on the String b=theStack.pop() and String a=theStack.pop() lines... how can i resolve this??? thanks!!!

public String postfix(){

   		for (int i=0; i<input.length(); i++){

   			char c=input.charAt(i);

   			if (c == '+' || c == '-' || c == '*' || c == '/'){

   				String b=theStack.pop();

   				String a=theStack.pop();

   				theStack.push(a+c+b);

   			}

   			else{

   				theStack.push(String.valueOf(c));

   			}

   			output=output+c;

   		}

   		return output;

   }


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
What does your declaration and initialization of "theStack" look like?

#3
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
ah yes... i forgot...
the whole code axually looks like this....

import java.util.Stack;

import java.util.Scanner;

import java.io.*;

import javax.swing.*;

public class act2ex {


   private Stack theStack;

   private String input;

   private String output = "";

   public act2ex(String in) {

      input = in;

      int stackSize = input.length();

      theStack = new Stack(stackSize);

   }


   public String doTrans() {

      for (int j = 0; j < input.length(); j++) {

         char ch = input.charAt(j);

         switch (ch) {

            case '+':

            case '-':

            gotOper(ch, 1);

            break;

            case '*':

            case '/':

            gotOper(ch, 2);

            break;

            case '(':

            theStack.push(ch);

            break;

            case ')':

            gotParen(ch);

            break;

            default:

            output = output + ch;

            break;

         }

      }


      while (!theStack.isEmpty()) {

         output = output + theStack.pop();

      }


      return output;

   }



   public void gotOper(char opThis, int prec1) {

      while (!theStack.isEmpty()) {

         char opTop = theStack.pop();

         if (opTop == '(') {

            theStack.push(opTop);

            break;

         }

         else {

            int prec2;

            if (opTop == '+' || opTop == '-')

            prec2 = 1;

            else

            prec2 = 2;

            if (prec2 < prec1) {

               theStack.push(opTop);

               break;

            }

		    else

            output = output + opTop;

         }

      }

      theStack.push(opThis);

   }



   public void gotParen(char ch){

      while (!theStack.isEmpty()) {

         char chx = theStack.pop();

         if (chx == '(')

         break;

         else

         output = output + chx;

      }

   }



   public static void main(String[] args)throws IOException {


   	  String choice=JOptionPane.showInputDialog ("Enter conversion choice: "+"\n0: Exit Program"+"\n1: Infix to Postfix"+"\n2: Postfix to Infix");

   	  int cho=Integer.parseInt (choice);


   	  while (choice!=null){


   	  	switch (cho){

   	 	 	case 0:

   		  		System.exit(0);

   		  		break;

   		  	case 1:

   		  		String input = JOptionPane.showInputDialog ("Enter infix notation:");

      			String output;

     			act2ex theTrans = new act2ex(input);

      			output = theTrans.doTrans();

      			JOptionPane.showMessageDialog (null,"Infix is: "+input+ "\nPostfix is " + output + '\n');

      		case 2:

      			input=JOptionPane.showInputDialog ("Enter postfix notation:");

      			output=theTrans.postfix();

      			JOptionPane.showMessageDialog (null, "Postfix is: "+input+"\nInfix is "+output+'\n');


  	 	  }



   	  }

   }


   public String postfix(){

   		for (int i=0; i<input.length(); i++){

   			char c=input.charAt(i);

   			if (c == '+' || c == '-' || c == '*' || c == '/'){

   				String b=theStack.pop();

   				String a=theStack.pop();

   				theStack.push(a+c+b);

   			}

   			else{

   				theStack.push(String.valueOf(c));

   			}

   			output=output+c;

   		}

   		return output;

   }



   class Stack {

      private int maxSize;

      private char[] stackArray;

      private int top;

      public Stack(int max) {

         maxSize = max;

         stackArray = new char[maxSize];

         top = -1;

      }

      public void push(char j) {

         stackArray[++top] = j;

      }

      public char pop() {

         return stackArray[top--];

      }

      public char peek() {

         return stackArray[top];

      }

      public boolean isEmpty() {

         return (top == -1);

     }

   }

}


#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You're trying to set a string equal to a char.
Try changing the string b and a to char instead.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think that when you add the generic type to the stack declaration + creation the compiler will tell you that there's something tricky going on there :)
Stack<Character> theStack = new Stack<Character>(stackSize);





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users