Chinaunix首页 | 论坛 | 博客
  • 博客访问: 916750
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-07-12 11:07:05

    先把程序罗列一下了:

[root@LinServer c-study]# cat -n square_root.c
     1  #include
     2  double
     3  square_root (int n)
     4  {
     5    double a_i_1;
     6    double a_i = 1;
     7
     8    while (1)
     9      {
    10        a_i_1 = (a_i + n / a_i) / 2;
    11        if (a_i_1 == a_i)
    12          {
    13            return a_i;
    14            break;
    15          }
    16        else
    17          a_i = a_i_1;
    18
    19
    20      }
    21
    22  }
    23
    24  int
    25  main (void)
    26  {
    27    int i;
    28    double result;
    29    printf
    30      ("please input a number , i will give the square root of it to u @_@ \n");
    31    scanf ("%d", &i);
    32    result = square_root (i);
    33    printf ("squrare of %d: %lf\n", i, result);
    34    return 0;
    35  }
[root@LinServer c-study]# ./a.out
please input a number , i will give the square root of it to u @_@
78567
squrare of 78567: 280.298056
[root@LinServer c-study]# awk 'BEGIN{print 280.298056*280.298056}'
78567

结果的确是挺准确的啊,不过这个求法实在是太土了,想用递归来实现它,另外,发现个小小的问题:如果开头不include 库文件,会报这样的错:
[root@LinServer c-study]# gcc square_root.c
square_root.c: In function ¡®main¡¯:
square_root.c:28: warning: incompatible implicit declaration of built-in function ¡®scanf¡¯
square_root.c:30: warning: incompatible implicit declaration of built-in function ¡®printf

当时我还man scanf看了看,实在是寒啊。
通过这个小程序我学到了什么:
1、跳出循环;
2、秉承这章《语句》的思想,继续使用语句;
3、函数的返回类型应该是float、或者double等有精度的,不能指望int来搞定了
4、一站式里说,循环能做的,递归肯定能做,所以,还得想象递归怎么实现它
阅读(547) | 评论(0) | 转发(0) |
0

上一篇:吃hamberger的程序

下一篇:打印prime

给主人留下些什么吧!~~