0、将数字转换成16进制字符串
1
#include
<
stdlib.h
>
2 #include < stdio.h >
3 #include < string .h >
4 /* *********************************
5 * 使用sprintf()函数
6 * ******************************** */
7 int main()
8 {
9 char str[ 5 ];
10 int buf = 57 ;
11 sprintf(str, " %02x " ,buf);
12 sprintf(str + 2 , " %02x " ,buf);
13 str[ 5 ] = ' \0 ' ;
14
15 printf( " 0x%s\n " ,str);
16 return 0 ;
17 }
2 #include < stdio.h >
3 #include < string .h >
4 /* *********************************
5 * 使用sprintf()函数
6 * ******************************** */
7 int main()
8 {
9 char str[ 5 ];
10 int buf = 57 ;
11 sprintf(str, " %02x " ,buf);
12 sprintf(str + 2 , " %02x " ,buf);
13 str[ 5 ] = ' \0 ' ;
14
15 printf( " 0x%s\n " ,str);
16 return 0 ;
17 }
1、去多余空格
1
/*
*************************************
2 * 去除字符串中的多余的空格
3 * 即是将多个连续的空格变成一个空格
4 * ************************************ */
5 void deblank( char string [])
6 {
7 int flag = 0 ;
8 char str[ 100 ];
9 char * ptr;
10 ptr = string ;
11 int i = 0 ;
12
13 while ( * ptr != ' \0 ' ) {
14 printf( " %c\n " , * ptr);
15 if ( * ptr == ' ' && flag == 1 ) {
16 ptr ++ ;
17 // printf("the second space\n");
18 continue ;
19 }
20 else if ( * ptr == ' ' && flag == 0 ) {
21 str[i] = * ptr;
22 ptr ++ ;
23 i ++ ;
24 // printf("the first space\n");
25 flag = 1 ;
26 continue ;
27 }
28 // printf("not a space\n");
29 flag = 0 ;
30 str[i] = * ptr;
31 i ++ ;
32 ptr ++ ;
33 }
34 str[i] = ' \0 ' ;
35 printf( " %s\n " ,str);
36 }
2 * 去除字符串中的多余的空格
3 * 即是将多个连续的空格变成一个空格
4 * ************************************ */
5 void deblank( char string [])
6 {
7 int flag = 0 ;
8 char str[ 100 ];
9 char * ptr;
10 ptr = string ;
11 int i = 0 ;
12
13 while ( * ptr != ' \0 ' ) {
14 printf( " %c\n " , * ptr);
15 if ( * ptr == ' ' && flag == 1 ) {
16 ptr ++ ;
17 // printf("the second space\n");
18 continue ;
19 }
20 else if ( * ptr == ' ' && flag == 0 ) {
21 str[i] = * ptr;
22 ptr ++ ;
23 i ++ ;
24 // printf("the first space\n");
25 flag = 1 ;
26 continue ;
27 }
28 // printf("not a space\n");
29 flag = 0 ;
30 str[i] = * ptr;
31 i ++ ;
32 ptr ++ ;
33 }
34 str[i] = ' \0 ' ;
35 printf( " %s\n " ,str);
36 }
2、求平方根
1
/*
***************************************
2 * 求一个数的平方根
3 * 根据公式ai1 = (ai + n/ai) / 2
4 * 当 ai1 近似等于 ai 时就是平方根
5 * ************************************** */
6 float extract( float n)
7 {
8 float ai = 1 ;
9 float ai1;
10 ai1 = (ai + n / ai) / 2 ;
11
12 while (ai1 != ai) {
13 ai = ai1;
14 ai1 = (ai + n / ai) / 2 ;
15 }
16
17 return ai1;
18 }
2 * 求一个数的平方根
3 * 根据公式ai1 = (ai + n/ai) / 2
4 * 当 ai1 近似等于 ai 时就是平方根
5 * ************************************** */
6 float extract( float n)
7 {
8 float ai = 1 ;
9 float ai1;
10 ai1 = (ai + n / ai) / 2 ;
11
12 while (ai1 != ai) {
13 ai = ai1;
14 ai1 = (ai + n / ai) / 2 ;
15 }
16
17 return ai1;
18 }