블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
  • total
  • today
  • yesterday

Category


음냐리~ 공부하려고 한 거 아닌데... 계산기 찾는 것보다 코딩하는 게 더 간편하게 느껴져서. 쿄쿄.

//exponentiation for a number

#include <iostream>

char line[100];    // input data
int number;    // a number converted from input
int exponent = 0;   
int base = 2;

int main (int argc, char * const argv[]) {
    // to get a number from a user
    printf("Enter a number    :    ");
    fgets(line, sizeof(line), stdin);
   
    // to convert input data to number
    number = atoi(line);
   
    // to calculate its exponent on the base
    int i;
    for (i=0; number>1; i++)
    {
        number /= base;
        exponent = i+1;
    }
    printf("base %d to the %d-th power\n", base, exponent);
       
    return 0;
}



실행창:
Enter a number    :    65536
base 2 to the 16-th power


업그레이드 버전:
//exponentiation for a number

#include <iostream>

char line[100];    // input data
int number;    // a number converted from input
int exponent = 0; // exponent number to be calculated   
int base = 2; // base to power

int main (int argc, char * const argv[]) {
    while(1)
    {
        // to get a number from a user
        printf("Enter a number    :    ");
        fgets(line, sizeof(line), stdin);
       
        if (line[0]==10)    // no input but enter
            break;    // exit
        else
        {
        // to convert input data to number
            number = atoi(line);
           
            // to calculate its exponent on the base
            int i;
            int flag = 0; // indicator
            int origin = number; //to reserve the input number
            for (i=0; number>=base; i++)
            {
                int remainder = number % base;
                if (remainder != 0) //not power based on base number
                {   
                    printf("%d does not have %d-based power.\n", origin, base);
                    flag = 1; //error to calculate exponentiation
                    break;
                }
                else
                {   
                    number /= base;               
                    exponent = i+1;
                }
            }
            if (flag==1) //error
                continue;
            else if(flag==0)
                printf("base %d to the %d-th power\n", base, exponent);
        }
    }
        return 0;
}


실행창:
Enter a number    :    65536
base 2 to the 16-th power
Enter a number    :    2
base 2 to the 1-th power
Enter a number    :    3
3 does not have 2-based power.
Enter a number    :    256
base 2 to the 8-th power
Enter a number    :    255
255 does not have 2-based power.
Enter a number    :   

expon has exited with status 0.

아 놔~ 나 숙제 안 하고 뭐하고 있는 거지? 열도 안 떨어지는구만. ㅜㅜ

posted by maetel