K&R C Solutions
fduan
这个作者很懒,什么都没留下…
展开
-
K&R C Exercise 1-20 Solution
/* * Exercise 1-20 Write a program detab that replaces tabs in the input * with the proper number of blanks to space to the next tab stop. Assume * a fixed set of tab stops, say every n columns. *原创 2011-12-11 16:42:14 · 1279 阅读 · 0 评论 -
K&R C Exercise 2-6 Solution
/* * 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. * * fduan原创 2011-12-14 02:26:26 · 835 阅读 · 0 评论 -
K&R C Exercise 3-1 Solution
/* * Exercise 3-1 Write the binary search algorithm with only * one test inside the loop. * * Written by fduan on Dec. 15, 2011. */#include /* binary search: v[0] <= v[1] <= ... <= v[n-1] */原创 2011-12-15 03:00:01 · 1006 阅读 · 0 评论 -
K&R C Exercise 2-8 Solution
/* * 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. * * Written by fduan on Dec, 14, 2011. */#include /* th原创 2011-12-15 02:33:15 · 1119 阅读 · 0 评论 -
K&R C Exercise 2-7 Solution
/* * Exercise 2-7 Write a function invert(x, p, n) that returns * x with the n bits that begins at position p inverted, leaving * the other bits unchanged. * * fduan, Dec. 14, 2011. */#include原创 2011-12-15 02:03:39 · 772 阅读 · 0 评论 -
K&R C Exercise 2-4 Solution
/* * Exercise 2-4 Write an alternate version of squeeze( s1, s2 ) * that deletes each character in s1 that matches any character * in the string s2. * * fduan, Dec. 12, 2011. */#include void原创 2011-12-13 00:53:30 · 768 阅读 · 0 评论 -
K&R C Exercise 2-5 Solution
/* * 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 character from * s原创 2011-12-13 01:23:52 · 601 阅读 · 0 评论 -
K&R C Exercise 1-13 Solution
/* * Exercise 1-13 * print a histogram of the lengths of words in its input. It is easy to draw the * histogram with the bars horizontal; a vertical orientation is more challenging. * * fduan,原创 2011-12-07 21:00:35 · 623 阅读 · 0 评论 -
K&R C Exercise 1-22 Solution
/* * Exercise 1-22 Write a program to "fold" long input lines into * two or more shorter lines after the last non-blank character * that occurs before the n-th column of input. Make sure your *原创 2011-12-12 00:15:07 · 662 阅读 · 0 评论 -
K&R C Exercise 1-9 Solution
Exercise 1-9 Write a program to copy its input to output, replacing each string of one or more blanks by a single blank. 下面给出两种求解思路,第一种方法考虑输出字符的条件,不难看出,当当前字符为非空格时以及当前字符为空格,而上一个字符为非空格时,应当输出当前字符。为原创 2011-12-07 01:56:56 · 561 阅读 · 0 评论 -
K&R C Exercise 2-3 Solution
/* * Exercise 2-3 Write the function htoi(s), which converts a string * of hexadecimal digits into its equivalent integer value. */#include #include #define MAX_LEN 10int is_valid_hex_alpha(原创 2011-12-12 11:59:46 · 864 阅读 · 0 评论 -
K&R C Exercise 1-12 Solution
/* * Exercise 1-12 Write a program that prints its input one word per line. * * fduan, Dec. 07, 2011 */#include #define IN 1#define OUT 0int main(){ int c, state, nw; state = OUT; nw原创 2011-12-07 17:13:57 · 488 阅读 · 0 评论 -
K&R C Exercise 1-16 Solution
/* * Exercise 1-16 Revise the main routine of the longest line * program so it will correctly print the length of arbitrarily * long inputl lines, and as much as possible of the text. * * fduan原创 2011-12-10 21:27:14 · 724 阅读 · 0 评论 -
K&R C Exercise 1-17 Solution
/* * Exercise 1-17 Write a program to print all input lines that are * longer than 80 characters. * * fduan, Dec. 11, 2011 */#include #define MAX_LEN 1000#define LONG_LINE 80int getline(原创 2011-12-10 22:01:02 · 1094 阅读 · 0 评论 -
K&R C Exercise 1-19 Solution
/* * Exercise 1-19 Write a function reverse(s) that reverses * that character string s. Use it to write a program that * reverses its input a line at a time. * * fduan, Dec. 11, 2011 */#includ原创 2011-12-11 01:36:01 · 563 阅读 · 0 评论 -
K&R C Exercise 1-21 Solution
/* * Exercise 1-21 Write a program entab that replaces strings of blanks * by the minimum number of tabs and blanks to achieve the same spacing. * * fduan, Dec. 12, 2011 */#include #define TA原创 2011-12-11 21:38:42 · 665 阅读 · 0 评论 -
K&R C Exercise 1-18 Solution
/* * Exercise 1-18 Write a program to remove trailing blanks and tabs from * each line of input, and to delete the entirely blank lines. * * fduan, Dec. 11, 2011 */#include #define MAX_LEN 10原创 2011-12-11 01:35:21 · 730 阅读 · 0 评论 -
K&R C Exercise 2-9 Solution
/* * Exercise 2-9 In a two's complement number system, * x & ( x - 1 ) deletes the rightmost 1-bit in x. * Use this to write a fast version of bitcount. * * Written by fduan on Dec. 14, 2011.原创 2011-12-15 02:43:28 · 663 阅读 · 0 评论