zoukankan      html  css  js  c++  java
  • 《K&R》里贯穿全书的代码

    1、getline(char s[], int lim)

    调用结果:往参数数组中读入字符直到换行(即:读取一行)

    返回值:参数数组所读入的字符串长度

    在《c程序设计语言中》里使用频率特别高。

    version 1:

    int getline(char s[], int lim)
    {
        int c, i;
    
        for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '
    '; ++ i)
            s[i] = c;
        if(c == '
    '){
            s[i] = c;
            ++ i;
        }
        s[i] = '';
        return i;
    }

     version 2:

    int getline(char s[], int lim)
    {
        int c, i;
        
        i = 0;
        while(--lim > 0 && (c=getchar()) != EOF && c != '
    ')
            s[i++] = c;
        if(c == '
    ')  // 这里非常重要!
            s[i++] = c;
        s[i] = '';
        return i;
    }

    2、atoi(char s[])

    返回值:整型数

    将字符串s转换为相应的整数类型

    version 1:

    int atoi(char s[])
    {
        int i, n;
    
        n = 0;
        for(i = 0; s[i] >= '0' && s[i] <= '9'; ++ i)
            n = 10 * n + (s[i] - '0');
        return n;
    }

     version 2:

    #include<ctype.h>
    
    int atoi(char s[])
    {
        int i, n, sign;
        
        for(i = 0; isspace(s[i]); i ++)
            ;
        sign = (s[i] == '-')? -1 : 1;
        if(s[i] == '+' || s[i] == '-')
            i ++;
        for(n = 0; isdigit(s[i]); i ++)
            n = 10 * n + (s[i] - '0');
        return sign * n;
    }

    3、binsearch(int x, int v[],  int n)

    返回值:元素x在数组v[]中的下标(v[]数组内元素是升序的)

    #include<stdio.h>
    #define MAXLINE 7
    
    int binsearch(int x, int v[], int n);
    // unit test
    main()
    {
        int a[MAXLINE] = {1, 3, 5, 7, 8, 10, 19};
        
        if(binsearch(5, a, MAXLINE) == 2 && binsearch(8, a, MAXLINE) == 4)
            printf("binsearch: pass
    ");
        else
            printf("binsearch: error
    ");
    }
    
    int binsearch(int x, int v[], int n)
    {
        int low, high, mid;
        
        low = 0;
        high = n - 1;
        while(low <= high){
            mid = (low + high) / 2;
            if(x < v[mid])
                high = mid - 1;
            else if(x > v[mid])
                low = mid + 1;
            else 
                return mid; // found match
        }
        return -1;    // no match
    }

    4、reverse(char s[])

    倒置字符中s中各个字符的位置

    #include<string.h>
    
    void reverse(char s[])
    {
        int c, i, j;
    
        for(i = 0, j = strlen(s)-1; i < j; i++, j--){
            c = s[i];
            s[i] = s[j];
            s[j] = c;
        }
    }

    这段代码只是比较典型。

    5、itoa(int n, char s[])

    将数字n转换为字符串并保存到s中

    void itoa(int n, char s[])
    {
        int i, sign;
        
        if((sign = n) < 0)
            n = -n;
        i = 0;
        do{
            s[i++] = n % 10 + '0';
        }while((n /= 10) > 0);
        if(sign < 0)
            s[i++] = '-';
        s[i] = '';
        reverse(s);
    }

    代码简洁,逻辑清晰。

    6、atof(char s[])

    把字符串s转换为相对应的双精度浮点数

    #include<ctype.h>
    
    double atof(char s[])
    {
        double val, power;
        int i, sign;
        
        for(i = 0; isspace(s[i]); i++)
            ;
        sign = (s[i] == '-')? -1 : 1;
        if(s[i] == '+' || s[i] == '-')
            i++;
        for(val = 0.0; isdigit(s[i]); i++)
            val = 10.0 * val + (s[i] - '0');
        if(s[i] == '.')
            i++;
        for(power = 1.0; isdigit(s[i]); i++){
            val = 10.0 * val + (s[i] - '0');
            power *= 10.0;
        }
        return sign * val / power;
    }

    和atoi不同的是小数部分的处理

  • 相关阅读:
    SpringMVC 2
    MySQL--事务,隔离性和隔离级别
    String.intern()
    初识消息队列--ActiveMq
    Java后台上传图片到七牛云
    Thread.interrupt(),Thread.isInterrupted(),Thread.interrupted()碎碎念
    Java基础--对象
    Java后台调用gcc编译C语言代码
    ToolProvider.getSystemJavaCompiler()方法空指针的排坑
    [LeetCode]29 两数相除和一个小坑点
  • 原文地址:https://www.cnblogs.com/xkxf/p/6208343.html
Copyright © 2011-2022 走看看