20101109

[C] Ex3-4

The C Programming Language Exercise 3-4
題目:
In a two's complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize - 1)) . Explain why not. Modify it to print that value correctly regardless of the machine on which it runs.

解法:
也是有趣題!先把最小值 + 1,即可處理 XD。只附上itoa()。

[C] Ex3-3

The C Programming Language Exercise 3-3
題目:
Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z . Arrange that a leading or trailing - is taken literally.

解法:
覺得還滿有趣的一題。判斷「-」是否為字串的第一或最後一個字元,再處理字母與數字問題即可。
A-A類的也要稍微判斷。

20101030

[C] Ex1-23

題目:
(The C Programming Language)
移除C code裡的comments。
解法:
分別處理(1) escape sequence (2) one line comments (3) multiline comments即可。
卡在對&&觀念不熟,囧,邏輯課重修……

20101029

[C] Ex1-20

The C Programming Language
題目:
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. Should n be a variable or a symbolic parameter?
解法:
一開始看不懂題目在寫甚麼……原來只是要我們用C模擬tab的作用方式。
找到tab stop的規則即可輕鬆解決。
tab stop會出現在n + 1的位置。(假設ts = n)

[C_Programming]Ex1-16

題目:
Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.

解法:
一直卡在當overflow時,i會自動再run一次getline(),我不知道為什麼,所以無法直接測出該行字串的長度(如果溢位的話)。於是用另一個j來測量長度,i只做getline的動作。

The C Programming Language

這本書算是C的聖經書,言簡意賅,想了解C,讀這本書就對了。

把一些比較奇怪,或是說讓我覺得很Orz的題目po上來,供大家參考。

2011/09/13 update: code不會特別附上,避免有人copy……話說我寫這麼爛應該也沒人要copy lol。

2011/10/12 update: 後來想想code放上來也沒差,反正網路有一堆人寫的code。