Binary converter function + 1.0beta

First, run shot

1,

 

 

2,

 

 

 

3,

 

 

 

4,

 

 

 

5,

 

 

 

Second, the function introduction

1,

 

 

void TenToTwo () 
{ 
	int NUM, m, C, I = 0; 
	int n-= 2; 
	int A [32]; 
	the printf ( "Enter an integer:"); 
	Scanf ( "% D", & NUM); 
	m = NUM ; 
	the while (NUM> 0) 
	{ 
		C = (NUM% n-); 
		A [I] = C; 
		NUM = NUM / n-; 
		I ++; 
	} 
	the printf ( "decimal number% d is converted into a binary number is:", m); 
	for (i--; i> = 0 ; i--) // array output reverse 
		the printf ( "% D", A [I]); 
	the printf ( "\ n-"); 
}

 

 2,

void TenToEight () 
{ 
	int NUM, m, C, I = 0; 
	int n-=. 8; 
	int A [32]; 
	the printf ( "Enter an integer:"); 
	Scanf ( "% D", & NUM); 
	m = NUM ; 
	the while (NUM> 0) 
	{ 
		C = (NUM% n-); 
		A [I] = C; 
		NUM = NUM / n-; 
		I ++; 
	} 
	the printf ( "decimal number% d is converted into octal number is:", m); 
	for (i--; i> = 0 ; i--) // array output reverse 
		the printf ( "% D", A [I]); 
	the printf ( "\ n-"); 
}

  3,

void TenToSixteen () 
{ 
	char ARR [] = "0123456789ABCDEF"; 
	char hex [16]; 
	int I = 0; 
	int J = 0; 
	int NUM = 0, A = 0; 
	the printf ( "Enter an integer:"); 
	Scanf ( "% D", & NUM); 
	a = NUM; 
	the while (NUM) 
	{ 
		hex [I ++] = ARR [NUM% 16]; // remainder decimal numbers and eventually match the characters hextable array 
		num = num / 16; 
	} 
	the printf ( "% d decimal number hexadecimal number is converted to:", A); 
	for (I = J -. 1; J> = 0; --j) 
		the printf ( "% C", hex [ J]); 
	the printf ( "\ n-"); 
}

  4,

void TwoToTen () 
{ 
	Long Long n-, A; 
	int SUM = 0, I = 0, m; 
	the printf ( "Enter a binary number:"); 
	Scanf ( "% LLD", & n-); 
	A = n-; 
	the while (n- ! = 0) 
	{ 
		m = n-10%; 
		n-/ = 10; 
		SUM = m * + POW (2, I); 
		++ I; 
	} 
	the printf ( "% lld binary number is converted to decimal% d \ n", A, SUM); 
}

  5,

void EightToTen () 
{ 
	int n-, A; 
	int SUM = 0, I = 0, m; 
	the printf ( "Enter a octal number:"); 
	Scanf ( "% D", & n-); 
	A = n-; 
	the while (n-! = 0) 
	{ 
		m = n-% 10; 
		n-/ = 10; 
		SUM + = m * POW (. 8, I); 
		++ I; 
	} 
	the printf ( "eight-nary number% d is converted to decimal% d \ n", a , SUM); 
}

  6,

SixteenToTen void () 
{ 
	int NUM = 0; 
	the printf ( "Enter a hexadecimal number:"); 
	Scanf ( "% x", & NUM); 
	the printf ( "% x hexadecimal number is converted to decimal% d. \ n-", NUM, NUM); 
}

  Three, main function

int main()
{
	int n = 0;
	while (1)
	{
		mune();
		printf("请选择:");
		scanf("%d", &n);
		switch (n)
		{
		case 1:TenToTwo();
			break;
		case 2:TenToEight();
			break;
		case 3:TenToSixteen();
			break;
		case 4:TwoToTen();
			break;
		case 5:EightToTen();
			break;
		case 6:SixteenToTen();
			break;
		case 0:
			exit(0);
			break;
		default:
			printf("您的选择错误!!!\n");
			break;
		}
	}
}

  Fourth, the mind map

 

 

Fifth, the problems encountered and solutions

 

 

1, the beginning do not know how to write binary conversion function between the job and later submitted up by reference to the preparation of students as well as lookups by Baidu.

2, the function and the main function of convergence is not good, look through Baidu, he eventually link up with the switch.

Six, mutual evaluation codes (Guoqi Song)

 

char num2 (int num) // converts a number to a character type, so that the hex 16 may be calculated 
{ 
    IF (NUM> = 0 && NUM <=. 9) 
    { 
        return (char) ( '0' + NUM); 
    } 
    the else 
    { 
        return (char) ( 'A' + NUM - 10); 
    } 
}

 

  

IF (judge2,8,10 (A, n-)) 
            {
                the printf ( "of the digital binary number is:");
                chageeverything (changeten (A, n-), 2);
                the printf ( "\ n-");
                the printf ( " this number is a decimal number:% D \ n-", changeten (a, n-));
                the printf (" hexadecimal number is the number: ");
                chageeverything (changeten (a, n-), 16);
                BREAK ;
            }}

  1, first of all he enter a number into a character, hexadecimal avoid execution error will not occur.

       2, at first glance did not see what he was writing, he was later found in a function and put on another function. This approach is very novel.

Seven summary

 

 

1, recognizes the function room can be nested, you can make the function more introduction.

2, by the job, knowledge of such conversion between decimal deeper.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/cxdscx/p/11829699.html