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
  #11  
Old 21-Aug-2008, 20:30
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 ??


Quote:
Originally Posted by bobsam
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


bobsam ...erm.... can i ask you some more ?
How to do it in while ?

And .... this is because instructor dont want to give us any hints ..... ask us do it by ourselves ...... that;s why I'm really no idea
  #12  
Old 22-Aug-2008, 16:37
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
And .... this is because instructor dont want to give us any hints ..... ask us do it by ourselves ......
Because your instructor has not told you otherwise, you will need to consider that all operators do not have the same precedence. As an example, while the first expression can be processed left-to-right:
Code:
2 * 3 + 4
The following cannot be simply solved by reading all values & operators left-to-right:
Code:
2 + 3 * 4
Common techniques for dealing with this includes using a stack or tree structure.
  #13  
Old 06-Sep-2008, 12:49
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

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


CPP / C++ / C Code:
#include    <stdio.h>	

int main()
{
	float input1,input2,result2;
	int operator;
	result2=input1=input2=operator=0;
	printf("insert first value : ");
	scanf("%f",&input1);
	
 do{
	 printf("insert operator: ");
	 scanf("%d",&operator);

	 if(operator!=0)
	{
	 printf("insert second value : ");
	 scanf("%f",&input2);	 
 
	 switch(operator){	 
	
	 case 1:
	 result2=input1+input2;
	 input1=result2;
	 break;

	 case 2:
	 result2=input1-input2;
 	 input1=result2;
	 break;

	 case 3:
	 result2=input1*input2;
	 input1=result2;
	 break;
	
	 case 4:
	 result2=input1/input2;
	 input1=result2;
	 break;	
	 }
	 }
	 }
	 while(operator!=0);
	
	 printf("%f\n",result2);
	
	
	
float i;
int counter,result,result1,zz,yy,d1,d2,d3,d4,d5,d6,d7,d8,d9;
counter=0;


result=i=result2;

printf("result is %d\n",result);

result1=result;

/*result=result%10;

printf("result is %d\n",result);*/

do{
zz=result/10;
printf("zz is %d\n",zz);
counter++;
result=zz;


}
while(zz!=0);

printf("counter is %d\n",counter);

printf("ori result is %d\n",result1);

switch(counter){
case 1:
d1=result1;
switch(d1){
case 0:printf("zero");break;
case 1:printf("one");break;
case 2:printf("two");break;
case 3:printf("three");break;
case 4:printf("four");break;
case 5:printf("five");break;
case 6:printf("six");break;
case 7:printf("seven");break;
case 8:printf("eight");break;
case 9:printf("nine");break;
}
break;

case 2:
d1=result1/10;
d1%=10;
switch(d1){
case 0:printf("zero ");break;
case 1:printf("one");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d2=result1%10;
switch(d2){

case 1:printf("one ");break;
case 2:printf("two ");break;
case 3:printf("three ");break;
case 4:printf("four");break;
case 5:printf("five ");break;
case 6:printf("six ");break;
case 7:printf("seven ");break;
case 8:printf("eight ");break;
case 9:printf("nine ");break;
}
break;



break;

case 3:{

d1=result1/100;
d1%=10;
switch(d1){
case 0:printf("zero");break;
case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d2=result1/10;
d2%=10;
switch(d2){

case 1:printf("one ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d3=result1%10;
switch(d3){
case 1:printf("one ");break;
case 2:printf("two ");break;
case 3:printf("three ");break;
case 4:printf("four");break;
case 5:printf("five ");break;
case 6:printf("six ");break;
case 7:printf("seven ");break;
case 8:printf("eight ");break;
case 9:printf("nine ");break;
}
break;

case 4:
d1=result1/1000;
d1%=10;
switch(d1){
case 0:printf("zero");break;
case 1:printf("one thousand ");break;
case 2:printf("two thousand ");break;
case 3:printf("three thousand ");break;
case 4:printf("four thousand ");break;
case 5:printf("five thousand ");break;
case 6:printf("six thousand ");break;
case 7:printf("seven thousand ");break;
case 8:printf("eight thousand ");break;
case 9:printf("nine thousand ");break;
}

d2=result1/100;
d2%=10;
switch(d2){

case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d3=result1/10;
d3%=10;
switch(d3){

case 1:printf("one ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d4=result1%10;
switch(d4){

case 1:printf("one");break;
case 2:printf("two");break;
case 3:printf("three");break;
case 4:printf("four");break;
case 5:printf("five");break;
case 6:printf("six");break;
case 7:printf("seven");break;
case 8:printf("eight");break;
case 9:printf("nine");break;
}
break;

case 5:

d1=result1/10000;
d1%=10;
switch(d1){
case 0:printf("zero");break;
case 1:printf("Ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d2=result1/1000;
d2%=10;
switch(d2){
case 0:printf("thousand ");break;
case 1:printf("one thousand");break;
case 2:printf("two thousand ");break;
case 3:printf("three thousand ");break;
case 4:printf("four thousand ");break;
case 5:printf("five thousand ");break;
case 6:printf("six thousand ");break;
case 7:printf("seven thousand ");break;
case 8:printf("eight thousand ");break;
case 9:printf("nine thousand ");break;
}

d3=result1/100;
d3%=10;
switch(d3){

case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d4=result1/10;
d4%=10;
switch(d4){
case 1:printf("one ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d5=result1%10;
switch(d5){

case 1:printf("one");break;
case 2:printf("two");break;
case 3:printf("three");break;
case 4:printf("four");break;
case 5:printf("five");break;
case 6:printf("six");break;
case 7:printf("seven");break;
case 8:printf("eight");break;
case 9:printf("nine");break;
}
break;


case 6:

d1=result1/100000;
d1%=10;
switch(d1){
case 0:printf("zero");break;
case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d2=result1/10000;
d2%=10;
switch(d2){
case 0:printf("zero");break;
case 1:printf("Ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d3=result1/1000;
d3%=10;
switch(d3){
case 0:printf("thousand ");break;
case 1:printf("one thousand");break;
case 2:printf("two thousand ");break;
case 3:printf("three thousand ");break;
case 4:printf("four thousand ");break;
case 5:printf("five thousand ");break;
case 6:printf("six thousand ");break;
case 7:printf("seven thousand ");break;
case 8:printf("eight thousand ");break;
case 9:printf("nine thousand ");break;
}

d4=result1/100;
d4%=10;
switch(d4){

case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d5=result1/10;
d5%=10;
switch(d5){

case 1:printf("one ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d6=result1%10;
switch(d6){

case 1:printf("one");break;
case 2:printf("two");break;
case 3:printf("three");break;
case 4:printf("four");break;
case 5:printf("five");break;
case 6:printf("six");break;
case 7:printf("seven");break;
case 8:printf("eight");break;
case 9:printf("nine");break;
}
break;

case 7:

d1=result1/1000000;
d1%=10;
switch(d1){
case 0:printf("zero");break;
case 1:printf("one million ");break;
case 2:printf("two million ");break;
case 3:printf("three million ");break;
case 4:printf("four million ");break;
case 5:printf("five million ");break;
case 6:printf("six million ");break;
case 7:printf("seven million ");break;
case 8:printf("eight million ");break;
case 9:printf("nine million ");break;
}

d2=result1/100000;
d2%=10;
switch(d2){
case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d3=result1/10000;
d3%=10;
switch(d3){
case 0:printf("thousand ");break;
case 1:printf("Ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d4=result1/1000;
d4%=10;
switch(d4){

case 1:printf("one thousand");break;
case 2:printf("two thousand ");break;
case 3:printf("three thousand ");break;
case 4:printf("four thousand ");break;
case 5:printf("five thousand ");break;
case 6:printf("six thousand ");break;
case 7:printf("seven thousand ");break;
case 8:printf("eight thousand ");break;
case 9:printf("nine thousand ");break;
}

d5=result1/100;
d5%=10;
switch(d5){

case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d6=result1/10;
d6%=10;
switch(d6){

case 1:printf("one ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d7=result1%10;
switch(d7){

case 1:printf("one");break;
case 2:printf("two");break;
case 3:printf("three");break;
case 4:printf("four");break;
case 5:printf("five");break;
case 6:printf("six");break;
case 7:printf("seven");break;
case 8:printf("eight");break;
case 9:printf("nine");break;
}
break;

case 8:

d1=result1/10000000;
d1%=10;
switch(d1){
case 0:printf("zero");break;
case 1:printf("Ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d2=result1/1000000;
d2%=10;
switch(d2){
case 0:printf("zero");break;
case 1:printf("one million ");break;
case 2:printf("two million ");break;
case 3:printf("three million ");break;
case 4:printf("four million ");break;
case 5:printf("five million ");break;
case 6:printf("six million ");break;
case 7:printf("seven million ");break;
case 8:printf("eight million ");break;
case 9:printf("nine million ");break;
}

d3=result1/100000;
d3%=10;
switch(d3){
case 0:printf("zero");break;
case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d4=result1/10000;
d4%=10;
switch(d4){
case 0:printf("zero");break;
case 1:printf("Ten ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d5=result1/1000;
d5%=10;
switch(d5){
case 0:printf("zero");break;
case 1:printf("one thousand");break;
case 2:printf("two thousand ");break;
case 3:printf("three thousand ");break;
case 4:printf("four thousand ");break;
case 5:printf("five thousand ");break;
case 6:printf("six thousand ");break;
case 7:printf("seven thousand ");break;
case 8:printf("eight thousand ");break;
case 9:printf("nine thousand ");break;
}

d6=result1/100;
d6%=10;
switch(d6){
case 0:printf("zero");break;
case 1:printf("one hundred ");break;
case 2:printf("two hundred ");break;
case 3:printf("three hundred ");break;
case 4:printf("four hundred ");break;
case 5:printf("five hundred ");break;
case 6:printf("six hundred ");break;
case 7:printf("seven hundred ");break;
case 8:printf("eight hundred ");break;
case 9:printf("nine hundred ");break;
}

d7=result1/10;
d7%=10;
switch(d7){
case 0:printf("zero");break;
case 1:printf("one ");break;
case 2:printf("twenty ");break;
case 3:printf("thirty ");break;
case 4:printf("fourty ");break;
case 5:printf("fifty ");break;
case 6:printf("sixty ");break;
case 7:printf("seventy ");break;
case 8:printf("eighty ");break;
case 9:printf("ninety ");break;
}

d8=result1%10;
switch(d8){
case 0:printf("zero");break;
case 1:printf("one");break;
case 2:printf("two");break;
case 3:printf("three");break;
case 4:printf("four");break;
case 5:printf("five");break;
case 6:printf("six");break;
case 7:printf("seven");break;
case 8:printf("eight");break;
case 9:printf("nine");break;
}
break;

}
}

return 0;

}

Can I ask you all, why I can't use +,-,x,/ and = ? I can't do it except I substitute the +-x/= with 1,2,3,4,0 .....
Please help me solve it!

The actually is : printf
Last edited by admin : 06-Sep-2008 at 14:17. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #14  
Old 06-Sep-2008, 13:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by kcp88
Can i ask you all , why i cant use +,-,x,/ and = ?

Make the operator a char variable. Note that when you use scanf for a numeric variable, a '\n' char is always left in the system input buffer. You have to get rid of it before you try scanf to read a char variable. Also, since you have gone to so much trouble to get the output like you like it, I recommend that you test input for valid user entry. (I always recommend this. Strongly.)

Maybe something like:

CPP / C++ / C Code:
.
.
.
    char oprator;
    result2 = input1 = input2 = oprator= 0;
    printf("Enter first value : ");
    if (scanf("%f", &input1) != 1) {
        printf("Invalid input\n");
        return 0;
    }

    do {
        /* eat the rest of the line, including the newline character */
        while (getchar() != '\n')
            ;
        printf("Enter oprator: ");
        scanf("%c", &oprator);
        switch (oprator) {
            case '+':
            case '-':
            case '*':
            case '/':
            case '=':
                break;
            default:
                printf("Invalid oprator\n");
                return 0;
        }

        if (oprator != '=') {
            printf("Enter next value : ");
            if (scanf("%f", &input2) != 1) {
                printf("Invalid input\n");
                return 0;
            }

            switch (oprator) {

            case '+':
                result2 = input1 + input2;
                input1 = result2;
                break;

            case '-':
                result2 = input1 - input2;
                input1 = result2;
                break;

            case '*':
                result2 = input1 * input2;
                input1 = result2;
                break;

            case '/':
                result2 = input1 / input2;
                input1 = result2;
                break;
            }
        }
    } while (oprator != '=');

Quote:
Originally Posted by kcp88
The actually is : printf

When posting code:

1. Highlight the code in the edit window. Not your description or other narrative; just the code itself.

2. Click the # icon above the edit window. This puts "code tags" around the code

3. Click the "Preview Post" button before submitting your post. This lets you see if you got it right.

Regards,

Dave
  #15  
Old 06-Sep-2008, 13:48
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

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


Thanks very much !!!
  #16  
Old 06-Sep-2008, 13:55
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

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


May i ask one more question ? Erm .. it is about my switch loops .... Can i shorter it ? It is damn long ! Any better way to do it ? I mean may be by using if ..... else , or others . Very sorry because I'm beginner . really thanks a lots ! My results have to be in alphabet .....
  #17  
Old 06-Sep-2008, 17:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by kcp88
I'm beginner
OK, we get it. My observation is that this is not what I would consider a "normal" programming project for a beginner.

For example, looking back at your original post:

There is no standard library function that can read numbers with commas separating groups of three digits in the integer part of a number. You certainly can't use scanf("%f)" or any other form of standard C library I/O functions.

Since you are apparently happy with scanf for inputs, I'll look at the output requirements, as I try to guess what you really need to do.

If you do need to change the input stuff, I would break that off as a separate test program and make sure I could meet requirements with just the input. Then I would integrate it into the program along with the calculation and output parts. For now, let's stick with scanf.

Writing the value of a numeric quantity in words is an interesting problem, and there are many approaches; some are more "elegant" than others. I think that using arrays of pointers to char is a reasonable way, and I'll show an example. But---if you are a beginner and the course is for beginners, then I'm not sure that is appropriate for your application.

I have never seen a problem where the input is a mixed number (integer part and a fractional part expressed in decimal) where the integer part was given with comma-separated groups of three digits. I mean 10,000.33? Really?

That brings up the most important point from my perspective:

Exactly what was the assignment?

By that, I mean:

What range of input and output numbers is the program expected to handle? What is the required precision (number of correct significant decimal digits or number of decimal places in the output)?

If you are going to use floats to hold internal values, the total precision of any variable will be limited to something like seven significant decimal digits. Furthermore, since internal representations are based on binary floating point numbers, many perfectly fine, exact decimal numbers can't be represented exactly inside the machine.


So, for example, 1.0078125 can be represented exactly as the value of a floating point variable, but 10000.1 can not. The result: If the answer is exactly 1.0078125, you may be able to print its value exactly. If the result should be 10000.1, and you just spit out stuff calculated from the machine digits, the decimal digits might come out to be something like 10000.0996094...

OK, enough of that. Let's get to the easy part: the integer values and how to print the names of the numbers.
Quote:
Originally Posted by kcp88
... about my switch loops .... Can i shorter it ...may be by using if ..... else....

About switch statements versus if-else stuff: It's a matter of style, not substance. Your original question is more important: If you are going to print integer values from 1 to 1000, do you need 1000 cases (or, equivalently, 1000 if() conditions)?

Here's what I like to do: Separate the tasks of the program. I can test the output part without having to supply inputs and let the program do the calculations each time.

So: Let's just concentrate on integer outputs for now. Furthermore, lets just handle integer values from zero to nine.

With a case statement:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int n;

    printf("Enter an integer from 0 to 9: ");
    while ((scanf("%d", &n) == 1) && (n >= 0) && (n <= 9)) {
        printf("You entered %d: ", n);
        switch (n) {
            case 0: printf("zero");
                    break;
            case 1: printf("one");
                    break;
            case 2: printf("two");
                    break;
            case 3: printf("three");
                    break;
            case 4: printf("four");
                    break;
            case 5: printf("five");
                    break;
            case 6: printf("six");
                    break;
            case 7: printf("seven");
                    break;
            case 8: printf("eight");
                    break;
            case 9: printf("nine");
                    break;
        }
        printf("\nEnter an integer from 0 to 9: ");
    }
    return 0;
}

I suggest that you compile and run something like this. Make absolutely sure that it works for all valid inputs (it bails out if you give it anything other than a decimal number from zero to nine). If you want to change stuff, then do it. If you want to start all over with our own nomenclature, then do it. But, make sure you understand it and test it.

With if-else stuff

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int n;

    printf("Enter an integer from 0 to 9: ");
    while ((scanf("%d", &n) == 1) && (n >= 0) && (n <= 9)) {
        printf("You entered %d: ", n);
        if (n == 0) {
            printf("zero");
        }
        else if (n == 1) {
            printf("one");
        }
        else if (n == 2) {
            printf("two");
        }
        else if (n == 3) {
            printf("three");
        }
        else if (n == 4) {
            printf("four");
        }
        else if (n == 5) {
            printf("five");
        }
        else if (n == 6) {
            printf("six");
        }
        else if (n == 7) {
            printf("seven");
        }
        else if (n == 8) {
            printf("eight");
        }
        else {
            printf("nine");
        }
        printf("%s\n", units[n]);
        printf("\nEnter an integer from 0 to 9: ");
    }
    return 0;
}

(Same comments as the previous example.)


Now, what's the difference between the programs? Style, not substance. On simple things like this, I can't see where it makes a heckofa lot of difference. I think it's important to be familiar with both forms, since sometimes one might be more obvious (and easier to debug or to "verify by inspection").

Now, here's how I might do it so that I don't have all of those cases or all of those if() thingies: Use an array or pointers to char. Initialize the array to point to literal strings with the names of the numbers:

With an array of pointers to literal strings consisting of the names of the numbers:

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    int n;
    char *units[10] = {
        "zero", "one", "two"  , "three", "four",
        "five", "six", "seven", "eight", "nine"
    };
    printf("Enter an integer from 0 to 9: ");
    while ((scanf("%d", &n) == 1) && (n >= 0) && (n <= 9)) {
        printf("You entered %d: ", n);
        printf("%s\n", units[n]);
        printf("\nEnter an integer from 0 to 9: ");
    }
    return 0;
}

Which is "best"? Take your choice.

Now, to the bigger picture, what about 11, 12, 13, ..., 19? I think that the most straightforward way is just to make all of these special cases. So, it would require 20 case statement labels, 19 if() conditions, or a simple augmentation of the name array. I'll show the latter:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int n;
    char *teens[20] = {
        "zero"   , "one"    , "two"      , "three"   , "four"    ,
        "five"   , "six"    , "seven"    , "eight"   , "nine"    ,
        "ten"    , "eleven" , "twelve"   , "thirteen", "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
    };
    printf("Enter an integer from 0 to 19: ");
    while ((scanf("%d", &n) == 1) && (n >= 0) && (n <= 19)) {
        printf("You entered %d: ", n);
        printf("%s\n", teens[n]);
        printf("\nEnter an integer from 0 to 19: ");
    }
    return 0;
}

OK so far? Note how careful I am to make sure that I feed legitimate values to the array index. That's really, really important.

How about 20-99?

Now, from this point, the nomenclature becomes regular (in English, at least). I am assuming that you will always print 87 as "eighty-seven," and not "four score and seven." Stuff like that.

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int n;
    /* note that the first two aren't used; I just show the place holders */
    char *tens[10]  = {
        "zero" , "ten"  , "twenty" , "thirty", "forty", 
        "fifty", "sixty", "seventy", "eighty", "ninety"
    };
    char *teens[20] = {
        "zero"   , "one"    , "two"      , "three"   , "four"    ,
        "five"   , "six"    , "seven"    , "eight"   , "nine"    ,
        "ten"    , "eleven" , "twelve"   , "thirteen", "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
    };
    int tens_digit;
    int units_digit;
    printf("Enter an integer from 0 to 99: ");
    while ((scanf("%d", &n) == 1) && (n >= 0) && (n <= 99)) {
        printf("You entered %d: ", n);
        if (n < 20) {
            printf("%s", teens[n]);
        }
        else {
            tens_digit  = n / 10; /* we know that this is > 1       */
            units_digit = n % 10; /* use only the first ten members */
            printf("%s",tens[tens_digit]);
            if (units_digit != 0) {
                printf("-%s", teens[units_digit]);
            }
        }
        printf("\n\nEnter an integer from 0 to 99: ");
    }
    return 0;
}

Note that I have used the convention that I consider "standard." Namely, that for numbers greater than 20, if the units digit is not zero, there is a hyphen. So, for example, 37 is printed out as "thirty-seven"

Now, you can go on from here with hundreds, thousands, etc. You don't need any new arrays and the logic is not very difficult. For numbers greater than 20, Just extract digits and index into an array. A little more head scratching should lead to the ability to print numbers with "hundred", "thousand," etc. You don't need any more arrays. The rest is just simple arithmetic and simple logic. (I know, I know: That's easy for me to say. See Footnote.)

I would probably put the actual number-to-name stuff in one or more functions that would each write to a string rather than printing out directly, but, again, that's a matter of style, rather than substance. I will say, that as programs get larger than a few tens of lines style becomes extremely important. (Yours has more than a thousand lines, and I don't think your output stuff actually works very well, and it isn't very extensible, I'm thinking.)

I hate to repeat myself, but I can't believe that this is, seriously, a real "beginner's" assignment. (We haven't even got to the floating point stuff yet.)

What, exactly, is the course? Are there any prerequisites?


Regards,

Dave

Footnote:
"No one was born knowing this stuff, you know."
---davekw7x
  #18  
Old 06-Sep-2008, 22:14
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

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


Seriously , I'm really a beginner ..... Only learnt programming around 1 month and something ...

However , i'm really appreciate . Thank you so much ! I gain so much in this forum !
  #19  
Old 07-Sep-2008, 10:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

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


Quote:
Originally Posted by kcp88
....I'm really a beginner...
I wasn't accusing you of being a non-beginner.

There's nothing wrong with being a non-beginner, and there's nothing wrong with non-beginners asking questions.


If an instructor gives a class of beginners an assignment that, in my opinion, is not a "typical" beginner's assignment, then a couple of things come to mind:

1. My opinion of "typical" is completely irrelevant.

2. The instructor wants people to learn to dig things out that are not covered explicitly in materials for that class or the various prerequisite courses.

3. All of the above.

Regards,

Dave

As I re-read your previous posts, I am struck by your statement to the effect that, "instructor doesn't want to give us any hints...ask us do it by ourselves .." I assume that implies permission to get help from strangers on the internet rather than asking the instructor directly. Or were you really expected to isolate yourself, totally insulated from outside resources, and "think" of a solution? If that's the case, please ignore all my previous posts.

In other words:

"I object!"
---Hamilton Burger on about a million episodes (maybe more) of Perry Mason

"The jury will disregard that question and answer..."
---Judges on about a million episodes (maybe more) of Perry Mason
  #20  
Old 07-Sep-2008, 13:31
kcp88 kcp88 is offline
New Member
 
Join Date: Aug 2008
Posts: 15
kcp88 can only hope to improve

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


Thanks a lot ! you help me a lots ! Now i complete my assignment already ! Thansk !
 
 

Recent GIDBlogOnce again, no time for hobbies 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 08:55.


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