这一章习题做着很舒服,毕竟很简单。所以很有感觉。
练习 2-1
Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.
1 #include <stdio.h> 2 #include <limits.h> 3 int main() 4 { 5 printf("char max : %d min : %d\n",CHAR_MAX, CHAR_MIN); 6 printf("unchar max : %u min : %u\n",UCHAR_MAX, 0); 7 printf("int max : %d min : %d\n",INT_MAX, INT_MIN); 8 printf("unsigned int max : %lu min : %d\n",UINT_MAX, 0); 9 printf("long max : %ld min : %ld\n",LONG_MAX, LONG_MIN); 10 printf("unsigned long max : %lu min : %d \n",ULONG_MAX ,0); 11 12 return 0; 13 }
ps :假如unsigned int 最大值不用 %lu 而用 %lld 会导致 后面变量发生无法预知的错误, %ld 无法装下unsigned int 最大值
练习 2-2
Exercise 2-2 discusses a for loop from the text. Here it is:
1 int main(void) 2 { 3 /* 4 for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i) 5 s[i] = c; 6 */ 7 8 int i = 0, 9 lim = MAX_STRING_LENGTH, 10 int c; 11 char s[MAX_STRING_LENGTH]; 12 13 while (i < (lim - 1)) 14 { 15 c = getchar(); 16 17 if (c == EOF) 18 break; 19 else if (c == '\n') 20 break; 21 22 s[i++] = c; 23 } 24 25 s[i] = '\0'; /* terminate the string */ 26 27 return 0; 28 }
练习 2-3
Write the function htoi(s) , which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9,a through f, and A through F .
1 #include <stdio.h> 2 char A[100]; 3 4 int main(int argc, char const *argv[]) 5 { 6 scanf("%s", A); 7 int v = _atoi(A); 8 printf("%d\n", v); 9 return 0; 10 } 11 12 int _atoi(char s[]){ 13 int i, n, hex; 14 n = 0; 15 for(i=0;s[i]>='0' && s[i]<='9' || s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i] <= 'Z'; ++i){ 16 if(s[i]>='0' && s[i]<='9') 17 hex = s[i] - '0'; 18 if(s[i]>='a' && s[i]<='z') 19 hex = s[i] - 'a' + 10; 20 if(s[i]>='A' && s[i]<='Z') 21 hex = s[i] - 'A' + 10; 22 n = 16 * n + hex; 23 } 24 return n; 25 }
练习 2-4
Write an alternate version of squeeze(s1,s2) that deletes each character in the string s1 that matches any character in the string s2
1 #include <stdio.h> 2 #include <string.h> 3 char a[100]; 4 char b[100]; 5 6 int lenth(char b[]); 7 void strcat_1(char s[], char t[]); 8 9 int main() { 10 scanf("%s", a); 11 scanf("%s", b); 12 strcat_1(a ,b); 13 printf("%s", a); 14 } 15 16 void strcat_1(char s[], char t[]){ 17 int i, j, k, l, tlen; 18 tlen = lenth(t); 19 for(i=0;s[i] != '\0';i++){ 20 k=0; 21 if(s[i] != t[k]) 22 continue; 23 l = i; 24 while(s[l++] == t[k++]) { 25 if(t[k]=='\0') 26 break; 27 } 28 if(t[k] == '\0'){ 29 while(s[l-tlen]!='\0'){ 30 s[l-tlen] = s[l]; 31 l++; 32 } 33 i+=tlen; 34 } 35 } 36 } 37 38 int lenth(char b[]){ 39 int i; 40 for(i=0;b[i]!='\0';++i); 41 return i; 42 }
Friend 's version
#include <stdio.h> void squeeze(char s[], char t[]) { int i, j, k; k = 0; for(i = 0; s[i] != '\0'; ++i) { for(j = 0; t[j] != '\0'; ++j) { if(s[i] == t[j]) break; } if(t[j] == '\0') s[k++] = s[i]; } s[k] = '\0'; } int main() { char s[] = "You are e a pig!!!"; char t[] = "Not me"; squeeze(s, t); printf("%s\n", s); return 0; }
练习 2-5
Write the function any(s1,s2) , which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no characters from s2 . (The standard library function strpbrk does the same job but returns a pointer to the location.)
1 #include <stdio.h> 2 #include <string.h> 3 char s1[100]; 4 char s2[100]; 5 6 int any(char s1[], char s2[]){ 7 int i, j, len1, len2; 8 len1 = strlen(s1); 9 len2 = strlen(s2); 10 for(i=0;i<len1;++i){ 11 for(j=0;j<len2;++j) 12 if(s1[i] == s2[j]) 13 return i+1; 14 15 } 16 return -1; 17 } 18 19 20 int main(int argc, char const *argv[]) 21 { 22 int t; 23 scanf("%s", s1); 24 scanf("%s", s2); 25 t = any(s1, s2); 26 printf("%d\n", t); 27 return 0; 28 }
练习 2-6
Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.
1 #include <stdio.h> 2 unsigned getbits(unsigned x, int p, int n); 3 unsigned setbits(int x, int p, int n, int y); 4 int main(int argc, char const *argv[]) 5 { 6 unsigned a, y, result; 7 int b, c, count; 8 scanf("%d %d %d %d", &a, &b, &c, &y); 9 //result = getbits(a, b, c); 10 result = setbits(a, b, c, y); 11 printf("The result is : %d\n", result); 12 return 0; 13 } 14 15 unsigned setbits(unsigned x, int p, int n, int y){ 16 return ( x & ~(~(~0 << n) << (p+1-n))) | ( (y & ~(~0 << n)) << (p+1-n) ) ; 17 } 18 19 unsigned getbits(unsigned x, int p, int n){ 20 return (x >> (p+1-n)) & ~(~0 << n); 21 }
练习 2-7
Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa), leaving the others unchanged.
1 #include <stdio.h> 2 unsigned invert(unsigned x, int p, int n); 3 int main(int argc, char const *argv[]) 4 { 5 unsigned a, result; 6 int b, c, count; 7 scanf("%d %d %d", &a, &b, &c); 8 result = invert(a, b, c); 9 printf("The result is : %d\n", result); 10 11 12 return 0; 13 } 14 15 unsigned invert(unsigned x, int p, int n){ 16 return (x & ~(~(~0 << n) << (p+1-n))) | (x >>(p+1-n) ^ ~(~0 << n)) << (p+1-n) ; 17 }
练习 2-8
Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.
递归法:
1 #include <stdio.h> 2 3 void show(unsigned x, int step) { 4 if(step < 31) 5 show(x >> 1, step + 1); 6 printf("%d", x&1); 7 } 8 9 unsigned rightrot(unsigned x, int n) { 10 return (x<<(32-n))|(x>>n); 11 } 12 13 int main() { 14 unsigned x = 129923235; 15 show(x, 0); 16 printf("\n"); 17 show(rightrot(x, 11), 0); 18 printf("\n"); 19 return 0; 20 }
练习 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 .
1 #include <stdio.h> 2 3 int bitcount(unsigned x) { 4 int b; 5 6 for(b = 0; x != 0; x >>= 1) 7 if(x & 01) 8 b++; 9 return b; 10 } 11 12 int bitcount2(unsigned x) { 13 int b; 14 15 for(b = 0; x != 0; x &= (x-1)) 16 b++; 17 return b; 18 } 19 20 int main() { 21 printf("%d, %d\n", bitcount(13333), bitcount2(13333)); 22 printf("%d, %d\n", bitcount(11111111), bitcount2(11111111)); 23 return 0; 24 }
练习 2-10
Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .
1 #include <stdio.h> 2 char A[100]; 3 int main(int argc, char const *argv[]) 4 { 5 int i; 6 scanf("%s", A); 7 for(i=0;A[i]!=0;++i) 8 A[i] = higher(A[i]); 9 printf("%s\n", A); 10 return 0; 11 } 12 13 int lower(int c){ 14 return c >= 'A' && c <='Z' ? c + 'a' - 'A' : c; 15 } 16 17 int higher(int c){ 18 return c >= 'a' && c <='z' ? c + 'A' - 'a' : c; 19 }