Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
java code to evaluate a arithmetic expression....
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Pradyut Bhattacharya  
View profile  
 More options Dec 30 2007, 12:40 pm
From: "Pradyut Bhattacharya" <prady...@gmail.com>
Date: Sun, 30 Dec 2007 23:10:14 +0530
Local: Sun, Dec 30 2007 12:40 pm
Subject: java code to evaluate a arithmetic expression....

Java code to evaluate in arithmetic expression......

following order...
Division
Multiplication
Subtraction
Addition

The code: -

--------------------------------------------------------------------------- --------------------------------------------------------------------------- ------------------------------------------------------

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String a = "4-6-1+3*9/2"; //1*3+2*7-1-1
        String[] b = null;
        String c = null;
        //double[] d =null;
        Main obj = new Main();
        //double d1 = 0;
        System.out.println(obj.check(a));
        /*b = obj.mul(a);
        //a = b[0].substring(0, b[1].length()-1) + func2(func3(b[0].trim()),
b[1]).toString() + b[]
        System.out.println(b[0].substring(obj.last(b[0])+1, b[0].length()));
        System.out.println(b[1].substring(0, obj.first(b[1])));
        c = obj.mult(b[0].substring(obj.last(b[0])+1, b[0].length()),
b[1].substring(0, obj.first(b[1])));
        System.out.println(d1);

        a = b[0].substring(0, obj.last(b[0])+1) +d1 + b[1].substring(
obj.first(b[1]), b[1].length());
        System.out.println("The string is: " + a);

        //System.out.print(func2(b[0].substring(0, func3(b[0].trim())),
b[1].substring(func4(b[1].trim()), ));
        for(int i=0; i<b.length; i++)
            System.out.println(b[i]);

        //System.out.println(b[1].substring(0,b[1].indexOf('+')));

        //String f = b[1].substring(0,b[1].indexOf('+'));

        //d1 = obj.func2(b[0], f);
        System.out.println("The addition: " + d1);*/
    }
    double func(String a) {
        double d = 0;

        return d;
    }
    String mult(String a, String b) {
        double c = func1(a) * func1(b);
        return (Double.toString(c));
    }
    String minus(String a, String b) {

        double c = func1(a) - func1(b);
        System.out.println(c);
        return (Double.toString(c));
    }
    String add(String a, String b) {
        double c = func1(a) + func1(b);
        return (Double.toString(c));
    }

    String divide(String a, String b) {
        double c = func1(a) / func1(b);
        return (Double.toString(c));
    }

    double func1(String a) {
        double d =0;
        try {
            d = Double.parseDouble(a);
        } catch (NumberFormatException n1) {
            System.out.println(n1);
        }
        return d;
    }
    /*String[] mul(String a) {
        String[] b = null;
        b = a.split("\\*");
        return b;
    }
    double mul1(String[] a) {
        double c = 0;
        try{

        } catch()
        return c;
    }*/
    /*int last(String a) {
        int c=0;
        char d;
        for(int i =a.length()-1; i>0; i=i-1) {
            if(a.charAt(i)=='+' || a.charAt(i)=='-' || a.charAt(i)=='*' ||
a.charAt(i)=='/')
            return i;
        }
        return c;
    }
    int first(String a) {
        int i=0;
        for(i =0; i<a.length(); ++i) {
            if(a.charAt(i)=='+' || a.charAt(i)=='-' || a.charAt(i)=='*' ||
a.charAt(i)=='/')
            return i;
        }
        return i;
    }*/
    String check(String a) {
        String[] b=null;
        if(a.contains("+")) {
            //System.out.println("This is inside minus");
            b = a.split("\\+",2);

            a=add(check1(b[0]),check(b[1])) ;
            return a;
        }
        else
            return check1(a);
    }

    private String check1(String a) {

        String[] b=null;
        //System.out.println(a);
        if(a.contains("-")) {
            //System.out.println("This is inside minus");
            /*if(a.indexOf('-')!=0) {
                b = a.split("\\-",2);
            b[1] = "-" + b[1];
            a = add(check2(b[0]), check1(b[1])) ;
            }
            else {
                a = add(check2(b[0]), b[1]) ;
            }*/
            if(a.indexOf('-')==0)
            {
                a =a.substring(1);
                if(a.contains("-")) {
                    b = a.split("-", 2);
                    b[0] = "-" + b[0];
                    b[1] = "-" + b[1];
                    a = add(check2(b[0]), check1(b[1]));
                }
                else
                    a = "-"+a;
            } else {
                b = a.split("-", 2);
                //b[0] = "-" + b[0];

                b[1] = "-" + b[1];
                a = add(check2(b[0]), check1(b[1]));
            }
            return a;
        }else
            return check2(a);
        //throw new UnsupportedOperationException("Not yet implemented");

    }

    private String check2(String a) {
        String[] b=null;
        if(a.contains("*")) {
            b = a.split("\\*",2);
            a = mult(check3(b[0]), check2(b[1])) ;
            return a;
        }

        else
            return check3(a);
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    private String check3(String a) {
        String[] b=null;
        if(a.contains("/")) {
            b = a.split("\\/");
            a = divide(b[0], check3(b[1])) ;
            return a;
        }

        else
            return a;
        //throw new UnsupportedOperationException("Not yet implemented");
    }

}

