zoukankan      html  css  js  c++  java
  • c语言 11-10

    1、字符串转换为int型

    #include <stdio.h>
    
    int toint(char *s1)
    {
        int i, j = 0;
        while(*s1)
        {
            for(i = 0; i <= 9; i++)
            {
                if((*s1 - '0') == i)
                    j = j * 10 + i;
            }
            s1++;
        }
        return j;
    }
    
    int main(void)
    {
        char str1[128];
        printf("str1: "); scanf("%s", str1);
        
        printf("result converted to int type: %d
    ", toint(str1));
        return 0;
    }

    2、转换为long型

    #include <stdio.h>
    
    long tolong(char *s1)
    {
        int i;
        long j = 0;
        while(*s1)
        {
            for(i = 0; i <= 9; i++)
            {
                if((*s1 - '0') == i)
                    j = j * 10 + (long)i;
            }
            s1++;
        }
        return j;
    }
    
    int main(void)
    {
        char str1[128];
        printf("str1: "); scanf("%s", str1);
        
        printf("result converted to long type: %ld
    ", tolong(str1));
        return 0;
    }

    3、转换为double型

    #include <stdio.h>
    
    double todouble(char *s1)
    {
        int i;
        double j = 0.0;
        while(*s1)
        {
            for(i = 0; i <= 9; i++)
            {
                if((*s1 - '0') == i)
                    j = j * 10 + (double)i;
            }
            s1++;
        }
        return j;
    }
    
    int main(void)
    {
        char str1[128];
        printf("str1: "); scanf("%s", str1);
        
        printf("result converted to double type: %f
    ", todouble(str1));
        return 0;
    }

  • 相关阅读:
    HDU 5135(再思考)
    HDU 5105
    HDU 5135
    Codeforces 985E
    Codeforces 985D
    Codeforces 975D
    Codeforces 975C
    Codeforces 976D
    HDU 1024 Max Sum Plus Plus (DP,水题)
    HDU 1003 Max Sum(DP,水题)
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14844698.html
Copyright © 2011-2022 走看看