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類的也要稍微判斷。