--------------------------------------------------------------------------- --------------------------------------------------------------------------- ------------------------------------------------------

Reply any comments or suggestions.......

Thanks....

--
Pradyut
http://pradyut.tk
http://oop-edge.blogspot.com/
http://pradyutb.blogspot.com/
http://praddy-photos.blogspot.com/
http://oop-edge.spaces.live.com/
http://www.flickr.com/photos/praddy
http://groups.google.com/group/oop_programming
India


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "no output error in program" by inder arora
inder arora  
View profile  
 More options Dec 30 2007, 1:04 pm
From: inder arora <inder.c...@yahoo.com>
Date: Sun, 30 Dec 2007 10:04:52 -0800 (PST)
Local: Sun, Dec 30 2007 1:04 pm
Subject: no output error in program

bhai maine yeh program run kiya  to error dey raha hai....

---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
abhijit ghosal  
View profile  
 More options Dec 31 2007, 9:48 am
From: "abhijit ghosal" <abhijit.gho...@gmail.com>
Date: Mon, 31 Dec 2007 20:18:48 +0530
Local: Mon, Dec 31 2007 9:48 am
Subject: Re: no output error in program

what is the error man just tell me

On 12/30/07, inder arora <inder.c...@yahoo.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "java code to evaluate a arithmetic expression...." by Pradyut Bhattacharya
Pradyut Bhattacharya  
View profile  
 More options Jun 27 2008, 5:29 pm
From: "Pradyut Bhattacharya" <prady...@gmail.com>
Date: Sat, 28 Jun 2008 02:59:27 +0530
Local: Fri, Jun 27 2008 5:29 pm
Subject: Re: java code to evaluate a arithmetic expression....

The code better implemented: -
Please review and suggest a better implementation.
    Any comments are welcome.
    Download the code at: -
        http://myjavaserver.com/~pradyut/files/Calci.java
     The code: -
--------------------------------------------------------------------------- --------------------------------------------------------------------------- ---------------

public class Calci {

    public static void main(String[] args) {

        String a =
"4-(6-1)+3*9/2(8(6-1))(7-43)4-(6-1)+3.67*9/2(8(6.77+1))(7-4.3)";
//1*3+2*7-1-1
        //(2+1)-(4-2)3/(-5)
        //3*(1-2)
        //4-(6-1)+3*9/2(8(6-1))(7-43)
        //14-(4/(6-9))

        String[] b = null;
        String c = null;

        Calci obj = new Calci();

        System.out.println(obj.parse(a));

    }
    double func(String a) {
        double d = 0;

        return d;
    }
    String mult(String a, String b) {
        double c = func1(a) * func1(b);
        return (Double.toString(c));
    }
    String minus(String a, String b) {

        double c = func1(a) - func1(b);
        System.out.println(c);
        return (Double.toString(c));
    }
    String add(String a, String b) {
        double c = func1(a) + func1(b);
        return (Double.toString(c));
    }

    String divide(String a, String b) {
        double c = func1(a) / func1(b);
        return (Double.toString(c));
    }

    double func1(String a) {
        double d =0;
        try {
            d = Double.parseDouble(a);
        } catch (NumberFormatException n1) {
            System.out.println(n1);
        }
        return d;
    }

