Solved

Calculator

Posted on 1997-03-09
12
193 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:ThEGuy
12 Comments
 
LVL 2

Expert Comment

by:javiertb
ID: 1215252
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.

0
 

Author Comment

by:ThEGuy
ID: 1215253

0
 

Expert Comment

by:dpuryear
ID: 1215254
Are you just looking for a more detailed, better algorithm or are you looking for complete source code?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Expert Comment

by:MasterBo
ID: 1215255
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.

0
 

Author Comment

by:ThEGuy
ID: 1215256
dpuryear, actually i'm lookin for a complete soursecode if possible in graphic mode.
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 1215257
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.
0
 

Expert Comment

by:MasterBo
ID: 1215258
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?

0
 
LVL 1

Accepted Solution

by:
mrosen earned 20 total points
ID: 1215259
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.
0
 

Expert Comment

by:kilobug
ID: 1215260
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
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1215261
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.
0
 

Expert Comment

by:wpmaurice
ID: 1215262
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.
0
 

Expert Comment

by:renkens
ID: 1215263
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 !
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to delete from listbox using edit box 6 297
Get TStatusBar text from another application - Delphi 7 4 1,950
Client Data Set - Wide Memo 6 1,755
Delphi 2007 printer setup problem 8 771
This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question