| hch19860906 |
아래와 같이 완성하시면 됩니다.
#include <stdio.h>
#include <ctype.h>
int hexValue(char ch) // 10이상의 숫자를 A,B,C,D,E,F로 바꾸는 함수
{
switch(ch){
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
return (ch - '0');
case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':
return (ch - 'A'+10);
case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':
return (ch - 'a'+10);
default:
return -1;
}
}
int getIntNum(char firstCharacter) // firstCharacter에 따라 10진수, 8진수, 16진수인가를 판별합니다.
// firstCharacter가 0이면 8진수, 0이고 그다음문자가 x이면 16진수
// firstCharacter가 0이 아닌 숫자이면 10진수로 판별하여 10진수값을 계산합니다.
{
int num = 0;
int value;
char ch;
if (firstCharacter != '0')
{
ch = firstCharacter;
do
{
num = 10*num + (int)(ch-'0');
ch = getchar();
}while(isdigit(ch));
}
else
{
ch = getchar();
if((ch>='0')&&(ch <='7'))
do
{
num = 8*num + (int)(ch-'0');
ch = getchar();
}while((ch>='0')&&(ch <='7'));
else if ((ch == 'X') || (ch=='x'))
{
while((value = hexValue(ch=getchar())) != -1)
num = 16 * num + value ;
}
else num = 0;
}
ungetc(ch , stdin);
return num;
}
int main()
{
char firstcharacter;
int nDecimal;
firstcharacter = getchar();
nDecimal = getIntNum(firstcharacter);
printf("Decimal number is %d\n", nDecimal);
return 0;
} |
2013/11/16 |
|