Solved

Calculator

Posted on 1997-03-09
12
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TMemoryStream -> PNGImage -> TIcon convertion 22 1,270
Converting a Windows Bitmap Stream to TIcon 2 807
Secure Boot on W8 & 8.1 8 47
ADO Memory leak with DELPHI 2007 37 208
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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