[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# Calculator -- with a twist

Posted on 2000-02-29
Medium Priority
271 Views
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
Question by:winwiz
[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
• 4
• 3
• 3
• +2

LVL 1

Expert Comment

ID: 2571501
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

ID: 2571503
Oops. Forgot I was in the C section.
0

LVL 33

Expert Comment

ID: 2571641
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

LVL 3

Expert Comment

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

0

LVL 1

Expert Comment

ID: 2572393
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

ID: 2572894
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

LVL 2

Author Comment

ID: 2573291
0

LVL 2

Author Comment

ID: 2573292
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

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

LVL 84

Expert Comment

ID: 2573684
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

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

LVL 33

Expert Comment

ID: 2575304

hongjun
0

LVL 3

Accepted Solution

ufolk123 earned 600 total points
ID: 2575482
Have you checked this site.
It gives you the source code for the calc in c.

0

## Featured Post

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month14 days, 20 hours left to enroll