「C2_Ushiku」の編集履歴(バックアップ)一覧はこちら

C2_Ushiku」(2009/12/05 (土) 03:06:59) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

- update test -- test (2009-11-23 20:22:38) #comment() &bold(){Exercise 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. #highlight(linenumber.c){{ #include<stdio.h> #include<limits.h> #include<float.h> int main(){ char a = 0; short c = 0; int e = 0; long g = 0L; int i; puts("<from limits.h>"); printf("char_MAX : %d, char_MIN : %d¥n",CHAR_MAX,CHAR_MIN); printf("unsigned_char_MAX : %u¥n", UCHAR_MAX); printf("int_MAX : %d, int_MIN : %d¥n",INT_MAX, INT_MIN); printf("unsgined_int_MAX : %u¥n", UINT_MAX); printf("long_MAX : %ld, long_MIN : %ld¥n", LONG_MAX, LONG_MIN); printf("unsigned_LONG_MAX : %lu¥n", ULONG_MAX); puts("<by computation>"); printf("char_SIZE %d bit¥n", (int)sizeof(a) * 8 ); printf("char_MAX : %d, char_MIN : %d¥n", a | 0x7F, ~(a | 0x7F)); printf("unsigned_char_MAX : %u¥n", a | 0xFF); printf("short_SIZE : %d bit¥n", (int)sizeof(c) * 8); printf("short_MAX : %d, short_MIN : %d¥n", c | 0x7FFF, ~(c | 0x7FFF)); printf("unsigned_short_MAX : %u¥n", c | 0xFFFF); printf("int_SIZE %d bit¥n", (int)sizeof(e) * 8 ); printf("int_MAX : %d, int_MIN : %d¥n", e | 0x7FFFFFFF, ~(e | 0x7FFFFFFF)); printf("unsgined_int_MAX : %u¥n", e | 0xFFFFFFFF); printf("long_SIZE : %d¥n", (int)sizeof(g) * 8); printf("long_MAX : %ld, long_MIN : %ld¥n", g | 0x7FFFFFFFFFFFFFFF, ~(g | 0x7FFFFFFFFFFFFFFF)); printf("unsignded_long_MAX : %lu¥n", g | 0xFFFFFFFFFFFFFFFF); printf("float_MAX : %e, float_MIN : %e¥n", FLT_MAX, FLT_MIN); } }} &bold(){Exercise 2-2.}Write a loop equivalent to the for loop above without using && or || . #highlight(linenumber.c){{ i = 0; while(1){ if(i >= lim-1){ if((c = (getchar() != '¥n'){ if(c != EOF){ s[i++] = c; }else break; }else break; }else break; } }} &bold(){Exercise 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 . #highlight(linenumber.c){{ #include<stdio.h> int htoi(char s[]){ int i; int result = 0; for(i = 2; s[i] != '¥0'; i++){ if(s[i] >= '0' || s[i] <= '9'){ result <<= 4; result += s[i] - 48; // printf("%d ¥n ", result); } else if(s[i] >= 'a' || s[i] <= 'f' || s[i] >= 'A' || s[i] <= 'F'){ result <<= 4; switch(s[i]){ case 'a' : case 'A' : result += 10; break; case 'b' : case 'B' : result += 11; break; case 'c' : case 'C' : result += 12; break; case 'd' : case 'D' : result += 13; break; case 'e' : case 'E' : result += 14; break; case 'f' : case 'F' : result += 15; break; } } } return result; } int main(int argc, char *argv[]){ if(argc <= 1){ puts("please input the hex-number with formatting 0x??, or 0X???"); exit(1); } if(argc != 2){ puts("too many argments"); exit(1); } if( argv[1][0] != '0' || (argv[1][1] != 'x' && argv[1][1] != 'X')){ puts("please input like follwing format. 0x??, or 0X??"); exit(1); } int result = htoi(argv[1]); printf("hex-num %s is converted to %d¥n", argv[1], result); } }} &bold(){Exercise 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 . #highlight(linenumber.c){{ #include<stdio.h> char *squeeze(char s1[], char s2[]){ int i,j,k,l; for(i = 0; s2[i] != '¥0'; i++){ k = 0; for(j = 0; s1[j] != '¥0'; j++){ if( s1[j] != s2[i]){ s1[k++] = s1[j]; } else{ for(l = j; s1[l] != '¥0'; l++){ s1[l] = s1[l+1]; } j--; } } } s1[k] = '¥0'; return s1; } int main(int argc, char *argv[]){ if(argc <= 1){ puts("please input like following format, ./a.out s1 s2 ,replace s1,s2 arbitrary"); exit(1); } if(argc >= 4){ puts("too many arguments"); exit(1); } printf("your input ¥n s1: %s¥n s2: %s¥n", argv[1], argv[2]); char *result = squeeze(argv[1], argv[2]); printf("removed string : %s ¥n", result); } }} &bold(){Exercise 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.) #highlight(linenumber.c){{ }} &bold(){Exercise 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. #highlight(linenumber.c){{ }} &bold(){Exercise 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. #highlight(linenumber.c){{ }} &bold(){Exercise 2-8.}Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions. #highlight(linenumber.c){{ }} &bold(){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 . #highlight(linenumber.c){{ }} &bold(){Exercise 2-10.}Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else . #highlight(linenumber.c){{ }}
- update test -- test (2009-11-23 20:22:38) #comment() &bold(){Exercise 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. #highlight(linenumber.c){{ #include<stdio.h> #include<limits.h> #include<float.h> int main(){ char a = 0; short c = 0; int e = 0; long g = 0L; int i; puts("<from limits.h>"); printf("char_MAX : %d, char_MIN : %d¥n",CHAR_MAX,CHAR_MIN); printf("unsigned_char_MAX : %u¥n", UCHAR_MAX); printf("int_MAX : %d, int_MIN : %d¥n",INT_MAX, INT_MIN); printf("unsgined_int_MAX : %u¥n", UINT_MAX); printf("long_MAX : %ld, long_MIN : %ld¥n", LONG_MAX, LONG_MIN); printf("unsigned_LONG_MAX : %lu¥n", ULONG_MAX); puts("<by computation>"); printf("char_SIZE %d bit¥n", (int)sizeof(a) * 8 ); printf("char_MAX : %d, char_MIN : %d¥n", a | 0x7F, ~(a | 0x7F)); printf("unsigned_char_MAX : %u¥n", a | 0xFF); printf("short_SIZE : %d bit¥n", (int)sizeof(c) * 8); printf("short_MAX : %d, short_MIN : %d¥n", c | 0x7FFF, ~(c | 0x7FFF)); printf("unsigned_short_MAX : %u¥n", c | 0xFFFF); printf("int_SIZE %d bit¥n", (int)sizeof(e) * 8 ); printf("int_MAX : %d, int_MIN : %d¥n", e | 0x7FFFFFFF, ~(e | 0x7FFFFFFF)); printf("unsgined_int_MAX : %u¥n", e | 0xFFFFFFFF); printf("long_SIZE : %d¥n", (int)sizeof(g) * 8); printf("long_MAX : %ld, long_MIN : %ld¥n", g | 0x7FFFFFFFFFFFFFFF, ~(g | 0x7FFFFFFFFFFFFFFF)); printf("unsignded_long_MAX : %lu¥n", g | 0xFFFFFFFFFFFFFFFF); printf("float_MAX : %e, float_MIN : %e¥n", FLT_MAX, FLT_MIN); } }} &bold(){Exercise 2-2.}Write a loop equivalent to the for loop above without using && or || . #highlight(linenumber.c){{ i = 0; while(1){ if(i >= lim-1){ if((c = (getchar() != '¥n'){ if(c != EOF){ s[i++] = c; }else break; }else break; }else break; } }} &bold(){Exercise 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 . #highlight(linenumber.c){{ #include<stdio.h> int htoi(char s[]){ int i; int result = 0; for(i = 2; s[i] != '¥0'; i++){ if(s[i] >= '0' || s[i] <= '9'){ result <<= 4; result += s[i] - 48; // printf("%d ¥n ", result); } else if(s[i] >= 'a' || s[i] <= 'f' || s[i] >= 'A' || s[i] <= 'F'){ result <<= 4; switch(s[i]){ case 'a' : case 'A' : result += 10; break; case 'b' : case 'B' : result += 11; break; case 'c' : case 'C' : result += 12; break; case 'd' : case 'D' : result += 13; break; case 'e' : case 'E' : result += 14; break; case 'f' : case 'F' : result += 15; break; } } } return result; } int main(int argc, char *argv[]){ if(argc <= 1){ puts("please input the hex-number with formatting 0x??, or 0X???"); exit(1); } if(argc != 2){ puts("too many argments"); exit(1); } if( argv[1][0] != '0' || (argv[1][1] != 'x' && argv[1][1] != 'X')){ puts("please input like follwing format. 0x??, or 0X??"); exit(1); } int result = htoi(argv[1]); printf("hex-num %s is converted to %d¥n", argv[1], result); } }} &bold(){Exercise 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 . #highlight(linenumber.c){{ #include<stdio.h> char *squeeze(char s1[], char s2[]){ int i,j,k,l; for(i = 0; s2[i] != '¥0'; i++){ k = 0; for(j = 0; s1[j] != '¥0'; j++){ if( s1[j] != s2[i]){ s1[k++] = s1[j]; } else{ for(l = j; s1[l] != '¥0'; l++){ s1[l] = s1[l+1]; } j--; } } } s1[k] = '¥0'; return s1; } int main(int argc, char *argv[]){ if(argc <= 1){ puts("please input like following format, ./a.out s1 s2 ,replace s1,s2 arbitrary"); exit(1); } if(argc >= 4){ puts("too many arguments"); exit(1); } printf("your input ¥n s1: %s¥n s2: %s¥n", argv[1], argv[2]); char *result = squeeze(argv[1], argv[2]); printf("removed string : %s ¥n", result); } }} &bold(){Exercise 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.) #highlight(linenumber.c){{ #include<stdio.h> #define MAX_CHAR 10000 int any(char s1[], char s2[]){ int candidates[MAX_CHAR] = {0}; int i,j,k = 0,l; int first_check = 1; for(i = 0; s2[i] != '¥0' ; i++){ if(first_check){ for(j = 0; s1[j] != '¥0'; j++){ if(s1[j] == s2[i]) candidates[k++] = j; } first_check--; candidates[k] = -1; } else for(j = 0; candidates[j] != -1; j++){ if(s1[candidates[j]+i] != s2[i]){ for(l = j; candidates[l] != -1; l++){ candidates[l] = candidates[l+1]; } j--; } } } return candidates[0]+1; } int main(int argc, char *argv[]){ int result; if(argc <= 1){ puts("please input like following format, ./a.out s1 s2 ,replace s1,s2 arbitrary"); exit(1); } if(argc != 3){ puts("please input properly"); exit(1); } printf("your input ¥n s1: %s¥n s2: %s¥n", argv[1], argv[2]); if(result = any(argv[1], argv[2])) printf("the placee of the word you chosed : %d ¥n", result); else puts("-1"); } }} &bold(){Exercise 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. #highlight(linenumber.c){{ }} &bold(){Exercise 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. #highlight(linenumber.c){{ }} &bold(){Exercise 2-8.}Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions. #highlight(linenumber.c){{ }} &bold(){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 . #highlight(linenumber.c){{ }} &bold(){Exercise 2-10.}Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else . #highlight(linenumber.c){{ }}

表示オプション

横に並べて表示:
変化行の前後のみ表示: