Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: convert infix to postfix

  1. #1
    Junior Member tina G's Avatar
    Join Date
    Mar 2010
    Location
    philippines
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Post convert infix to postfix

    import java.io.IOException;
    import java.io.*;
    import java.util.Arrays;
     
    public class datastruct {
      private Stack theStack;
     
      public static void main(String[] args) throws IOException {
     
      	BufferedReader A = new BufferedReader (new InputStreamReader(System.in));
      	String mathex;
        System.out.println("enter a mathematical expresion:" );
    	  mathex = A.readLine();
     
        String input = null;
        String output;
        datastruct theTrans = new datastruct(mathex);
        output = theTrans.doTrans(); 
        System.out.println("postfix is" + output + '\n');
        System.out.println("infix is" + mathex + '\n');
     
     
      }
     
      private String input;
     
      private String output = "";
     
      public datastruct(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();
     
        }
     
        System.out.println(output);
        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; 
        }
      }
     
     
      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);
     
        }
      }
     
    }


    hi, just want to ask how can I input the "System.out.println("invalid")"
    it is when the user did not satisfy the condition it will display invalid... help me please?
    I am stuck here...^^thankyou


  2. #2
    Junior Member tina G's Avatar
    Join Date
    Mar 2010
    Location
    philippines
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: convert infix to postfix

    help me..^^, thaks!!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: convert infix to postfix

    There are a few cases where you should be printing out invalid: parenthesis mismatch, an out of place operator/token, or an invalid token. Your program never actually parses in all the different token types, only parentheses and operators.

    You can see my implementation of a simple shunting-yard algorithm which converts from infix to postfix, and also has some basic error checking via throwing exceptions.

    *note: I don't think I had the commas implemented correctly in that code, or the functions with multiple parameters working in that code. Simply parsing of arithmetic expressions using +,-,*,/,^, and () should all work, though.

    While it doesn't ever actually print out the post-fix evaluation order, I'm sure you can modify your code to do so fairly easily (note that the parser's toString() method has been over-ridden to print out a calculated in-fix notation with parenthesis to force the order of operations).

Similar Threads

  1. Convert DOC,XLS to PDF with Java
    By comm in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 2nd, 2013, 04:10 AM
  2. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 09:33 AM
  3. Infix to Prefix parser
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 1
    Last Post: November 8th, 2009, 01:11 AM
  4. Infix to Prefix
    By Sasarai in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 7th, 2009, 10:03 PM
  5. convert GSM to PCM (wav)
    By cilang in forum Java Theory & Questions
    Replies: 4
    Last Post: August 7th, 2009, 03:46 AM