2008. 4. 16. 17:15
@GSMC/서용덕: DMD Programming
음냐리~ 공부하려고 한 거 아닌데... 계산기 찾는 것보다 코딩하는 게 더 간편하게 느껴져서. 쿄쿄.
//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;
}
#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
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;
}
#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.
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.
아 놔~ 나 숙제 안 하고 뭐하고 있는 거지? 열도 안 떨어지는구만. ㅜㅜ
'@GSMC > 서용덕: DMD Programming' 카테고리의 다른 글
hw 6 - 9장 연습문제 3번 (0) | 2008.04.17 |
---|---|
[Steve Oualline] 9. Variable Scope and Functions (0) | 2008.04.16 |
week 6 review (0) | 2008.04.16 |
[Steve Oualline] 8. More Control Statements (0) | 2008.04.06 |
[Steve Oualline] 7. Programming Process (0) | 2008.04.05 |