954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Calculator

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

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


p/s:pls can someone help me solve this problem and i am currently using knoppix software to solve this problem..pls help me..thx..

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 

You also forgot to read these as well
http://www.daniweb.com/forums/announcement118-2.html
http://www.daniweb.com/forums/thread78060.html

Now, which bit are you really stuck on?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

sure, sure. i've got nothing better to do than do your homework.

shall i also write your report, bind it, and mail it first class to your instructor?

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

i do not know how to change the result into character..Can teach me?

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 

Consider this
23 = Twenty three
123 = One hundred and twenty three
123,000 = One hundred and twenty three thousand

So you need to think about (eventually)
- splitting into groups of 1000
- splitting hundreds from tens and units

But start simple, try to code up converting 1 to 99.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

If I was doing this I think I would use sprintf to convert the numeric result to a string then parse the characters building the words from it:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html

mike_g
Newbie Poster
14 posts since Jul 2008
Reputation Points: 12
Solved Threads: 3
 
#include <stdio.h>

int main()
{
   int   invalid_operator = 0;
   char  operator1;
   float number1, number2, result;

   printf( "Enter two numbers and an operator in the format\n" );
   printf( "number1 operator number2\n" );
   scanf( "%f %c %f", &number1, &operator1, &number2 );

   switch ( operator1 ) {
      case '*' :
         result = number1 * number2;
         break;
      case '/' : 
         result = number1 / number2; 
         break;
      case '+' :
         result = number1 + number2; 
         break;
      case '-' :
         result = number1 - number2; 
         break;
      default :
         invalid_operator = 1;
   }
      
   switch ( invalid_operator ) {
      case 1 :
         printf( "Invalid operator.\n" ); 
         break;
      default :
         printf( "%f %c %f is %f\n",
            number1, operator1, number2, result );
   }
       
   return 0;
}

p/s:said i hav done this programe..But i do not know how to make the calculated output into character.Can someone pls show me how to make it?

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 
If I was doing this I think I would use sprintf to convert the numeric result to a string then parse the characters building the words from it: http://www.cplusplus.com/reference/c...o/sprintf.html


Have you tried converting the result to a string yet? that should make a good first step.

mike_g
Newbie Poster
14 posts since Jul 2008
Reputation Points: 12
Solved Threads: 3
 

but the problem now is i do not even know how to convert a single integer into a character.can u pls at least show me how to convert an integer into a character?

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 

The following example-code will 'convert' an int into a char

int number = 97; //97 is the ascii value of 'a' 
char character = (char)number; // "convert" it
printf("%c", character);


A char and an int are both numbers, they just differ in the way us humans read them :) ascii-table

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

but the output above will printout an 'a' output.How do i make it to printout the word ninety seven?

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 
char *ones[ ] = { "zero", "one", "two", "three" };
char *tens[ ] = { "none", "ten", "twenty" };
int n = 23;
printf( "%s %s", ones[n%10], tens[n/10] );

It's too big a clue to explain, you'll just have to figure it out.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Since the result is a float I still reckon the easiest way to go about it would be to use sprintf and parse the string, but the op dosent seem to be interested in that idea :/

mike_g
Newbie Poster
14 posts since Jul 2008
Reputation Points: 12
Solved Threads: 3
 

but the output above is an 'a'.how do i program it to make the output ninety seven?pls ignore this msg..i post wrongly oredi

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 

So that's all you've got to show for another day's help, and maybe you thinking about the problem some more (or maybe - shock horror - actually trying something for yourself).

Just post the same question again.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

yay finally i did it but i still facing problem on the decimal number.Gt any good idea to solve it?

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 

Yes, but first you are going to have to learn how to read. You could practice with this . Then learn how to converse with other people coherently. Hopefully the reading material should help you with that too.

mike_g
Newbie Poster
14 posts since Jul 2008
Reputation Points: 12
Solved Threads: 3
 

...Cmon la tel me how to make the decimal putput tat read the output point zero one?

mikedd
Newbie Poster
9 posts since Aug 2008
Reputation Points: 9
Solved Threads: 0
 
...Cmon la tel me how to make the decimal putput tat read the output point zero one?


I knew I should have keep my copy of the Rosetta Stone.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

> .Gt any good idea to solve it?
Yeah, exactly the same way that you solved the part before the decimal point.
It's all the same technique.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You