打印

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

[复制链接]
683|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
elecintop|  楼主 | 2014-7-26 21:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stdafx.h"

double sqrt(double temp)
{       
        double before, after;
        before = 1.0;
        after = 1.0;
        do
        {
                before = after;
                after = (before + temp/before)/2;
        } while (before != after);
        return after;
}


int _tmain(int argc, _TCHAR* argv[])
{
        double temp,result;
        puts("input N:");
        scanf_s("%lf",&temp);
        result = sqrt(temp);
        printf("sqrt N = %lf \n",result);
        return 0;
}


相关帖子

沙发
elecintop|  楼主 | 2014-7-26 21:26 | 只看该作者
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
        int length = 100;
        puts("Prime(1~100):\n");
        for (int i = 1; i <= length; i++)
        {
                int ALU = 0;
                for (int j =1; j <= i; j++)
                {
                        if (i%j == 0)
                        {
                                ALU++;
                        }
                }
                if (ALU == 2)
                {
                        printf("%d\t", i);
                }
        }
        return 0;
}

使用特权

评论回复
板凳
elecintop|  楼主 | 2014-7-26 21:26 | 只看该作者
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
        int a,b,c;
        puts("输入三边长度(a,b,c):\n");
        do
        {
                scanf_s("%d,%d,%d",&a,&b,&c);
        } while ((a+b)<=c || (a+c)<=b || (b+c)<=a);
        if ((a == b)&&(b == c))
        {
                puts("等边!");
        }
        else
                if ((a==b)||(b==c)||(c==a))
                {
                        puts("等腰!");
                }
                else
                {
                        puts("不等边!");
                }
        return 0;
}

使用特权

评论回复
地板
elecintop|  楼主 | 2014-7-26 21:27 | 只看该作者
void copy_n( char dst[], char src[], int n )
{
        int i,l_src;
        l_src = 0;
        for (i = 0; dst[i] == '\0'; i++)
        {
                l_src++;
        }
        if (l_src < n)
        {
                for (i = 0; i < l_src; i++)
                {
                        dst[i] = src[i];
                }
                for (i = l_src; i < n; i++)
                {
                        dst[i] = '\0';
                }
        }
        else
        {
                for (i = 0; i < n; i++)
                {
                        dst[i] = src[i];
                }
        }
}

使用特权

评论回复
5
elecintop|  楼主 | 2014-7-26 21:27 | 只看该作者
void copy_n( char dst[], char src[], int n )
{
        int dst_index, src_index;
        src_index = 0;
        for (dst_index = 0; dst_index < n; dst_index++)
        {
                dst[dst_index] = src[src_index];
                if (src[src_index] != 0)
                {
                        src_index++;
                }
        }
}

使用特权

评论回复
6
elecintop|  楼主 | 2014-7-26 21:28 | 只看该作者
void substr(char dst[], char src[], int start, int len)
{
        int dst_index, src_index, l_src;
        l_src =        src_index = 0;
        do
        {
                l_src++;
        } while (src[l_src] != '\0' );


        if (start < 0 || len < 0 || start > l_src ++)
        {
                dst[0] = '\0';
        }
        else
        {
                for ( dst_index = 0; dst_index < len; dst_index++)
                {
                        dst[dst_index] = src[src_index + start];
                        if (src[src_index] != '\0')
                        {
                                src_index++;
                        }
                }
        }
}

使用特权

评论回复
7
elecintop|  楼主 | 2014-7-26 21:28 | 只看该作者
void deblank(char string[])
{
        char copy_str[] = {'\0'};

        int str_index, copy_str_index, temp = 0;
        for ( str_index = 0; string[str_index] != '\0'; str_index++)
        {
                if (string[str_index] == ' ')
                {
                        if (temp > 0)
                        {
                                continue;
                        }
                        else
                        {
                                copy_str_index++;
                                copy_str[copy_str_index] = string[str_index];
                                temp++;
                        }
                }
                else
                {
                        copy_str_index++;
                        copy_str[copy_str_index] = string[str_index];
                }
        }
        copy_str[copy_str_index++] = '\0';
        for ( copy_str_index = 0; copy_str[copy_str_index] != '\0'; copy_str_index++)
        {
                string[copy_str_index]= copy_str[copy_str_index];
        }
        string[copy_str_index + 1] = '\0';
}

使用特权

评论回复
8
angerbird| | 2014-7-27 19:40 | 只看该作者
这写代码的都是在讲指针的么?看的有点眼花缭乱的。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

176

主题

1329

帖子

3

粉丝