http://www.hackerschool.org/HS_Boards/zboard.php?id=QNA_programming&no=6410 [º¹»ç]
#include <stdio.h>
#include <ctype.h>
int hexValue(char ch)
{
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)
{
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;
} |
Hit : 3782 Date : 2013/11/16 10:15
|