Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Developer
tej
Posts: 31
Registered: ‎12-26-2013
My Device: 8760
My Carrier: airtel

Calculator in Blackberry

Hi i am working on calculator but i am not getting correct output. For example with

 

4+(1/2)+8

 

The desired output is 12.5 but my code returns 12.0

i

have used this code but this will no giving the rounded value

 

 

public String evaluatePostfix(String postfix){

LongStack S = new LongStack();
float resout;
//answer for val1 and val2
Dialog.alert("postfix: "+postfix + "length "+ postfix.length());

for(int k = 0; k < postfix.length(); k++)
{
char c =postfix.charAt(k);
if( c >= '0'&& c <= '9')//i < tokens.length && (Character.isDigit(tokens[i]) || tokens[i] == '.')
S.push((c - '0'));

else if (c == '+' || c== '-' || c == '*' || c == '/' || c == '%' || c == '~')
{
if(S.isEmpty()) throw new RuntimeException("Empty Stack");
float RightOp = S.pop();
Dialog.alert(": "+ RightOp);
if( c == '~') S.push( (long) -RightOp);
if(S.isEmpty()) throw new RuntimeException("Empty Stack.");
switch (c)
{
case '+' : S.push((long) (S.pop() + RightOp)); break;
case '-' : S.push((long) (S.pop() - RightOp)); break;
case '*' : S.push((long) (S.pop() * RightOp)); break;
case '/' : S.push((long) (S.pop()/ RightOp)); break;
case '%' : S.push((long) (S.pop() % RightOp)); break;

}// END of switch
}

else if ( c != ' ')throw new RuntimeException("Error!");
}
Dialog.alert(Long.toString(S.pop()));

Float fX = new Float(S.pop());
Formatter f = new Formatter();
String result = f.formatNumber(fX.floatValue(), 2);
return result;

}

 

 

Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Calculator in Blackberry

did you copy that code from somewhere?
doing a conversion from character to long by subtracting '0' looks questionable to me. The code also does not handle brackets.

you should add debugging in your code and check where exactly it goes wrong.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Calculator in Blackberry

Agree with Simon.

 

The biggest issue for me is that all the evaluations seem to be done using long, which is of course integer arithmetic.  So you need to rework your processing to use floats or doubles for all the evaluations, not just the result. 

Please use plain text.
Developer
Developer
tej
Posts: 31
Registered: ‎12-26-2013
My Device: 8760
My Carrier: airtel

Re: Calculator in Blackberry

@simon and peter i serched for float api but i am not getting 

 

here it is showing only int and long

 

import net.rim.device.api.util.IntStack;
import net.rim.device.api.util.LongStack;

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Calculator in Blackberry

So you will need to implement your own....

Please use plain text.