zoukankan      html  css  js  c++  java
  • Linux下c++中的atoi、atol、atoll、atof函数调用实例

    本文中调用的四个函数如下:

    atoi函数:将字符串转化为int类型变量

    atol函数:将字符串转化为long类型变量

    atoll函数:将字符串转化为long long类型变量

    atof函数:将字符串转化为double类型变量

    这些函数的转化过程,都是将一个字符串的可读部分取到变量中

    遇到不可读的部分,则直接终止读取

    调用示例:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define Seperate(); printf("
    =============
    
    ");
    
    int main()
    {
        Seperate();
    
        //atoi
        printf("atoi: string to integer
    ");
        const char* s00 = "1234567890";
        printf("%s -> %d
    ", s00, atoi(&s00[0]));    
        const char* s01 = "123.4";
        printf("%s -> %d
    ", s01, atoi(&s01[0]));
        const char* s02 = "xyz";
        printf("%s -> %d
    ", s02, atoi(&s02[0]));
        const char* s03 = "1234xyz";
        printf("%s -> %d
    ", s03, atoi(&s03[0]));
    
        Seperate();
    
        //atol
        printf("atol: string to long
    ");
        const char* s10 = "1234567890123";
        printf("%s -> %ld
    ", s10, atol(&s10[0]));    
        const char* s11 = "123.4";
        printf("%s -> %ld
    ", s11, atol(&s11[0]));
        const char* s12 = "xyz";
        printf("%s -> %ld
    ", s12, atol(&s12[0]));
        const char* s13 = "1234xyz";
        printf("%s -> %ld
    ", s13, atol(&s13[0]));
        
        Seperate();    
    
        //atoll
        printf("atoll: string to long long
    ");
        const char* s20 = "1234567890123";
        printf("%s -> %lld
    ", s20, atoll(&s20[0]));    
        const char* s21 = "123.4";
        printf("%s -> %lld
    ", s21, atoll(&s21[0]));
        const char* s22 = "xyz";
        printf("%s -> %lld
    ", s22, atoll(&s22[0]));
        const char* s23 = "1234xyz";
        printf("%s -> %lld
    ", s23, atoll(&s23[0]));    
    
        Seperate();
    
        //atof
        printf("atof: string to double
    ");
        const char* s30 = "1234567890";
        printf("%s -> %lf
    ", s30, atof(&s30[0]));    
        const char* s31 = "123.4";
        printf("%s -> %lf
    ", s31, atof(&s31[0]));
        const char* s32 = "xyz";
        printf("%s -> %lf
    ", s32, atof(&s32[0]));
        const char* s33 = "1234xyz";
        printf("%s -> %lf
    ", s33, atof(&s33[0]));    
    
        Seperate();
        
        return 0;
    }

    运行效果:

    END

     
  • 相关阅读:
    angry_birds_again_and_again(2014年山东省第五届ACM大学生程序设计竞赛A题)
    数学入门题目
    POJ1236:Network of Schools(tarjan+缩点)?
    POJ2186:Popular Cows(tarjan+缩点)
    HDU2426:Interesting Housing Problem(还没过,貌似入门题)
    POJ1175:Starry Night(bfs)
    POJ2506:Tiling(递推+大数斐波那契)
    POJ2135:Farm Tour
    POJ2195:Going Home(费用流入门)
    POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
  • 原文地址:https://www.cnblogs.com/Ph-one/p/6015691.html
Copyright © 2011-2022 走看看