Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math utils 1

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi there.
I have a string like this: -500+1000*10. The answer should be 9500 according to mathematical rules.

I've dome a simple math routine using "top to bottom" routine. For example:
Step 1: -500+1000 = 500
Step 2: 500*10 = 5000

How the heck can I implement the math rules?
Is there a function somewhere I've missed, or?




KungTure-RX.jpg

//Nordlund
 
I must be missing your point but

showmessage(inttostr(-500+(1000*10))); displays 9500



Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
I see what you are saying (after consulting the help file) multiply should have the highest pesidence the + then -
so it should do
1000 * 10 = 10000
10000 + -500 = 9500

But your 2 step proccess does it left to right.

I tried my previous post without the brackets and it still gives 9500 (as it should)
showmessage(inttostr(-500+1000*10));



Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
Yep...
I was a litle bit unclear when I wrote the problem.

My customer want to create their own forulas, And i have to solve them in a correct way and store the end value in the database. Like a Math parser...

Found one at: The RPN download

KungTure-RX.jpg

//Nordlund
 
As Steve said, Delphi will come up with 9500 for the example you gave. Delphi follows the usual rules for operator precedence (see 'Operator precedence rules' in the help file).
This surely means it is up to your customer to define their formulas correctly...

Steve
 
Ok, I think you don't understand my problem yet.

Like this example, then:
I want delphi to take charge of the Operator precedence rules.


Code:
procedure TForm1.Button1Click(sender: TObject);
var
  d: double;
begin
  d := CountTheString(Edit1.Text); 
  ShowMessage(FloatToStr(d));
end;



KungTure-RX.jpg

//Nordlund
 
Then, the customer enter (-10+30)*50/(2+10), and the function CountTheString should return something like: 83,33333333


KungTure-RX.jpg

//Nordlund
 
Ok then, but the download solved my probs... Thanks for discuss it with me...

KungTure-RX.jpg

//Nordlund
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top