Home > Back-end >  C will hex is converted to a decimal integer
C will hex is converted to a decimal integer

Time:11-28

CodePudding user response:

 
# define INT_SIZE 10

The class Integer
{
Public:
Integer (void)
{
OF_10=100000000;
OF_16=62500000;
InputBuffSize=10 * INT_SIZE + 1;
InputBuff=new char [inputBuffSize];
Memset (num, 0, sizeof (unsigned int) * INT_SIZE);
}

Bool Assign10 (char const * who)
{
//check valid
For (char const * input=who; * input!=0; + + input)
{
For (int I=INT_SIZE - 1; I & gt;=0; - I)
{
If (num [I] & gt;=OF_10 & amp; & I + I & lt; INT_SIZE)
{
Unsigned int bit=num [I]/OF_10;
Num [I] -=bit * OF_10;
Num [I + 1) +=bit;
}

Num [I] *=10;
}

If (' 0 '& lt;=* input & amp; & * input & lt;='9')
{
Num [0] +=* input - '0';
continue;
}

Memset (num, 0, sizeof (unsigned int) * INT_SIZE);
return false;
}

return true;
}

Bool Assign16 (char const * who)
{
//check valid
For (char const * input=who; * input!=0; + + input)
{
For (int I=INT_SIZE - 1; I & gt;=0; - I)
{
If (num [I] & gt;=OF_16 & amp; & I + I & lt; INT_SIZE)
{
Unsigned int bit=num [I]/OF_16;
Num [I] -=bit * OF_16;
Num [I + 1) +=bit;
}

Num [I] *=16;
}

If (' 0 '& lt;=* input & amp; & * input & lt;='9')
{
Num [0] +=* input - '0';
continue;
}

If (' a '& lt;=* input & amp; & * input & lt; )
='f'{
Num [0] +=10 + x input - 'a';
continue;
}

If (' A '& lt;=* input & amp; & * input & lt; )
='F'{
Num [0] +=10 + x input - 'A';
continue;
}

Memset (num, 0, sizeof (unsigned int) * INT_SIZE);
return false;
}


return true;
}

Void Scan10 (void)
{
Assign10 (gets_s (inputBuff inputBuffSize));
}

Void Scan16 (void)
{
Assign16 (gets_s (inputBuff inputBuffSize));
}

Void Print (void)
{
Bool output=false;
For (int I=INT_SIZE - 1; I & gt;=0; - I)
{
If (the output)
{
Printf (" %. 9 d, "num [I]);
}
Else if (num [I]!=0)
{
The output=true;
Printf (" % d ", num [I]);
}
}

if (! The output)
{
Printf (" 0 ");
}

printf("\r\n");
}

Private:
Unsigned int OF_10;
Unsigned int OF_16;
Int inputBuffSize;
Char * inputBuff;
Unsigned int num [INT_SIZE];
};

Int main ()
{
Integer (I);
I. can16 ();
I.P rint ();

system("pause");

return 0;
}


  • Related