We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Calculator

on
Medium Priority
220 Views
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.
Comment
Watch Question

## View Solution Only

Commented:
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.

Commented:

Commented:
Are you just looking for a more detailed, better algorithm or are you looking for complete source code?

Commented:
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.

Commented:
dpuryear, actually i'm lookin for a complete soursecode if possible in graphic mode.

Commented:
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.

Commented:
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?

Commented:
You need to first parse the operation and identify tokens (easy when your number is like '3' but hard when you have 03.02). To do that in order, you need to define stuff using backus-nauer notation, which is a recursive definiction of your input. It's like creating a complier for numbers.

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
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

Commented:
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.

Commented:
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.

Commented:
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 !
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile

OR

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the