943,616 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3984
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 19th, 2008
-1

Simple Calculator

Expand Post »
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..
Last edited by mikedd; Aug 19th, 2008 at 1:29 pm. Reason: forget to mention about knoppix
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
mikedd is offline Offline
9 posts
since Aug 2008
Aug 19th, 2008
0

Re: Simple Calculator

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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 19th, 2008
0

Re: Simple Calculator

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?
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Aug 20th, 2008
0

Re: Simple Calculator

i do not know how to change the result into character..Can teach me?
Reputation Points: 9
Solved Threads: 0
Newbie Poster
mikedd is offline Offline
9 posts
since Aug 2008
Aug 20th, 2008
1

Re: Simple Calculator

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 20th, 2008
0

Re: Simple Calculator

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
Reputation Points: 12
Solved Threads: 3
Newbie Poster
mike_g is offline Offline
14 posts
since Jul 2008
Aug 20th, 2008
0

Re: Simple Calculator

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int invalid_operator = 0;
  6. char operator1;
  7. float number1, number2, result;
  8.  
  9. printf( "Enter two numbers and an operator in the format\n" );
  10. printf( "number1 operator number2\n" );
  11. scanf( "%f %c %f", &number1, &operator1, &number2 );
  12.  
  13. switch ( operator1 ) {
  14. case '*' :
  15. result = number1 * number2;
  16. break;
  17. case '/' :
  18. result = number1 / number2;
  19. break;
  20. case '+' :
  21. result = number1 + number2;
  22. break;
  23. case '-' :
  24. result = number1 - number2;
  25. break;
  26. default :
  27. invalid_operator = 1;
  28. }
  29.  
  30. switch ( invalid_operator ) {
  31. case 1 :
  32. printf( "Invalid operator.\n" );
  33. break;
  34. default :
  35. printf( "%f %c %f is %f\n",
  36. number1, operator1, number2, result );
  37. }
  38.  
  39. return 0;
  40. }
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?
Last edited by Ancient Dragon; Aug 20th, 2008 at 9:18 am. Reason: add code tags
Reputation Points: 9
Solved Threads: 0
Newbie Poster
mikedd is offline Offline
9 posts
since Aug 2008
Aug 20th, 2008
0

Re: Simple Calculator

Quote originally posted by me ...
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.
Last edited by mike_g; Aug 20th, 2008 at 9:44 am.
Reputation Points: 12
Solved Threads: 3
Newbie Poster
mike_g is offline Offline
14 posts
since Jul 2008
Aug 21st, 2008
0

Re: Simple Calculator

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?
Reputation Points: 9
Solved Threads: 0
Newbie Poster
mikedd is offline Offline
9 posts
since Aug 2008
Aug 21st, 2008
0

Re: Simple Calculator

The following example-code will 'convert' an int into a char
  1. int number = 97; //97 is the ascii value of 'a'
  2. char character = (char)number; // "convert" it
  3. printf("%c", character);

A char and an int are both numbers, they just differ in the way us humans read them

ascii-table
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: please help me with pointer to structures
Next Thread in C Forum Timeline: greatest of three nos





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC