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 2 of 2

Thread: infix to prefix

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default infix to prefix

    hey guys i m having a problem with infix and prefix
    here is the code
    import java.util.StringTokenizer;
    import static java.lang.Math.pow;
    import java.util.Stack;
     
    public class Calculator {
     
           static int precedence(char op) {
                  switch(op) {
                  case '+' :
                  case '-' : 
                           return 5;
                  case '*' :
                  case '/' :
                           return 10;
                  default :
                           throw new IllegalArgumentException("λαθος εκφραση");
                   }
             }
           public static String infixToPostfix(String infix) {
                  StringTokenizer tokenizer = new StringTokenizer (infix);
                  String postfix = "";
                  Stack opStack = new Stack ();
     
                  while (tokenizer.hasMoreTokens()) {
                         String token = tokenizer.nextToken();
                         char c = token.charAt(0);
                         if (Character.isDigit(c)){
                                postfix +=(token + " ");
                         }
                         else {
                                 while (!opStack.empty()){
                                        char top = ((Character)opStack.peek()).charValue();
                                        if (precedence(top) >= precedence(c)) {
                                                postfix += (top + " ");
                                                opStack.pop();
                                         }
                                         else {
                                                break;
                                               }
                                         }
                                         opStack.push(new Character (c));
                              }
                          }
     
                   while (!opStack.empty()) {
                          char top = ((Character)opStack.pop()).charValue();
                          postfix += (top + " ");
                    }
                    return postfix;
            }
     
         public static int evalPostfix(String postfix) {
                  StringTokenizer tokenizer = new StringTokenizer(postfix);
                  Stack valStack = new Stack();
     
                  while(tokenizer.hasMoreTokens()) {
                         String token = tokenizer.nextToken();
                         char c = token.charAt(0);
                         if (Character.isDigit(c)) {
                                valStack.push(new Integer(token));
                          }
                          else {
                                  int rightVal = ((Integer)valStack.pop()).intValue();
                                  int leftVal = ((Integer)valStack.pop()).intValue();
                                  int rslt;
     
                                  switch(c) {
                                  case '+' : rslt = leftVal + rightVal;break;
                                  case '-' : rslt = leftVal - rightVal;break;
                                  case '*' : rslt = leftVal * rightVal;break;
                                  case '/' : rslt = leftVal / rightVal;break;
                                  case '^' : rslt = pow(leftVal,rightVal);break;
                                  default:
                                         throw new IllegalArgumentException("λαθος εκφραση");
                                    }
                                   valStack.push(new Integer(rslt));
                                }
                         }
                     int rslt = ((Integer)valStack.pop()).intValue();
                     if (!valStack.empty()){
                              throw new IllegalArgumentException("λαθος εκφαρση");
                      }
                          return rslt;
              }
          public static void main(String[] args)  {
                 String infix = args[0];
                 String postfix = infixToPostfix(infix); 
                 System.out.println("postfix"+postfix);
                 int value = evalPostfix(postfix);
                 System.out.println("value: " + value);
             }
    }
    it says in line 72
    possible loss of precision
    required: int
    found: double
    the pow only takes int values?not doubles?how do i fix it?


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: infix to prefix

    I guess this is what you are asking:

    double d = 1.2345;
    int i = (int) d;

    Let us know if there are any further problem
    Thanks and regards,
    Sambit Swain

Similar Threads

  1. Infix to Prefix
    By cscstudent in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 27th, 2011, 12:28 AM
  2. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 10:20 AM
  3. convert infix to postfix
    By tina G in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 11th, 2010, 01:46 AM
  4. Infix to Prefix parser
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 1
    Last Post: November 8th, 2009, 01:11 AM
  5. Infix to Prefix
    By Sasarai in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 7th, 2009, 10:03 PM