Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > evaluate a arithmetic expression

Reply
Thread Tools

evaluate a arithmetic expression

 
 
Pradyut
Guest
Posts: n/a
 
      06-27-2008
Hi,
I have written a code in evaluating a arithmetic expression.
Please review and suggest a better implementation.
Any comments are welcome.
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
Pradyut
http://www.pradyut.co.nr/
India
 
Reply With Quote
 
 
 
 
Pradyut
Guest
Posts: n/a
 
      06-27-2008
On Jun 28, 2:07 am, Pradyut <(E-Mail Removed)> wrote:
> Hi,
> I have written a code in evaluating a arithmetic expression.
> Please review and suggest a better implementation.
> Any comments are welcome.
> The code: -
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
>

<snip>
>
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Thanks
> Pradyuthttp://www.pradyut.co.nr/
> India


Download the code at: -
http://myjavaserver.com/~pradyut/files/Calci.java

Thanks
Pradyut
http://www.pradyut.co.nr/
India
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      06-27-2008
Pradyut <(E-Mail Removed)> writes:
>Please review and suggest a better implementation.


The requirement specification is missing, so there is no basis
for quality metrics to judge what is a better implementation.
(An implementation of which specification and requirement document?)

Without this specification, one at least can criticize
that documentation comments are missing.

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-27-2008
Pradyut wrote:
> On Jun 28, 2:07 am, Pradyut <(E-Mail Removed)> wrote:
>> I have written a code in evaluating a arithmetic expression.
>> Please review and suggest a better implementation.

> Download the code at: -
> http://myjavaserver.com/~pradyut/files/Calci.java


No comments in the code.

Very difficult to maintain due to extensive use of if, indexOf,
substring etc..

Very difficult to extend because the operators and their
characteristics are hardcoded all over the code.

If you are learning Java then it is OK.

But it is not anywhere near how it should be done.

Arne
 
Reply With Quote
 
Pradyut
Guest
Posts: n/a
 
      06-27-2008
On Jun 28, 4:06 am, Arne Vajh°j <(E-Mail Removed)> wrote:
> Pradyut wrote:
> > On Jun 28, 2:07 am, Pradyut <(E-Mail Removed)> wrote:
> >> I have written a code in evaluating a arithmetic expression.
> >> Please review and suggest a better implementation.

> > Download the code at: -
> >http://myjavaserver.com/~pradyut/files/Calci.java

>
> No comments in the code.
>
> Very difficult to maintain due to extensive use of if, indexOf,
> substring etc..
>
> Very difficult to extend because the operators and their
> characteristics are hardcoded all over the code.
>
> If you are learning Java then it is OK.
>
> But it is not anywhere near how it should be done.
>
> Arne


thanks for comment Arne .... please a better flowchart could be more
useful...thanks

Can someone please post a dfd for this program (the expression in
string format).
brackets remained a major problem for all the string manipulation in
the code....
any way to get rid of the string manipulation used in the code
especially in "parse and repl" functions.

Thanks
Pradyut
http://pradyut.co.nr
India
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-28-2008
Pradyut wrote:

>
> thanks for comment Arne .... please a better flowchart could be more
> useful...thanks


Unless I misunderstand you, that's what Arne said. A flowchart is
required to understand your mess, and even then it's not likely to help
much really. Your code is far too complicated. We can't read it.


>
> Can someone please post a dfd for this program (the expression in
> string format).


dfd?

> brackets remained a major problem for all the string manipulation in
> the code....
> any way to get rid of the string manipulation used in the code
> especially in "parse and repl" functions.


I don't understand "repl" here. Why replace anything?

I'll give you a break because you made the same mistake I did when my
instructor assigned us to write a program to parse algebraic expressions
in my assembly language class. I assumed that I had to do the work myself.

This is a mistake. There are many parsing algorithms available in the
literature. Go read up on how to turn algebraic expressions in infix
into Reverse Polish on a stack.

Here's a link to get you started. Notice how much shorter their example
is. This is actually a pretty easy problem:

<http://en.wikipedia.org/wiki/Operator-precedence_parser>
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-28-2008
Pradyut wrote:
> On Jun 28, 4:06 am, Arne Vajh°j <(E-Mail Removed)> wrote:
>> Pradyut wrote:
>>> On Jun 28, 2:07 am, Pradyut <(E-Mail Removed)> wrote:
>>>> I have written a code in evaluating a arithmetic expression.
>>>> Please review and suggest a better implementation.
>>> Download the code at: -
>>> http://myjavaserver.com/~pradyut/files/Calci.java

>> No comments in the code.
>>
>> Very difficult to maintain due to extensive use of if, indexOf,
>> substring etc..
>>
>> Very difficult to extend because the operators and their
>> characteristics are hardcoded all over the code.
>>
>> If you are learning Java then it is OK.
>>
>> But it is not anywhere near how it should be done.

>
> thanks for comment Arne .... please a better flowchart could be more
> useful...thanks
>
> Can someone please post a dfd for this program (the expression in
> string format).
> brackets remained a major problem for all the string manipulation in
> the code....
> any way to get rid of the string manipulation used in the code
> especially in "parse and repl" functions.


The traditional way is:

1) convert from string to list of tokens
2) convert list of tokens from infix to postfix
3) evaluate

where you have a list of operators and their precedence.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to evaluate simple arithmetic expression mstorkamp@yahoo.com C Programming 6 11-20-2007 10:12 PM
Can you evaluate a databinding expression? sydney.luu@gmail.com ASP .Net 1 03-28-2006 05:41 PM
How to evaluate an expression EDom ASP .Net 2 08-12-2005 07:41 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
How to explain "evaluate the expression as a void expression"? Jason luo C Programming 3 08-19-2004 12:43 AM



Advertisments