【转】 C和指针 (pointers on C)——5

[复制链接]
 楼主| elecintop 发表于 2014-7-26 21:25 | 显示全部楼层 |阅读模式
  1. #include "stdafx.h"

  2. double sqrt(double temp)
  3. {       
  4.         double before, after;
  5.         before = 1.0;
  6.         after = 1.0;
  7.         do
  8.         {
  9.                 before = after;
  10.                 after = (before + temp/before)/2;
  11.         } while (before != after);
  12.         return after;
  13. }


  14. int _tmain(int argc, _TCHAR* argv[])
  15. {
  16.         double temp,result;
  17.         puts("input N:");
  18.         scanf_s("%lf",&temp);
  19.         result = sqrt(temp);
  20.         printf("sqrt N = %lf \n",result);
  21.         return 0;
  22. }


 楼主| elecintop 发表于 2014-7-26 21:26 | 显示全部楼层
  1. #include "stdafx.h"

  2. int _tmain(int argc, _TCHAR* argv[])
  3. {
  4.         int length = 100;
  5.         puts("Prime(1~100):\n");
  6.         for (int i = 1; i <= length; i++)
  7.         {
  8.                 int ALU = 0;
  9.                 for (int j =1; j <= i; j++)
  10.                 {
  11.                         if (i%j == 0)
  12.                         {
  13.                                 ALU++;
  14.                         }
  15.                 }
  16.                 if (ALU == 2)
  17.                 {
  18.                         printf("%d\t", i);
  19.                 }
  20.         }
  21.         return 0;
  22. }
 楼主| elecintop 发表于 2014-7-26 21:26 | 显示全部楼层
  1. #include "stdafx.h"


  2. int _tmain(int argc, _TCHAR* argv[])
  3. {
  4.         int a,b,c;
  5.         puts("输入三边长度(a,b,c):\n");
  6.         do
  7.         {
  8.                 scanf_s("%d,%d,%d",&a,&b,&c);
  9.         } while ((a+b)<=c || (a+c)<=b || (b+c)<=a);
  10.         if ((a == b)&&(b == c))
  11.         {
  12.                 puts("等边!");
  13.         }
  14.         else
  15.                 if ((a==b)||(b==c)||(c==a))
  16.                 {
  17.                         puts("等腰!");
  18.                 }
  19.                 else
  20.                 {
  21.                         puts("不等边!");
  22.                 }
  23.         return 0;
  24. }
 楼主| elecintop 发表于 2014-7-26 21:27 | 显示全部楼层
  1. void copy_n( char dst[], char src[], int n )
  2. {
  3.         int i,l_src;
  4.         l_src = 0;
  5.         for (i = 0; dst[i] == '\0'; i++)
  6.         {
  7.                 l_src++;
  8.         }
  9.         if (l_src < n)
  10.         {
  11.                 for (i = 0; i < l_src; i++)
  12.                 {
  13.                         dst[i] = src[i];
  14.                 }
  15.                 for (i = l_src; i < n; i++)
  16.                 {
  17.                         dst[i] = '\0';
  18.                 }
  19.         }
  20.         else
  21.         {
  22.                 for (i = 0; i < n; i++)
  23.                 {
  24.                         dst[i] = src[i];
  25.                 }
  26.         }
  27. }
 楼主| elecintop 发表于 2014-7-26 21:27 | 显示全部楼层
  1. void copy_n( char dst[], char src[], int n )
  2. {
  3.         int dst_index, src_index;
  4.         src_index = 0;
  5.         for (dst_index = 0; dst_index < n; dst_index++)
  6.         {
  7.                 dst[dst_index] = src[src_index];
  8.                 if (src[src_index] != 0)
  9.                 {
  10.                         src_index++;
  11.                 }
  12.         }
  13. }
 楼主| elecintop 发表于 2014-7-26 21:28 | 显示全部楼层
  1. void substr(char dst[], char src[], int start, int len)
  2. {
  3.         int dst_index, src_index, l_src;
  4.         l_src =        src_index = 0;
  5.         do
  6.         {
  7.                 l_src++;
  8.         } while (src[l_src] != '\0' );


  9.         if (start < 0 || len < 0 || start > l_src ++)
  10.         {
  11.                 dst[0] = '\0';
  12.         }
  13.         else
  14.         {
  15.                 for ( dst_index = 0; dst_index < len; dst_index++)
  16.                 {
  17.                         dst[dst_index] = src[src_index + start];
  18.                         if (src[src_index] != '\0')
  19.                         {
  20.                                 src_index++;
  21.                         }
  22.                 }
  23.         }
  24. }
 楼主| elecintop 发表于 2014-7-26 21:28 | 显示全部楼层
  1. void deblank(char string[])
  2. {
  3.         char copy_str[] = {'\0'};

  4.         int str_index, copy_str_index, temp = 0;
  5.         for ( str_index = 0; string[str_index] != '\0'; str_index++)
  6.         {
  7.                 if (string[str_index] == ' ')
  8.                 {
  9.                         if (temp > 0)
  10.                         {
  11.                                 continue;
  12.                         }
  13.                         else
  14.                         {
  15.                                 copy_str_index++;
  16.                                 copy_str[copy_str_index] = string[str_index];
  17.                                 temp++;
  18.                         }
  19.                 }
  20.                 else
  21.                 {
  22.                         copy_str_index++;
  23.                         copy_str[copy_str_index] = string[str_index];
  24.                 }
  25.         }
  26.         copy_str[copy_str_index++] = '\0';
  27.         for ( copy_str_index = 0; copy_str[copy_str_index] != '\0'; copy_str_index++)
  28.         {
  29.                 string[copy_str_index]= copy_str[copy_str_index];
  30.         }
  31.         string[copy_str_index + 1] = '\0';
  32. }
angerbird 发表于 2014-7-27 19:40 | 显示全部楼层
这写代码的都是在讲指针的么?看的有点眼花缭乱的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

176

主题

1329

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部