Link to home
Start Free TrialLog in
Avatar of ThEGuy
ThEGuy

asked on

Calculator

I'm desperately in need of a program which does simple mathmetical calclations. But, the program has to be able to recognize operation priorities.Eg. : If we have 3+4*5-6/7, it will have to multiply 4 & 5, and divide 6 by 7 first. Then the plus and minus.
Avatar of javiertb
javiertb

You could just do it yourself by parsin the string in this way:
1st- Move through the string storing the individual number just before the sign.
2nd- If the sign was '*' or '/' then read the following number, do the calculation and substitute the two nunbers and the sign with the result in the string (always keeping track of the position with appropiate counters).
3rd-Move to the beginning of the string and do the 2nd step but this time looking for '+' or '-'

Hope this helps.

Avatar of ThEGuy

ASKER


Are you just looking for a more detailed, better algorithm or are you looking for complete source code?
I would again recommend getting Pascal snippets, or SWAG
colelction, from garbo.uwasa.fi, in /pc directory.
Simple parsing procedures, one of which you need, has been
implemented several times in SWAG. Please visit Garbo site.

Avatar of ThEGuy

ASKER

dpuryear, actually i'm lookin for a complete soursecode if possible in graphic mode.
25 points for a complete string-evaluator source is not much.

However, some years ago I wrote such an algorithm.
I used exactly the algorithm raviert gave you. It wasn't more than 200 lines of code.
If you need code written ab initio, yes, 25 points is too
little. However, did you ever browsed SWAG collection?

It's not library, it's source code collection.

By the way, graphics, in my opinion, is not related to
the problem. How does graphics and string parsing interfere?

ASKER CERTIFIED SOLUTION
Avatar of mrosen
mrosen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've made a full unit wich recognize very complex strings, with variables, mathematic functions, logical operators, ....

If some of you wants it, only ask it by mail....

Bye
I have an old unit somewhere which could do mathematical parsing.
As far as I remember it could also do functions such as sin and cos and so on. I used it to write a shareware program many years ago (graph3d). Anyway, if you want it, just tell me.
Assuming that the function's level of difficulty is that in your question. You can input the calculation as a string, then use a double loop to scan for the operator (+,-). Next, perform the calculations by means of array manipulations. A string is actually an array of characters.
The first proposed answer is of course the right one.
In constructing a (simple) DBMS I build a expression evaluator
myself (not for sale for 25 points). But some tips :
By or rent or get the book 'Algoritmes + Datastructures = Programs' from Niklaus Wirth. It contains great material on
constructing a parser for a given syntax diagram.
Then make the syntax diagram for youre expressions and code
the procedures using the rules from the book.
At last add the needed arithmic to you're parser procedures and
the job is doned !