好文档 - 专业文书写作范文服务资料分享网站

C程序设计语言 (第二版) 课后答案第二章

天下 分享 时间: 加入收藏 我要投稿 点赞

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. #include #include int main () {

printf(\

printf(\ printf(\ printf(\ printf(\

printf(\ printf(\ printf(\ printf(\

printf(\ printf(\ printf(\ printf(\ return 0; }

Exercise 2-2

Write a loop equivalent to the for loop above without using && or || . #include #define lim 100 void main() {

int i=0, c; char s[lim]; while(i

c=getchar(); if(c='\\n') break; if(c= EOF) break; ++i; s[i]=c;

} }

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 . #define YES 1 #define NO 0 int htoi(s[]) {

int a ,I ,b ,n ; if(s[i]=='0') { ++i; if(s[i]=='x'||s[i]=='X') { ++i; } } n=0; b=YES;

if( ;b==YES;++i) {

if(s[i]>='0'&&s[i]<='9') a=s[i]-'0';

else if(s[i]>='a'&&s[i]<='z') a=s[i]-'a'+10;

else if(s[i]>='A'&&s[i]<='Z') a=s[i]-'A'+10; else

b=NO; if (b==YES) n=16*n+a; } return n; }

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 . void squeeze2(char s1[], char s2[]) {

int i, j, k;

int instr2 = 0;

for(i = j = 0; s1[i] != '\\0'; i++) {

instr2 = 0;

for(k = 0; s2[k] != '\\0' && !instr2; k++) {

if(s2[k] == s1[i]) {

instr2 = 1; } }

if(!instr2) {

s1[j++] = s1[i]; } }

s1[j] = '\\0'; }

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.) int any(char s1[], char s2[]) {

int i; int j; int pos;

pos = -1;

for(i = 0; pos == -1 && s1[i] != '\\0'; i++) {

for(j = 0; pos == -1 && s2[j] != '\\0'; j++) {

if(s2[j] == s1[i]) {

pos = i; } } }

return pos;

}

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. #include

unsigned setbits(unsigned x, int p, int n, unsigned y) {

return (x & ((~0 << (p + 1)) | (~(~0 << (p + 1 - n))))) | ((y & ~(~0 << n)) << (p + 1 - n)); }

int main(void) {

unsigned i; unsigned j; unsigned k; int p; int n;

for(i = 0; i < 30000; i += 511) {

for(j = 0; j < 1000; j += 37) {

for(p = 0; p < 16; p++) {

for(n = 1; n <= p + 1; n++) {

k = setbits(i, p, n, j);

printf(\ } } } }

return 0; }

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. #include int main(void) {

unsigned x; int p, n;

for(x = 0; x < 700; x += 49)

for(n = 1; n < 8; n++) for(p = 1; p < 8; p++)

printf(\ return 0; }

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. #include int main(void) {

unsigned x; int n;

for(x = 0; x < 700; x += 49) for(n = 1; n < 8; n++)

printf(\ return 0; }

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 . int bitcount(unsigned x) {

int b;

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

Exercise 2-10

Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else . #include #include #define TEST

#define ORIGINAL 0 #define SOLUTION 1 #define PORTABLE_SOLUTION 0

#if ORIGINAL int lower(int c) {

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

return c; }

#endif

C程序设计语言 (第二版) 课后答案第二章

Exercise2-1Writeaprogramtodeterminetherangesofchar,short,int,andlongvariables,bothsignedandunsigned,byprintingappropriatevaluesfromstandardheadersandb
推荐度:
点击下载文档文档为doc格式
6m5bi27nli0fvam2gyzr6h1tx45d76007p2
领取福利

微信扫码领取福利

微信扫码分享