Link to home
Start Free TrialLog in
Avatar of winwiz
winwiz

asked on

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
Avatar of snifong
snifong
Flag of United States of America image

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."
Oops. Forgot I was in the C section.
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
Avatar of ufolk123
ufolk123

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
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."

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
Avatar of winwiz

ASKER

Adjusted points to 200
Avatar of winwiz

ASKER

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!
What is the largest number that you will allow to be an operand?
Avatar of ozo
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"?
Avatar of winwiz

ASKER

just code a basic prog. that will manage numbers up to twenty.  Thx.
Withdrawn my answer

hongjun
ASKER CERTIFIED SOLUTION
Avatar of ufolk123
ufolk123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial