GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Aug-2008, 06:07
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

Write a calculator program that lets users enter a simple mathematic operation


For your all informations ..... I'm a newbie in C programming ....

Can somebody help me to solve this problem ??


Write a calculator program that lets users enter a simple mathematic operation and display the results as in the sample output shown below (bold text indicates user input).

Calculate: 92 + 1
Result = Ninety Three

Calculate: 92, 000 + 1
Result = Ninety Two Thousand

Calculate: 92, 000.01 + 1
Result = Ninety Two Thousand and One Point Zero One

Calculate: 92, 000.01 * 1 + 1 / 1
Result = Ninety Two Thousand and One Point Zero One

our program MUST at least be able to interpret the following operators:
• +
• -
• /
• *
• %
• >
• <
• >=
• <=

The results should be in string and the 92,000 got comma one , a bit hard for me to solve it .....

Can solve it and discuss it here ?
  #2  
Old 21-Aug-2008, 07:58
bobsam bobsam is offline
New Member
 
Join Date: May 2008
Posts: 7
bobsam is on a distinguished road

Re: Can help me to solve this problem ??


the calculator part is quite easy, but you may have to input data on separate lines like:
92
+
1
you would have to use scanf() to recognise the numbers
and use a switch function for the sum like:
CPP / C++ / C Code:
double num1, num2, ans;
char sum;
scanf("%f %c %f", &num1, &sum, &num2);
switch(sum){
case '+':
ans=num1+num2;
break;
case '-':
ans=num1-num2;
break;
}
printf("=%f\n", ans);
but the printing out as a string is more difficult
you could use a switch statement with the numbers 0-9 and their names to be used for each digit, but the output would be more like Zero Nine Three Point Zero Zero

To get a better out put you would have to make several switch statments, one for each digit.
Last edited by admin : 21-Aug-2008 at 15:27. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #3  
Old 21-Aug-2008, 08:28
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

Re: Can help me to solve this problem ??


So anybody can help me ??? I'm really dont know how to do the string part !!!! If use switch , is it we need 1000 cases ??
  #4  
Old 21-Aug-2008, 08:53
bobsam bobsam is offline
New Member
 
Join Date: May 2008
Posts: 7
bobsam is on a distinguished road

Re: Can help me to solve this problem ??


Quote:
Originally Posted by kcp88
So anybody can help me ??? I'm really dont know how to do the string part !!!! If use switch , is it we need 1000 cases ??

no you will need to do it like this:
CPP / C++ / C Code:
int n;
//for the first digit(eg if limit is 99999.99) would be 90000
n=(int)(ans%10000)
switch(n){
case 2:
printf("Two "); //This could be 'Twenty' to be followed by the next digit, but there break;//would be difficulty using this method for teens, that is what I ment;
case 3:
printf("Three ");
break;//ect
}
If you did it the easy way you would have to do a while loop changing which digit you used each time

If you did it the hard way you would have to make 7 swich statements, one for each digit, plus a system for sorting out teens
Last edited by admin : 21-Aug-2008 at 15:27. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 21-Aug-2008, 08:58
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Can help me to solve this problem ??


Quote:
Originally Posted by kcp88
So anybody can help me ??? I'm really dont know how to do the string part !!!! If use switch , is it we need 1000 cases ??
Be patient. This is not a live board manned 24/7. The norm is that you will likely receive an answer within a day or so.

Now to answer your question, try to establish patterns. ie.
  • 9 == nine.
  • 90 == ninety.
  • 900 == nine hundred.
  • 9,000 == nine thousand.
  • 90,000 == ninety thousand.
  • etc.
Also consider:
  • 99 == ninety-nine.
  • 990 == nine hundred ninety.
  • 9,900 == nine thousand nine hundred.
  • etc.
So, to decompose 999,999,
  • Note the position of the comma.
  • Establish the longest sequence first: nine hundred thousand.
  • Subtract what has already been determined & decompose the remainder: ninety-nine thousand.
  • Because of the comma, consolidate the answer thus far: nine hundred ninety-nine thousand.
  • Subtract & apply the same algorithm. Establish the longest pattern: nine hundred.
  • Subtract, & find the next pattern: ninety.
  • Subtract, & find the next pattern: nine.
  • Subtract, & since the result is zero, build up what strings have been determined: nine hundred ninety-nine thousand nine hundred ninety-nine.
