• Status: Solved
• Priority: Medium
• Security: Public
• Views: 274

# Calculator -- with a twist

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
winwiz
• 4
• 3
• 3
• +2
1 Solution

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

Commented:
Oops. Forgot I was in the C section.
0

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

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

0

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

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

Author Commented:
0

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

Commented:
What is the largest number that you will allow to be an operand?
0

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

Author Commented:
just code a basic prog. that will manage numbers up to twenty.  Thx.
0

Commented:

hongjun
0

Commented:
Have you checked this site.
It gives you the source code for the calc in c.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.