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..

jephthah commented: Ich furze in Deinen allgemeinen Richtung. -1

## All 21 Replies

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?

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

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.

commented: you're really just a cuddly carebear, aren't you ;-) +4
``````#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?

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.

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?

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 :)

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

``````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.

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 :/

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

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.

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

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.

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

...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.

> .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.

``````#include <stdio.h>

int main()
{
char *a[]={"","point one","point two","point three","point four","point five","point six","point seven","point eight","point nine"};
char *b[]={"","one","two","three","four","five","six","seven","eight","nine"};
char *c[]={"","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
char *d[]={"","one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred"};
char *e[]={"","one thousand","two thousand","three thousand","four thousand","five thousand","six thousand","seven thousand","eight thousand","nine thousand"};
char *f[]={"","ten thousand","twenty thousand","thirty thousand","forty thousand","fifty thousand","sixty thousand","seventy thousand","ninety thousand"};
char *g[]={"","one hundred thousand","two hundred thousand","three hundred thousand","four hundred thousand","five hundred thousand","six hundred thousand","seven hundred thousand","eight hundred thousand","nine hundred thousand"};
char *h[]={"","one million","two million","three million","four million","five million","six million","seven million","eight million","nine million"};

int n;

n=102;
{

if(n==11)
printf("eleven");
else if(n==12)
printf("twelve");
else if(n==13)
printf("thirteen");
else if(n==14)
printf("fourteen");
else if(n==15)
printf("fifteen");
else if(n==16)
printf("sixteen");
else if(n==17)
printf("seventeen");
else if(n==18)
printf("eighteen");
else if(n==19)
printf("nineteen");

else
printf("%s%s%s%s%s%s%s%s",h[n/1000000],g[n/100000],f[n/10000],e[n/1000],d[n/100],c[n/10],b[n%10]);
}
return 0;

}``````

p/s:i have came our with this program.But i still could not overcome the decimal point problem.and tis program also cannot gif me the text such as five hundred two and so on..how can i solve this problem?

Well something like 102, you would need say

n = 102;

Then say
x = n / 100;
// now look up x in your "hundreds" array

y = n % 100;
Then look up y
If y < 20, then use all the special cases you have, otherwise use
p = y / 10
q = y % 10
and look those up in the respective 'tens' and 'units' arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.