再读《The C Programming Language》 第二章 2.4 练习汇总

来源:互联网 发布:网吧游戏菜单软件 编辑:程序博客网 时间:2024/05/22 16:50
2.4 练习题汇总

Exercise 2-2.Write a loop equivalent to the forloop above without using &&or ||.

#include <stdio.h>      /* 系统头文件 */#include <stdlib.h>     /* 系统头文件 */#define MAXLINE               1000char line[MAXLINE] = {0};int getline(char str[], int lim);int getline_copy(char str[], int lim);int main(){        int len = 0;        len = getline(line, MAXLINE);        printf("len = %d,%s", len, line);        len = getline_copy(line,MAXLINE);        printf("len = %d,%s", len, line);     system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */     return 0;}int getline(char str[], int lim){     int i = 0;     char c;     for(i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; i++){          str[i] = c;     }     if(c = '\n'){          str[i] = '\n';          i++;     }     return i;}int getline_copy(char str[], int lim){     int i = 0;     char c;     while(i < lim){          c = getchar();          if( c == EOF){               return i;          }else if( c == '\n'){                        str[i] = '\n';               i++;               return i;          }else{               i++;          }     }}


Exercise 2-3.Write a function htoi(s), which converts a string of hexadecimal digits (including an optional 0xor 0X) into its equivalent integer value. The allowable digits are 0through 9, athrough f, and Athrough F. 

#include <stdio.h>      /* 系统头文件 */#include <stdlib.h>     /* 系统头文件 */#include <ctype.h>#define MAXLINE                 1000char line[MAXLINE] = {0};int getline(char str[], int lim);int atoi(char str[]);int htoi(char str[]);int main(){     int len;     len = getline(line,MAXLINE);     //printf("num = %d\n", atoi(line));     printf("num = %d\n", htoi(line));     system("PAUSE"); /* 系统暂停运行,可以方便看到显示结果 */     return 0;}int getline(char str[], int lim){     int i = 0;     char c;     for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; i++){          str[i] = c;     }     if (c = '\n'){          str[i] = '\n';          i++;     }     return i;}int atoi(char str[]){     int i, n;     n = 0;     for (i = 0; isdigit(str[i]); i++){         n = 10 * n + str[i] - '0';     }     return n;}int htoi(char str[]){     int i, c, tmp;         int n = 0;     if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))          for (i = 2; isxdigit(c = str[i]); i++){               if (isdigit(c))                    tmp = c - '0';               else if (isupper(c))                    tmp = c - 'A';               else                    tmp = c - 'a';               n = 16 * n + tmp;          }     return n;}

Exercise 2-4.Write an alternative version of squeeze(s1,s2)that deletes each character in s1 that matches any character in the string s2. 

Exercise 2-5.Write the function any(s1,s2), which returns the first location in a string s1where any character from the string s2occurs, or -1if s1contains no characters from s2. (The standard library function strpbrkdoes the same job but returns a pointer to the location.) 

#include <stdio.h>#include <stdlib.h>char s1[] = "abcdefghijkllmn";char s2[] = "";char s3[] = "a";char s4[] = "b";char s5[] = "  c";char s6[] = "ppooij";void squeeze(char s1[], char s2[]);int any(char s1[], char s2[]);int main(){     printf("i = %d\n",any(s1,s2));     printf("i = %d\n",any(s1,s3));     printf("i = %d\n",any(s1,s4));     printf("i = %d\n",any(s1,s5));     printf("i = %d\n",any(s1,s6));     squeeze(s1, s2);     printf("%s\n",s1);     squeeze(s1, s3);     printf("%s\n",s1);     squeeze(s1, s4);     printf("%s\n",s1);     squeeze(s1, s5);     printf("%s\n",s1);     squeeze(s1, s6);     printf("%s\n",s1);     system("PAUSE");}/* if found any char in s1 ,than return the index num */int any(char s1[], char s2[]){     int i,j;     for (i = 0; s1[i] != '\0'; i++){          for (j = 0; s2[j] != '\0'; j++){               if ( s1[i] == s2[j])                    return i;          }     }     return -1;}void squeeze(char s1[], char s2[]){     int i,j;     int k;     for (i = 0; s2[i] != '\0'; i++){          for (j = 0, k = 0; s1[j] != '\0'; j++){               if (s2[i] != s1[j])                    s1[k++] = s1[j];          }          s1[k] = '\0';     }}



Exercise 2-6.Write a function setbits(x,p,n,y)that returns xwith the nbits that begin at position pset to the rightmost n bits of y, leaving the other bits unchanged. 

Exercise 2-7.Write a function invert(x,p,n)that returns xwith the nbits that begin at position pinverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged. 

Exercise 2-8.Write a function rightrot(x,n)that returns the value of the integer xrotated to the right by npositions. 

#include <stdio.h>#include <stdlib.h>int setbits(int x, int p, int n, int y);int invert(int x, int p, int n);int rightrot(int x, int n);int bitcount(unsigned x);int bitcountv2(unsigned x);int main(){     int a,b,c,d;     a = 0xFFFFFF0F;     b = 3;     c = 12;     d = 0;     printf("x = %x\n",setbits(a,b,c,d));     printf("x invert is %x\n", invert(a,b,c));     printf("rightrot is %x \n", rightrot(a,b));     printf(" b= %d,%d\n",bitcount(a),bitcountv2(a));     system("PAUSE");     return 0;}int setbits(int x, int p, int n, int y){     int i;     for (i = p; i < n; i++){          if (y & 1)               x |= (1<<i);     /* set 1 */          else               x &= ~(1<<i);     /* clear 0 */     }     return x;}int invert(int x, int p, int n){     int i;     for (i = p; i < n; i++){          x ^= (1<<i);          /* invert */     }     return x;}int rightrot(int x, int n){     int tmp = 0;     int i;     for (i = 0; i < n; i++){          tmp = x & 1;          x >>= 1;          if (tmp)               x |= (1<<32);     }     return x;}int bitcount(unsigned x){     int b;     for (b = 0; x != 0; x >>= 1)          if (x & 1)               b++;     return b;}int bitcountv2(unsigned x){     int b = 0;     while(x){          b++;          x &= (x-1);     }     return b;}



Exercise 2-9.In a two’s complement number system, x &= (x-1)deletes the rightmost 1-bit in x. Explain why. Use this observation to write a faster version of bitcount. 


#include <stdio.h>#include <stdlib.h>int bitcount(unsigned int x);int bitcountv2(unsigned int x);int main(){int tmp = 0xff00ff00;printf("%d,%d\n",bitcount(tmp), bitcountv2(tmp));system("PAUSE");return 0;}int bitcount(unsigned int x){int b;for (b = 0; x != 0; x >>= 1)if (x & 1)b++;return b;}int bitcountv2(unsigned int x){int b = 0;while (x){b++;x &= (x-1);}return b;}



原创粉丝点击