You now have enough enough of an algorithm which will allow you to extend it to millions, billions, etc.
  #6  
Old 21-Aug-2008, 09:12
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 696
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Can help me to solve this problem ??


You do not need 1000 cases. You should probably be able to convert it to a string first, then analyze each character. You will have to calculate the offset from the decimal character and decide whether it represents hundreds, tens, or ones. If the number is a 3 and it's in the tens spot then you know it is "thirty". After each 3 numbers, you'll need to decide whether the offset represents thousands, millions, billions, trillions, etc. (Can we assume a limit of 999.999999999999 trillion?) then append the proper word. So, for example, lets say the number is 1234567.89.

Code:
Analyze the 1. Offset represents ones. Append "one " to the string. String = "one ". Does the offset require thousands/millions/etc? Yes, append "million " to the string. String = "one million ". Analyze the 2. Offset represents hundreds. Append "two hundred" to the string. String = "one million two hundred". Does the offset require thousands/millions/etc? No. Analyze the 3. .... ....
  #7  
Old 21-Aug-2008, 10:24
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

Re: Can help me to solve this problem ??


Calculate: 92, 000.01 * 1 + 1 / 1

If i do exactly what bobsam wrote , i cant have 3 variables for the calculation part ..... Is it cannot use switch ? or ?

And , thanks for your all ......You all are really nice ..... Really , thanks a lots !
  #8  
Old 21-Aug-2008, 12:04
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Can help me to solve this problem ??


Quote:
Originally Posted by kcp88
If i do exactly what bobsam wrote , i cant have 3 variables for the calculation part ..... Is it cannot use switch ? or ?
The information provided to you thus far is only meant to get you started. bobsam's example was to illustrate how you might approach the problem is there were only two operands.
Quote:
Calculate: 92, 000.01 * 1 + 1 / 1
Result = Ninety Two Thousand and One Point Zero One
You need to clarify with your instructor how you are to deal with operator precedence. Typical arithmetic rules place multiplication & division higher than addition & subtraction.

For example, given the expression 2 + 3 * 4, the proper answer is 14. If operator precedence is relaxed, the answer may be 20.

You should also clarify whether parentheses are potential characters seen in the input stream.
Quote:
Calculate: 92, 000 + 1
Result = Ninety Two Thousand
I can only guess that this was mistyped. The actual answer is "ninety-two thousand one".
Quote:
Calculate: 92, 000.01 + 1
Result = Ninety Two Thousand and One Point Zero One

Calculate: 92, 000.01 * 1 + 1 / 1
Result = Ninety Two Thousand and One Point Zero One
Although most people will use the word "and" when speaking the answer in English, the word "and" is not considered proper, & inserting this into your algorithm will unnecessarily make the work you need to complete harder. You should clarify with your instructor whether you are expected to insert the word "and" as well.
  #9  
Old 21-Aug-2008, 12:37
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

Re: Can help me to solve this problem ??


Oh ... then the correct way is still usong switch to do it ? Thanks for teaching me . And also thanks to Bobsam
  #10  
Old 21-Aug-2008, 12:48
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Can help me to solve this problem ??


Quote:
Originally Posted by kcp88
Oh ... then the correct way is still usong switch to do it
A switch statement might be in order, but is only part of the solution. Before you invest too much more time, you need to resolve the questions on operator precedence. You need to talk to your instructor.
 
 

Recent GIDBlogReview: Gel laptop cooling pad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An easy problem solve for most of you slapshot6819 C++ Forum 2 20-Nov-2007 03:14
Please help me to solve C++ problem fahad C++ Forum 1 01-Nov-2007 05:43
Problem Description: Graph’s Degree.....can help me to solve this out? thefinalyy C++ Forum 0 24-Aug-2007 06:42
I have a problem to solve summation in c++ logieen C++ Forum 4 22-May-2007 08:25
A problem using streams. (I've spent a month trying to solve it) Mararia C++ Forum 8 14-Dec-2006 17:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.