Home > Back-end >  For a great god, and a simple question
For a great god, and a simple question

Time:09-29

Experiment 2-3

Implement the following function C, complete the hexadecimal conversion,

# include

/* can add other header files to be used here */

using namespace std;

//targ has a value of 8 said to transform n into 8 hexadecimal (such as 128, converted to: 0200), a value of 16 said n to convert hexadecimal (such as 255 converted to uppercase: 0 XFF)

//converted form as a string, the result of the allocated storage space with the need to use new

//such as the opening size of 100 consecutive character space (the same as the array) way for:

//char * pbufs=new char [100].

Char * Convert (int n, int targ)

{

}

Int main ()

{

int n;

Cin & gt;> n;

For (int I=0; i
{

Int num, targ;

Char * pRet=NULL;

Cin & gt;> Num & gt;> Targ;

PRet=the Convert (num, targ);

Cout & lt;
The delete [] pRet;

}

return 0;

}

Input:

Multiple lines, the first line contains an integer n said the next n lines of input, the next n lines of two integers in each row, for the first integer to convert numerical value, the second integer represents the target into the system,

Output:

N lines, each behavior transformed as a result,

The sample input:

2

255 16

128

The sample output:

0 XFF

0200

CodePudding user response:

//DigitConvert. CPP: defines the entry point of the console application,
//

# include "stdafx. H"
# include & lt; iostream>
# include & lt; Stack>

using namespace std;

//targ has a value of 8 said to transform n into 8 hexadecimal (such as 128, converted to: 0200), a value of 16 said n to convert hexadecimal (such as 255 converted to uppercase: 0 XFF)
//converted form as a string, the result of the allocated storage space with the need to use new
//such as the opening size of 100 consecutive character space (the same as the array) way for:
//char * pbufs=new char [100].
Char intToChar (int n);

Char * Convert (int n, int targ)
{
Char * pbufs=new char [100].
Stack On STC.
Int m;
Char c=intToChar (14);
Cout & lt; While (n & gt; 0)
{
M=n/targ;
Int temp=n % targ;
On STC. Push (temp);
N=m;
}
int i=0;
while (! On STC. Empty ())
{
Int temp=on STC. The top ();
Pbufs [i++]=intToChar (temp);
On STC. Pop ();
}

Return pbufs;
}
Char intToChar (int n)
{
Char c;
If (n & lt; 0 | | n> 15) return '\ 0';
The switch (n)
{
Case 10:
C='A';
break;
Case 11:
C='B';
break;
Case 12:
C='c';
break;
Case 13:
C='D';
break;
Case 14:
C='E';
break;
Case 15:
C='F';
break;
Default:
C=48 + n.
break;
}
return c;
}

Int main ()
{
int n;
Cin & gt;> n;
for (int i=0; i {
Int num, targ;
Char * pRet=NULL;
Cin & gt;> Num & gt;> Targ;
PRet=the Convert (num, targ);
Cout & lt; The delete [] pRet;
system("pause");
}
return 0;
}
  • Related