zoukankan      html  css  js  c++  java
  • C程序设计语言练习 第二章

    2.3 常量

    strlen函数:返回s的长度

    int strlenn(char s[])
    {
        int i=0;
        while(s[i] != '')
            ++i;
        return i;
    }
    

    2.7 类型转换

    atoi函数:将字符串s转换为相应的整型

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

    lower函数:把字符c转换为小写形式,只对ASCII字符集有效

    int lower(int c)
    {
        if(c >= 'A' && c <= 'Z')
            return c + 'a' -'A';
        else
            return c;    
    }
    

    rand函数:返回取值在0~32767之间的伪随机数,(2^{15}) = 32768
    srand函数:为rand()函数设置种子数

    unsigned long  int next = 1;
    
    int rand()
    {
        next = next*1103515245 + 12345;
        return (unsigned int)(next/65536)%32768;
    }
    
    void srand(unsigned int seed)
    {
        next = seed;
    }
    

    练习2-3

    编写函数htoi(s), 把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值。字符串中允许包含数字包括:09,af以及A~F。

    int htoi(char s[])
    {
        int n = 0,i = 0;
        if(s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) i = 2;
    
        for(; s[i] != ''; ++i) {
            if(s[i] >= '0' && s[i] <= '9')
                n = 16*n + (s[i] - '0');
            else if(s[i] >= 'a' && s[i] <= 'f')
                n = 16*n + (s[i] - 'a' + 10);
            else if(s[i] >= 'A' && s[i] <= 'F')
                n = 16*n + (s[i] - 'A' + 10);
            else break;
        }
        return n;
    }
    

    2.8 自增运算符与自减运算符

    squeeze函数:从字符串s中删除字符

    void squeeze(char s[], char c)
    {
        int i,j;
        for(i=j=0; s[i] != ''; ++i)
            if(s[i] != c) //当不相等时才赋值,否则忽略
                s[j++] = s[i];
        s[j] = '';
    }
    

    strcat函数:将字符串t连接到字符串s的尾部;s必须有足够大的空间

    void strcatt(char s[], char t[])
    {
        int i=0,j=0;
        while(s[i] != '')
            ++i;
        while((s[i++] = t[j++]) != '')
            ;
    }
    

    练习2-4

    重写函数squeeze(s1,s2),将字符串s1中任何与字符串s2中字符匹配的字符都删除。

    void squeeze2(char s1[], char s2[])
    {
        int i,j,z;
        for(i=j=0; s1[i] != ''; ++i) {
            for(z=0; s2[z] != ''&& s1[i] != s2[z]; ++z)
                ;
            if(s2[z] == '') s1[j++] = s1[i]; //s2没有与之相等的字符
        }
        s1[j] = '';
    }
    

    练习2-5

    编写函数any(s1,s2),将字符s2中的任一字符在字符串s1中的第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。(标准库函数strpbrk具有同样的功能,但它返回的是指向该位置的指针。)

    int any(char s1[], char s2[])
    {
        int i,j;
    
        for(i=0; s1[i] != ''; ++i) {
            for(j=0; s2[j] != '' && s1[i] != s2[j]; ++j)
                ;
            if(s2[j] != '') return i; //发现s2有相等字符
        }
        return -1;
    }
    

    2.9 按位运算符

    getbits 函数:返回x中第p位开始的n位, 如getbits(x,4,3) 返回4,3,2位

    unsigned getbits(unsigned x, int p, int n)
    {
        return (x >> (p-n+1)) & ~(~0 << n);
    }
    

    2.10 赋值运算符与表达式

    bitcount函数:统计x中值位1的二进制位数

    int bitcount(unsigned x)
    {
        int b;
        for(b=0; x!= 0; x >>= 1)
            if(x & 01)
                ++b;
        return b;
    }
    

    练习2-9

    在求对2的补码时,表达式x &= (x-1)可以删除x中最右边值为1的一个二进制位。请解释这样做的道理。用这一方法重写bitcount函数,以加快其执行速度

    int bitcount(unsigned x)
    {
        int b;
        for(b=0; x!= 0; x &= (x-1))
            ++b;
        return b;
    }
    

    练习 2-10

    重新编写将大写字母转换为小写字母的函数lower,并用条件表达式替代其中的if-else结构

    int lower(int c)
    {
        return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;   
    }
    
  • 相关阅读:
    1.9
    在VS中添加lib库的三种方法
    第一章之位向量和位运算
    【转载】window下配置pthread的方法及出现问题的解决方法
    opencv环境配置问题
    box-shadow用法简介
    创建资源文件
    nhibernate Mybatisnet
    js中(function(){…})()立即执行函数写法理解
    最近项目中用到的js
  • 原文地址:https://www.cnblogs.com/wjundong/p/11563256.html
Copyright © 2011-2022 走看看