将数字转换成16进制字符串,从字符串中去除空格,求平方根

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 }

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、求平方根

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 }

转载于:https://www.cnblogs.com/ke-frank/archive/2011/03/04/1971111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值