• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Problem  RSS feed

 
Chapa Gurmukhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I have a strange problem
I am storing a formula say a = b + c as a string in a table.
Now I am getting the value of b and c as integer from external say email or from console
now i have to rerieve the formula from table and supply the value of band c and calculate the value of a. my problem is that when i retriev the formula its a string then how can i pass assign the value of b and c and do mathematical calculatuion to obtain value a. Please help me its quite urgent.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chapa,

It seems you want to "evaluate" an expression and you have variables in the expression. Is that what you want to do?

The Infix2Postfix link in my signature can help you. It would assume,
1. You have expression in space separated format meaning there is a space in between each valid tokens. e.g. a + b , c * d etc you know...

I would be glad to help you further if you decide to use Infix2Postfix API I have written. Also, there is an example with the source so that should help you ...

Also, can you let me know "why you have this requirement?". I am still collecting reasons that would help me justify my API for simple expression evaluations...

Regards
Maulin
[ December 08, 2004: Message edited by: Maulin Vasavada ]
 
Jimmy Die
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I think that evaluating the expression in some way (as just suggested)would be the way that I would prefer to consider, as the solution that I am about to propose is crude.




In principle you are using the strings as references to your case statements... Could work ok if you only have a handfull of formulas to solve.
[ December 08, 2004: Message edited by: Jimmy Die ]
 
Chapa Gurmukhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maulin,
I have downloaded API and will see if it fits in my requirement.
I tell you the scnario, what I need it
See my application gives the facility to the user to make their own formula of business and then calculate the value. Like I , as a user, enter and make a fomula Profit = Selling Price - Cost price. Now first I have to store the formula against the user in database. I can only store as a string. Now i eneter the value of the Selling Price and Cost Price regulary and my profit is calculated. My question is how can I calculate the formula as I rerieve the formula from table as String and then I have to pass the value of Selling Pricr and cost Price as double and have to make that "+" and "=" sign into arithmetic symbols of adition and equal. I hope u understand my query. please tell me whether your API can help me or not
 
Chapa Gurmukhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimmy thanks,
but my problem is that formula are not handful. Moreover fomulaw are constructed by user for their business and stored in table against their name
it can a = b+c-a or can a= b-s*r/d etc etc.
I have to fetch the formula from table and do the mathematical calculations

I hope u underdstand my problem
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
InFix to PostFix is a solution if you want to support operator precedence, eg in "a = b + c * d" the multiply should happen first. It's semi-deep Comp-Sci coursework.

If you don't support that you can just go left to right and maybe use parens to order things, so the user would have to write "a = b + ( c * d )". This is so much easier that I've done it in COBOL without recursion. The logic is something like:

I left out finding an open paren. A whole (expression) is just an operand. An open pretty much puts you back at expecting the first operand and a close means you have the value of the complicated operand.

"Get next token" can be tricky, too. I deliberately wrote my forumlae with spaces between all the tokens. Your user might write "a=b*c" with no spaces. Let's take that up as another topic if you find this approach interesting.

Does that help at all?
 
Chapa Gurmukhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James,
My problem is that formula will put in by user as per their business calculation and they can put space or can put braces
like a = z + (b+c)*d
and a = z + (b+c)d

In mathematics its same i have a problem how can i make codes generic to all the style of writing the formula. I am really confused. I think we have to write some business rules for this formula
What you say??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My simple (dumb) algorithm works as long as everyone knows there are no operator precedence rules and it's all left to right or inside parens first. I could share my non-recursive COBOL implementation if that would help. (ho ho ho)

Oh, your "no operator means multiply" example (b+c)d would take a little tweak. That's common in math, kinda hard to do on a calculator. Maybe you could talk them out of doing it.
[ December 10, 2004: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!