c语言习题解答求助
写一个函数,输入一个十六进制数,输出相应的十进制数。要求用函数 谢谢大神了[此贴子已经被作者于2017-3-27 18:17编辑过]
#include <stdio.h> #include <math.h> #include <stdlib.h> struct base { unsigned int a:4; unsigned int b:4; unsigned int c:4; unsigned int d:4; unsigned int e:4; unsigned int f:4; unsigned int g:4; unsigned int h:4; }; int base( char *s ); int main( void ) { char s1[ 20 ]; scanf("%s",s1); printf("%d\n", base(s1)); return 0; } int base( char *s ) { struct base *n; int a; a = strtol( s, NULL, 16); n = (struct base *)&a; return ( n->a * pow( 16, 0 ) + n->b * pow( 16 , 1 ) + n->c * pow( 16, 2 ) + n->d * pow( 16 , 3) + n->e * pow( 16, 4 ) + n->f * pow(16,5) + n->g * pow( 16, 6 ) + n->h * pow( 16, 7 ) ); }
#include <stdio.h> #include <stdlib.h> int base( char *s ); int main( void ) { char s1[ 20 ]; scanf("%s",s1); printf("%d\n", base(s1)); return 0; } int base( char *s ) { int a; int n; int m; a = strtol( s, NULL, 16); for( m = 0, n = 0xF; n; n <<= 4 ) m += (a & n); return m; }
[此贴子已经被作者于2017-3-27 18:56编辑过]