?
Solved

Calculator -- with a twist

Posted on 2000-02-29
13
Medium Priority
?
272 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
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

by:snifong
ID: 2571503
Oops. Forgot I was in the C section.
0
 
LVL 33

Expert Comment

by:hongjun
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"
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:ufolk123
ID: 2571896
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
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

by:hongjun
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

by:winwiz
ID: 2573291
Adjusted points to 200
0
 
LVL 2

Author Comment

by:winwiz
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

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

Expert Comment

by:ozo
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

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

Expert Comment

by:hongjun
ID: 2575304
Withdrawn my answer

hongjun
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 600 total points
ID: 2575482
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

839 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