프로그래밍

 3198, 1/160 회원가입  로그인  
   karicoru89
   c언어 진수 변환 코딩중인데요...

http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=6390 [복사]


일단 책에 주어진 함수를 이용하여 프로그램을 완성하는 건데요...
일단 주언진 함수가 ....
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 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;
     }
}


인데요....이것을 어떻게 완성해야되나요??

  Hit : 5379     Date : 2013/10/07 10:59



    
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