c语言 11-10

时间:2021-06-02 15:47:19   收藏:0   阅读:21

1、strtoi

#include <stdio.h>

int strtoi(const char *s)
{
    int i, j = 0;
    while(*s)
    {
        for(i = 0; i <= 9; i++)
        {
            if(*s - 0 == i)
                j = j * 10 + i;
        }
        s++;
    }
    return j;
}

int main(void)
{
    char str[128];
    printf("str:  "); scanf("%s", str);
    
    printf("result afrer concersion: %d\n", strtoi(str));
    return 0;
}

技术分享图片

 

 

2、

#include <stdio.h>

long strtol(const char *s)  //??????????????????
{
    int i;
    long j = 0;
    while(*s)
    {
        for(i = 0; i < 9; i++)
        {
            if(*s - 0 == i)
                j = j * 10 + (long)i;
        }
        s++;
    }
    return j;
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    
    printf("result after conversion: %d\n", strtol(str));
    return 0;
}

技术分享图片

 

 

3、

#include <stdio.h>

double strtof(const char *s)
{
    int i;
    double j = 0.0;
    while(*s)
    {
        for(i = 0; i <= 9; i++)
        {
            if(*s - 0 == i)
                j = j * 10 + (double)i;
        }
        s++;
    }
    return j;
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    
    printf("result after conversion: %f\n", strtof(str));
    return 0;
}

技术分享图片

 

原文:https://www.cnblogs.com/liujiaxin2018/p/14840508.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!