2.10 程序13:问题程序
下面这个程序为什么不能正确结账?另外,除了本章要展示的的问题之外,这个程序还有一个错误。这个错误在哪里?
1 /************************************************
2 * Billing -- Print out how much we owe ?*
3 * customers or they owe us. ?? *
4 ************************************************/
5 #include <iostream>
6
7 // Number of pennies in a dollar
8 const int DOLLAR = 100;
9
10 /***********************************************
11 * billing -- do the billing. ? *
12 * If the customer owes us money ? ? *
13 * -- output debt. ? *
14 * If we owe more than $100 ? *
15 * -- output credit. ? *
16 * Between $0 and $100 just ignore the ?? *
17 * account. ?? *
18 ***********************************************/
19 int billing(
20 // Current balance (in cents)
21 const int balance
22 ) {
23 if (balance < 0)
24 if (balance < - (100*DOLLAR))
25 std::cout << "Credit " << -balance
<< endl;
26 else
27 std::cout << "Debt " << balance
<< endl;
28
29 return (0);
30 }
31
32 int main()
33 {
34 /* Test code */
35 billing(50);
36 billing(-10);
37 return (0);
38 }
(请参见“提示44”和“答案31”)
2.11 程序14:移位程序
程序员都知道,向左移位就等于乘以2的一次幂。即:
x << 1与x * 2 (2 = 2 )相同
x << 2与x * 4 (4 = 2 )相同
x << 3与x * 8 (8 = 2 )相同
有个程序员使用该诀窍快速执行一个简单的计算,但是却出现了问题。
1 /************************************************
2 * Simple syntax testing. ?*
3 ************************************************/
4 #include <iostream>
5
6 int main(void)
7 {
8 int x,y; // Two numbers
9
10 x = 1;
11
12 y = x<<2 + 1; // x<<2 = 4 so y = 4+1 = 5
13 std::cout << "Y=" << y <<
std::endl;
14 return (0);
15 }
(请参见“提示226”和“答案49”)
花絮
一个黑客接受了一个任务:编写一个程序,以模拟具有四种功能的计算器。任务要求程序能够执行加减乘除运算,但并没有指定使用何种数字,结果该黑客的程序使用罗马数字(IV
+ III = VII)。此外还需要提供程序的用户手册,但是任务中并没有规定要使用何种语言,结果这个黑客提供了一个利用拉丁文编写的详细的用户手册。
2.12 程序15:没有任何单词是关键字
下面的程序设计用于查看单词是否是关键字。它为什么不能正常运行呢?
1 /************************************************
2 * test the keyword finding function: "keyword"
?? *
3 ************************************************/
4 #include <cstring>
5 #include <iostream>
6
7 /************************************************
8 * keyword -- return true if a keyword found ?????????
??? *
9 ************************************************/
10 bool keyword(
11 const char word[] // The work to look for
12 )
13 {
14 // A set of keywords
15 static const char *key_list[] = {
16 "bool",
17 "int",
18 "const",
19 NULL
20 };
21 int i; // Index into the list
22
23 // Look for the keyword
24 for (i = 0; key_list[i] != 0; ++i) {
25 if (std::strcmp(word, key_list[i]))
26 return (true);
27 }
28 return (false);
29 }
30 int main()
31 {
32 std::cout << "keyword(bool) = " <<
33 keyword("bool") << '\n';
34
35 std::cout << "keyword(sam) = " <<
36 keyword("sam") << '\n';
37 return (0);
38 }
(请参见“提示294”和“答案76”)
2.13 程序16:事半功倍
为什么复制下面这个程序如此慢?在我的系统中复制该文件花费了1分34秒,而利用Linux 的cp命令执行相同的任务,所花费的时间却不到半秒钟。是什么使该程序的复制如此快呢?
1 /************************************************
2 * copy input file to output file. ?*
3 ************************************************/
4 #include <iostream>
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 int main() {
9 // The fd of the input file
10 int in_fd = open("file.in", O_RDONLY);
11
12 // The fd of the output file
13 int out_fd = open("file.out",
14 O_WRONLY|O_CREAT, 0666);
15
16 char ch; // Character to copy
17
18 if (in_fd < 0) {
19 std::cout <<
20 "Error could not open input file\n";
21 exit (8);
22 }
23
24 if (out_fd < 0) {
25 std::cout <<
26 "Error could not open output file\n";
27 exit (8);
28 }
29 while (1) {
30 if (read(in_fd, &ch, 1) != 1)
31 break;
32
33 write(out_fd, &ch, 1);
34 }
35 close(in_fd);
36 close(out_fd);
37 return (0);
38 }
(请参见“提示6”和“答案96”)
|