练习1.1:查阅你使用的编译器文档,确定它所使用的文件命名约定。编译并运行第2页的main程序。
参考:GCC and File Extensions
练习1.2:改写程序,让它返回-1。返回值-1通常被当作程序错误的标识。重新编译并运行你的程序,观察你的系统如何处理main返回的错误标识。
参考:Exit Codes With Special Meanings
练习1.3:编写程序,在标准输出上打印 Hello, World。
#include int main()
{std::cout << "Hello,World" << std::endl;return 0;
}
练习1.4:我们的程序使用加法运算符+来将两个数相加。编写程序使用乘法运算符*,来打印两个数的积。
#include int main()
{std::cout << "Enter two numbers:" << std::endl;int v1 = 0, v2 = 0;std::cin >> v1 >> v2;std::cout << "The product is " << v1 * v2 << std::endl;return 0;
}
练习1.5:我们将所有输出操作放在一条很长的语句中。重写程序,将每个运算对象的打印操作放在一条独立的语句中。
#include int main()
{std::cout << "Enter two numbers:" << std::endl;int v1 = 0, v2 = 0;std::cin >> v1 >> v2;std::cout << "The product of ";std::cout << v1;std::cout << " and ";std::cout << v2;std::cout << " is ";std::cout << v1 * v2;std::cout << std::endl;return 0;
}
练习1.6:解释下面程序片段是否合法。
std::cout << "The sum of " << v1; << " and " << v2; << " is " << v1 + v2 << std::endl;
如果程序是合法的,它的输出是什么?如果程序不合法,原因何在?应该如何修正?
std::cout << "The sum of " << v1<< " and " << v2<< " is " << v1 + v2 << std::endl;
练习1.7:编译一个包含不正确的嵌套注释的程序,观察编译器返回的错误信息。
$ g++ -o ex07 ex07.cpp
ex07.cpp: In function ‘int main()’:
ex07.cpp:5:3: error: expected primary-expression before ‘/’ token*/^
ex07.cpp:6:2: error: expected primary-expression before ‘return’return 0;^
ex07.cpp:6:2: error: expected ‘;’ before ‘return’
练习1.8:指出下列哪些输出语句是合法的(如果有的话):
std::cout << "/*"; std::cout << "*/"; std::cout << /* "/*" */; std::cout << /* "/*" /* "/*" */;
预测编译这些语句会产生什么样的结果,实际编译这些语句来验证你的答案(编写一个小程序,每次将上述一条语句作为其主体),改正每个编译错误。
第三行非法,第三行改为:
std::cout << /* "*/" */";
练习1.9:编写程序,使用while循环将50到100的整数相加。
#include int main()
{int i = 50 ,sum = 0;while(i <= 100){sum += i;++i;}std::cout << sum << std::endl;return 0;
}
练习1.10:除了++运算符将运算对象的值增加1之外,还有一个递减运算符(–)实现将值减少1。编写程序,使用递减运算符在循环中按递减顺序打印出10到0之间的整数。
#include int main()
{int i = 10;while(i >= 0){std::cout << i << std::endl;--i;}return 0;
}
练习1.11:编写程序,提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数。
#include int main()
{int nSmall = 0, nBig = 0;std::cout << "please input two integers:";std::cin >> nSmall >> nBig;if (nSmall > nBig) {int nTmp = nSmall;nSmall = nBig;nBig = nTmp;}while (nSmall <= nBig) {std::cout << nSmall << " ";++nSmall;}std::cout << std::endl;return 0;
}
练习1.12:下面的for循环完成了什么功能?sum的终值是多少?
int sum = 0; for (int i = -100; i <= 100; ++i) sum += i;
求从-100加到100的和,最终值为0。
练习1.13:使用for循环重做1.4.1节中的所有练习(第11页)。
Ex1.9:
#include int main()
{int sum = 0;for (int i = 50; i <= 100; ++i) sum += i;std::cout << "the sum is: " << sum << std::endl;return 0;
}Ex1.10:
#include int main()
{for (int i = 10; i >= 0; --i)std::cout << i << std::endl;return 0;
}#include Ex1.11:
int main()
{std::cout << "please input two integers:\n";int small = 0, big = 0;std::cin >> small >> big;if (small > big){int tmp = small;small = big;big = tmp;}for (int i = small; i != big; ++i)std::cout << i << std::endl;return 0;
}
练习1.14:对比for循环和while循环,两种形式的优缺点各是什么?
已知迭代次数时,使用for循环比较简洁;不知道迭代次数时使用while。
参考:A similar question on Stack Overflow
练习1.15:编写程序,包含第14页“再探编译”中讨论的常见错误。熟悉编译器生成的错误信息。
练习1.16:编写程序,从cin读取一组数,输出其和。
#include int main()
{int sum = 0, value = 0;while(std::cin >> value){sum += value;}std::cout << sum << std::endl;return 0;
}
练习1.17:如果输入的所有值都是相等的,本节的程序会输出什么?如果没有重复值,输出又会是怎样的?
如果输入的所有值都是相等的,Ctrl+d后,才有输出统计的个数;如果没有重复输出,输入与前个不同的数即打印一行,Ctrl+d后,才输出最后一行统计的个数。
练习1.18:编译并运行本节的程序,给它输入全都相等的值。再次运行程序,输入没有重复的值。
$ ./ex17
1 1 1 1 1
1 occurs 5 times
$ ./ex17
1 2 3 4 5
1 occurs 1 times
2 occurs 1 times
3 occurs 1 times
4 occurs 1 times
5 occurs 1 times
练习1.19:修改你为1.4.1节练习1.11(第11页)所编写的程序(打印一个范围内的数),使其能处理用户输入的第一个数比第二个数小的情况。
练习1.20:在网站http://www.informit.com/title/032174113 上,第1章的代码目录包含了头文件 Sales_item.h。将它拷贝到你自己的工作目录中。用它编写一个程序,读取一组书籍销售记录,将每条记录打印到标准输出上。
#include
#include "Sales_item.h"int main()
{Sales_item item;std::cin >> item;std::cout << item <
练习1.21:编写程序,读取两个 ISBN 相同的 Sales_item 对象,输出他们的和。
#include
#include "Sales_item.h"int main()
{Sales_item item1,item2;std::cin >> item1 >> item2;std::cout << item1 + item2 <
练习1.22:编写程序,读取多个具有相同 ISBN 的销售记录,输出所有记录的和。
#include
#include "Sales_item.h"int main()
{Sales_item item,sum_item;std::cin >> sum_item;while(std::cin >> item){sum_item += item;}std::cout << sum_item << std::endl;return 0;
}
练习1.23:编写程序,读取多条销售记录,并统计每个 ISBN(每本书)有几条销售记录。
#include
#include "Sales_item.h"int main()
{Sales_item currItem, valItem;if (std::cin >> currItem) {int cnt = 1;while (std::cin >> valItem) {if (valItem.isbn() == currItem.isbn())++cnt;else {std::cout << currItem << " occurs " << cnt << " times "<< std::endl;currItem = valItem;cnt = 1;}}std::cout << currItem << " occurs " << cnt << " times " << std::endl;}return 0;
}
练习1.24:输入表示多个 ISBN 的多条销售记录来测试上一个程序,每个 ISBN 的记录应该聚在一起。
练习1.25:借助网站上的 Sales_item.h 头文件,编译并运行本节给出的书店程序。
#include
#include "Sales_item.h"int main()
{Sales_item total; // variable to hold data for the next transaction// read the first transaction and ensure that there are data to processif (std::cin >> total){Sales_item trans; // variable to hold the running sum// read and process the remaining transactionswhile (std::cin >> trans) {// if we're still processing the same bookif (total.isbn() == trans.isbn()) total += trans; // update the running total else { // print results for the previous book std::cout << total << std::endl; total = trans; // total now refers to the next book}}std::cout << total << std::endl; // print the last transaction} else {// no input! warn the userstd::cerr << "No data?!" << std::endl;return -1; // indicate failure}return 0;
}