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.

View Poll Results: who somebady help my????

Voters
0. You may not vote on this poll
  • 1

    0 0%
  • 2

    0 0%
  • 3

    0 0%
  • 4

    0 0%
Results 1 to 2 of 2

Thread: help my to write the true cod in java stack to find postfix evaluation.....

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry help my to write the true cod in java stack to find postfix evaluation.....

    I am writen this cod but didn't run with me >>>Iam tired please help me to make cod is run.....


    import java.util.*;

    class infixToPostfix
    {
    //data fill
    private static String expression;
    public static String postfix;
    private Stack<Character> stack = new Stack<Character>();

    //Constructor one argement"-----------"
    public infixToPostfix(String infixExpression)
    {
    expression = infixExpression;
    }

    //convert the expression to postfix
    public String convert()
    /**/{
    //local Variables
    char ch = ' ';
    String infix = "";//is input
    String postfix = "";//is output




    for(int i = 0; i < expression.length(); i++)
    {
    ch = expression.charAt(i);

    if(isOperator(ch))
    {
    while(!stack.empty() && precedence(stack.peek())>= precedence(ch))
    postfix += stack.pop() + " ";
    stack.push(ch);

    }
    else if(ch == '(')
    {
    stack.push(ch);
    }
    else if(ch== ')')
    {
    while(!stack.peek().equals('('))
    postfix += stack.pop() + " ";
    stack.pop();
    }
    /* if ones*/ else
    {
    if(Character.isDigit(ch)&&(i+1)< expression.length()&& Character.isDigit(expression.charAt(i+1)) )//is two digits)
    {
    postfix += ch;
    }
    else if(Character.isDigit(ch))//is one digit
    {
    postfix += ch + " ";
    }
    else
    {
    postfix += ch;//
    }
    }// elseIam using method isDigit()
    }//for loop

    while(!stack.empty())
    {
    postfix += stack.pop() + " ";
    }

    return postfix;
    }//convert


    public static int precedence(char operator)
    {
    if(operator == '+' || operator =='-')
    return 1;
    else if(operator == '*' || operator == '/')
    return 2;
    else
    return 0;
    }// precedence

    public boolean isOperator(char element)
    {
    if(element == '*' || element == '-' || element == '/' || element == '+')
    return true;
    else
    return false;
    }//isOperator
    }


    class evaluation{
    int finalResult;
    Stack <Integer> solu= new Stack<Integer>();
    int A,B,result;


    public int calculate() {


    for(int i=0;i<infixToPostfix.postfix.length();i++){
    char c=infixToPostfix.postfix.charAt(i);

    switch (c){
    case '+':
    A=solu.pop();
    B=solu.pop();
    result=A + B;
    solu.push(result);
    break;
    case '-':
    A=solu.pop();
    B=solu.pop();
    result=A - B;
    solu.push(result);
    break;
    case '*':
    A=solu.pop();
    B=solu.pop();
    result=A * B;
    solu.push(result);
    break;
    case '/':
    A=solu.pop();
    B=solu.pop();
    result=A / B;
    solu.push(result);
    break;
    case '%':
    A=solu.pop();
    B=solu.pop();
    result=A % B;
    solu.push(result);
    break;
    default:
    solu.push(Integer.parseInt(Character.toString(c))) ;
    break;





    }}

    finalResult = solu.pop();
    return finalResult;



    }

    }




    public class TEST1{
    public static void main(String []args) {
    infixToPostfix s=new infixToPostfix("5+(9/2*1+1))");
    evaluation a=new evaluation();
    System.out.println(s.convert());

    System.out.println(a.calculate());


    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help my to write the true cod in java stack to find postfix evaluation.....

    If you want help you are going to have to ask a question, and please use the code tags, and there is absolutely no reason your thread should be a poll. See the links in my signature for how you can help yourself get help, and please read http://www.javaprogrammingforums.com...uncements.html

Similar Threads

  1. [SOLVED] Translate sentence to morse cod?
    By CSUTD in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 27th, 2012, 02:38 PM
  2. [SOLVED] EVALUATION OF POSTFIX NOTATION
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 7th, 2011, 05:24 PM
  3. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 07:35 AM
  4. how to find stack size in java
    By aditiya in forum Java Theory & Questions
    Replies: 6
    Last Post: July 8th, 2011, 06:29 AM
  5. [SOLVED] evaluating postfix expressions using stack
    By lieles in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2011, 11:48 AM

Tags for this Thread