Solved

Calculator -- with a twist

Posted on 2000-02-29
13
263 Views
Last Modified: 2010-04-15
I am trying to make a calculator that will do basic math (i.e. 7+7, 2-1,9/3,6*4,etc.).  But it is a bit different from the normal calculator.

The user will input the problem in text only, like: six plus seven, four minus three, and nine divided by three.
The program needs to parse the user input, convert it to the actual equation and calculate the answer as output.

I am not sure where to begin.. Please program as much as possible and provide me the code.

Thx
Jason
0
Comment
Question by:winwiz
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 1

Expert Comment

by:snifong
Comment Utility
Well, I will not do the entire problem for you but I will give you a coulpe of ways to approach it.

1) The most straight forward wat to do it is to "map" the numeric integer to the english equiv. using an array or better yet the STL map class.

2) I just thought of this. You could define an enumerated type like:
enum NUMBER {zero, one, two, ...};
This way the english equiv. are really represented as ther integer values. Now that I think about it 1 is probably the better way to go than this one.

3) In my opinion, and I'm sure that many people are going to disagree with me, the best way is to create a class. Think of it this way. What you want is a way to "cast" the integer to english. It would look somethinkg like this:
int a = (int) one + (int) three;
cout << (english) a;

An important thing to think about would be: What is the max number that I am going to represent? If you are dealing with ints under 10 this is great but think of 1001 + 15678. You would have to have to be able to represent thousand and hundred. As you can see the larger the number the more words need to be in your "vocabulary."
0
 
LVL 1

Expert Comment

by:snifong
Comment Utility
Oops. Forgot I was in the C section.
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
You must do some validation of string. Example you must tell user that plus is accepted and pLus is not. Or you may want to make it case-insensitive. You must also provide functions like () so that the order of manipulation is determined. You may want to name it a bracket.

You may want to read the input into a string maybe using dynamic memory allocation to save up some memory space. Then read the string and then determine it is a number or an operator. If user enters an invalid string that is not defined, you may want to abort the computation and inform the user about it.

Hint:
if string is "1 plus 2"
1. Read the string.
2. Once you encounter a number that is you must check the ascii code, convert the '1' to an integer.

Is this an assignment question?

Good luck
hongjun
0
 
LVL 3

Expert Comment

by:ufolk123
Comment Utility
Why to re-invent thw wheel ?
As you require here is the source code for a C based calculator for your reference.

http://reality.sgi.com/chongo/tech/comp/calc/calc-download.html
0
 
LVL 1

Expert Comment

by:snifong
Comment Utility
hongjun,Unless I misunderstood he is saying that the it will not be read as a string like '1', it will be read as "one plus seven." The output would then be "eight."

0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
You can read the string as "one plus seven" from user. But when doing the computation, you can always user a loop and read one character by another. This can be done.

hongjun
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:winwiz
Comment Utility
Adjusted points to 200
0
 
LVL 2

Author Comment

by:winwiz
Comment Utility
thanks for all the responses guys! Let me clarify a few things.  The user will input the problem with text only like: seven plus nine, etc...  The answer will simply be given as a number so if the user inputs "six plus two"  the program will output 8.  not eight.  someone please include source code.  I have boosted the point value for the source code to be included!  Thanks guys!
0
 
LVL 1

Expert Comment

by:snifong
Comment Utility
What is the largest number that you will allow to be an operand?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
could the user also input numbers like "one thousand seven hundred eighty seven minus half a dozen" or "seventeen hundred divided by four score and seven"?
0
 
LVL 2

Author Comment

by:winwiz
Comment Utility
just code a basic prog. that will manage numbers up to twenty.  Thx.
0
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
Withdrawn my answer

hongjun
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 200 total points
Comment Utility
Have you checked this site.
It gives you the source code for the calc in c.


http://reality.sgi.com/chongo/tech/comp/calc/calc-download.html

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now