    String parse(String a) {
        String b = null;
        String c = null, d= null, e=null;
        int z=0, f=0;
        if(a.contains("(")) {

            //System.out.println(b[0]);

            z = a.lastIndexOf("(");
            f = a.indexOf(")", z)+1;
            c = a.substring(z, f);

            b =a.substring(0, z);
            d = a.substring(f, a.length());

            //processing b
            if(b.length() >=1) {
                e = String.valueOf(b.charAt(b.length()-1));
                if(e.equals("+") || e.equals("-") || e.equals("*") ||
e.equals("/") || e.equals("(")) {

                }else
                {
                    b = b+ "*";
                }
                //System.out.println("b is: " + b);
            }
            //processing d
            if(d.length() >1) {
                e = String.valueOf(d.charAt(0));
                if(e.equals("+") || e.equals("-") || e.equals("*") ||
e.equals("/") || e.equals(")")) {

                }else
                {
                    d = "*" + d;
                }
                //System.out.println("d is: "+d);
            }
                c = check(brackets(c));
                //System.out.println("c is: "+c);
                //c = "5";

            a = b + c + d;
                if(a.contains("*-")) {
                    //System.out.println("inside a is: "+a);
                    z = a.indexOf("*-");
                    b = a.substring(0, z);
                    //System.out.println("inside b is: "+b);
                    a = a.substring(z, a.length());
                    b = repl(b);

                    //System.out.println("inside a is: "+a);
                    a= a.replace("*-", "*");

                    a = b+a;
                } else if (a.contains("--")) {
                    a= a.replace("--", "+");
                } else if (a.contains("+-")) {
                    a= a.replace("+-", "-");
                } else if (a.contains("/-")) {
                    z = a.indexOf("/-");
                    b = a.substring(0, z);
                    //System.out.println("inside b is: "+b);
                    a = a.substring(z, a.length());
                    /*if( b.lastIndexOf('+') > b.lastIndexOf('-'))
                        b=b.replace(b.charAt(b.lastIndexOf('+')), '-');
                    else
                        b=b.replace(b.charAt(b.lastIndexOf('-')), '+');
                    */
                    b=repl(b);
                    //System.out.println("inside a is: "+a);
                    a= a.replace("/-", "/");

                    a = b+a;
                }
            System.out.println("a is: "+a);
                return parse(a);

            //System.out.println(a.substring(a.lastIndexOf("(")+1,
a.indexOf(")", z)));
        }
        else
            return check(a);
    }

    String repl(String b) {
        int j,k,l;
        String h;
        if(b.length() >1)
        {
                if(b.contains("+") || b.contains("-")) {
                    j = b.lastIndexOf('+');
                    k = b.lastIndexOf('-');
                    l = b.lastIndexOf(')');
                    if (l<j || l<k) {
                        if( b.lastIndexOf('+') > b.lastIndexOf('-'))
                        {
                            System.out.println("inside");

                            b=b.replace(b.charAt(b.lastIndexOf('+')), '-');
                            return b;
                        }
                        else
                        {
                            //System.out.println("test B");
                            System.out.println("inside 1");
                            b=b.replace(b.charAt(b.lastIndexOf('-')), '+');
                            return b;
                        }
                    } else {
                        System.out.println("inside 2");
                        h = b.substring(0, b.lastIndexOf('('));
                        System.out.println("h is: "+h);
                        b = b.substring(b.lastIndexOf('('), b.length());
                        System.out.println("b is: "+b);
                        if( h.lastIndexOf('+') > h.lastIndexOf('-'))
                        {
                            //System.out.println("test A");
                            h=h.replace(h.charAt(h.lastIndexOf('+')), '-');
                            b = h+b;
                            return b;
                        }
                        else
                        {
                            //System.out.println("test B");
                            h=h.replace(h.charAt(h.lastIndexOf('-')), '+');
                            b = h+ b;
                            return b;
                        }
                    }
                }
                else
                {
                    b = "-" + b;
                    return b;
                }
            } else
            {
                b = "-" + b;
                return b;
            }
    }

    String brackets(String b) {
        b = b.replace("(", "");
        b = b.replace(")", "");
        return b;
    }

    String check(String a) {
        String[] b=null;

        if(a.indexOf('+')==0) {
            a=a.substring(1);
            check(a);
        }
        if(a.contains("+")) {
            //System.out.println("This is inside minus");

            /*if(a.indexOf('+') ==0) {
                a=a.substring(1);
            }*/
            b = a.split("\\+",2);

            a=add(check1(b[0]),check(b[1])) ;
            return a;
        }
        else
            return check1(a);
    }

    private String check1(String a) {

        String[] b=null;

        if(a.contains("-")) {

            if(a.indexOf('-')==0)
            {
                a =a.substring(1);
                if(a.contains("-")) {
                    b = a.split("-", 2);
                    b[0] = "-" + b[0];
                    b[1] = "-" + b[1];
                    a = add(check2(b[0]), check1(b[1]));
                }
                else {
                    //System.out.println("this is here");
                    a = "-"+a;
                    a = check2(a);
                }
            } else {
                b = a.split("-", 2);
                //b[0] = "-" + b[0];

                b[1] = "-" + b[1];
                a = add(check2(b[0]), check1(b[1]));
            }
            return a;
        }else
            return check2(a);
        //throw new UnsupportedOperationException("Not yet implemented");

    }

    private String check2(String a) {
        String[] b=null;
        if(a.contains("*")) {
            b = a.split("\\*",2);
            a = mult(check3(b[0]), check2(b[1])) ;
            return a;
        }

        else
            return check3(a);
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    private String check3(String a) {
        String[] b=null;
        if(a.contains("/")) {
            b = a.split("\\/");
            a = divide(b[0], check3(b[1])) ;
            return a;
        }

        else
            return a;
        //throw new UnsupportedOperationException("Not yet implemented");
    }

}

--------------------------------------------------------------------------- --------------------------------------------------------------------------- ---------------

Thanks

On Sun, Dec 30, 2007 at 11:10 PM, Pradyut Bhattacharya <prady...@gmail.com>
wrote:

--
Pradyut
http://pradyut.tk
http://www.pradyut.co.nr/ ...

read more »


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google