C++ Primer5 第一章练习题答案ps肯定有错误,只为交流,望各位大神指正
来源:互联网 发布:睫毛膏干了 知乎 编辑:程序博客网 时间:2024/06/06 01:05
EXERCISES SECTION 1.1.1
Exercise 1.1: Review the documentation foryour compiler and determine what file naming convention it uses. Compile andrun the main program from page 2.
Source files .cpp; header files .h
Exercise 1.2: Change the program to return-1. A return value of -1 is often treated as an indicator that the programfailed. Recompile and return your program to see how your system treats afailure indicator from main.
There is nothing different. ?
EXERCISES SECTION 1.2
Exercise 1.3: Write a program to printHello, World on the standard output.
#include "stdafx.h"
#include<iostream>
int _tmain(int argc,_TCHAR*argv[])
{
std::cout << "Helllo,World"<< std::endl;
return 0;
}
Exercise1.4: Our program used the additionoperator, +, to add two numbers. Write a program that uses the multiplicationoperator, *, to print the product instead.
#include "stdafx.h"
#include<iostream>
int _tmain(int argc,_TCHAR*argv[])
{
std::cout << "Enter twonumbers:"<< std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of"<< v1 <<" and " << v2
<< " is " << v1*v2 <<std::endl;
return 0;
}
Exercise1.5: we wrote the output in onelarge statement. Rewrite the program to use a separate statement to print eachoperand.
#include "stdafx.h"
#include<iostream>
int _tmain(int argc,_TCHAR*argv[])
{
std::cout << "Enter twonumbers:";
std::cout << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1;
std::cin >> 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;
}
Exercise 1.6: Explain whether the followingprogram fragment is legal.
std::cout << "The sum of"<< v1;
<< " and " << v2;
<< " is " << v1 + v2 <<std::endl;
If theprogram is legal, what does it do? If the program isnot legal, why not? How would you fix it?
It is not legal. Because on the left-hand ofthe output operator (<<) must be an ostream object!
We can fix it like this: omit the semicolonbefore <<.
std::cout << "The sum of"<< v1
<< " and " << v2
<< " is " << v1 + v2 <<std::endl;
Or we can also fix it in this way: insertstd::cout before << every line.
std::cout << "The sum of"<< v1;
std::cout << " and " << v2;
std::cout << " is " << v1 + v2 <<std::endl;
EXERCISES SECTION 1.3
Exercise 1.7: Compile a program that hasincorrectly nested comments.
Exercise 1.8: Indicate which, if any, ofthe following output statements are legal:
std::cout<< "/*";
std::cout << "*/";
std::cout <</*"*/"*/;
std::cout <</*"*/"/*"/*"*/ ;
After you have predicted what will happen,test your answers by compiling a program with each of these statements. Correctany errors you encounter.
The third line std::cout<</*"*/"*/;isillegal.
EXERCISES SECTION 1.4.1
Exercise 1.9: Write a program that uses awhile to sum the numbers from 50 to 100.
int _tmain(int argc,_TCHAR*argv[])
{
//a program that uses a while to sumthe numbers from 50 to 100
int sum = 0, val = 50;
//keep executing the while as long asval is less than or equal to 100
while (val <= 100){
sum += val;//assigns sum + val to sum
++val;//add 1 to val
}
std::cout << " Sum of 50 to100 inclusive is " << sum << std::endl;
return 0;
}
Exercise 1.10: In addition to the ++ operatorthat adds 1 to its operand,there is a decrement operator(--) that subtracts 1.Use the decrement operator to write a while that prints the numbers from tendown to zero.
int _tmain(int argc,_TCHAR*argv[])
{
int val = 10;
//keep executing the while as long asval is greater than or equal to 0
while (val >= 0){
std::cout << val<< "";
--val;
}
return 0;
}
Exercise 1.11: Write a program that promptsthe user for two integers. Print each number in the range specified by thosetwo integers.
int _tmain(int argc,_TCHAR*argv[])
{
std::cout << "Enter twonumbers:\n";
int v1, v2;
std::cin >> v1 >> v2;
int lower, upper;
//keep the larger one in theupper,the smaller in the lower
if (v1 < v2){
upper = v2;
lower = v1;
}
else{
upper = v1;
lower = v2;
}
while (lower <= upper){
std::cout << lower<< "";
++lower;
}
return 0;
}
EXERCISES SECTION 1.4.2
Exercise 1.12: what does the following for loopdo? What is the final value of sum?
intsum=0;
for(inti=-100;i<=100;++i)
sum+=i;
It summed the numbers from -100 through 100.The value of sum is 0.
Exercise 1.13:Rewrite the exercises from1.4.1(p.13) using for loops.
Exercise1.9:
int _tmain(int argc,_TCHAR*argv[])
{
int sum = 0;
for (int val = 10; val <= 100; val++){
sum += val;
}
std::cout << "The sum ofnumbers from 50 through 100 is: "
<< sum<<std::endl;
return 0;
}
Exercise1.10:
int _tmain(int argc,_TCHAR*argv[])
{
for (int val = 10; val >= 0; --val){
std::cout << val<< "";
}
std::cout << std::endl;
return 0;
}
Exercise1.11:
int _tmain(int argc,_TCHAR*argv[])
{
std::cout << "Enter twonumbers:\n";
int v1, v2;
std::cin >> v1 >> v2;
int lower, upper;
//keep the larger one in the upper,the smaller in the lower
if (v1 < v2){
upper = v2;
lower = v1;
}
else{
upper = v1;
lower = v2;
}
for (int val = lower; val <= upper;++val){
std::cout << val<< "";
}
std::cout << std::endl;
return 0;
}
Exercise 1.14: Compare and contrast the loopsthat used a for with those using a while. Are there advantages or disadvantagesto using either form?
Exercise 1.15: Write programs that containthe common errors discussed in the box on page 16. Familiarize yourself withthe messages the compiler generates.
int _tmain(int argc,_TCHAR*argv[]
{
return 0;
}
Error 1 error C2143: syntax error : missing ')'before '{'
EXERCISES SECTION 1.4.3
Exercise 1.16: Write your own version of aprogram that prints the sum of a set of integers read from cin.
int _tmain(int argc,_TCHAR*argv[])
{
int sum = 0, val = 0;
while (std::cin >> val){
sum += val;
}
std::cout << "Sum is " << sum <<std::endl;
return 0;
}
EXERCISES SECTION 1.4.4
Exercise 1.17: What happens in the programpresented in this section if the input values are equal? What if there are noduplicated values?
There is only one output.
Every number has one output.
Exercise 1.18: Compile and run the programfrom this section giving it only equal values as input. Run it again giving itvalues in which no number is repeated.
Exercise 1.19: Revise the program you wrotefor the exercises in 1.4.1(p.13) that printed a range of numbers so that ithandles input in which the first number is smaller than the second.
EXERCISES SECTION 1.5.1
Exercise 1.20: http://www.informit.com/title/0321714113contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that fileto your working directory. Use it to write a program that reads a set of booksales transactions, writing each transaction to the standard output.
#include "stdafx.h"
#include<iostream>
#include"Sales_item.h"
int _tmain(int argc,_TCHAR*argv[])
{
Sales_item a;
while (std::cin >> a){
std::cout << a <<std::endl;
}
return 0;
}
Exercise 1.21: Write a program that readstwo Sales_item objects that have the same ISBN and produces their sum.
#include "stdafx.h"
#include<iostream>
#include"Sales_item.h"
int _tmain(int argc,_TCHAR*argv[])
{
Sales_item a, b;
std::cin >> a >> b;
std::cout << a + b <<std::endl;
return 0;
}
Exercise 1.22: Write a program that readsseveral transactions for the same ISBN. Write the sum of all the transactionsthat were read.
#include "stdafx.h"
#include<iostream>
#include"Sales_item.h"
int _tmain(int argc,_TCHAR*argv[])
{
Sales_item trans, total;
std::cin >> total;
while (std::cin >> trans){
total += trans;
}
std::cout << total <<std::endl;
return 0;
}
EXERCISES SECTION 1.5.2
Exercise 1.23: Write a program that readsseveral transactions and counts how many transactions occur for each ISBN.
#include "stdafx.h"
#include<iostream>
#include"Sales_item.h"
int _tmain(int argc,_TCHAR*argv[])
{
Sales_item trans, total;
if (std::cin >> total){
int cnt = 1;
while (std::cin >> trans){
if (total.isbn() == trans.isbn()){
++cnt;
}
else{//output the result,and process thenext one
std::cout << total.isbn()<< ":\t" << cnt <<std::endl;
total = trans;
cnt = 1;
} //else
}//while
std::cout <<total.isbn() << ":\t" << cnt << std::endl;
}//outerif
else{
std::cerr << "Nodata?!"<< std::endl;
return -1;
}
return 0;
}
Exercise 1.24: Test the previous program bygiving multiple transactions representing multiple ISBNs. The records for eachISBN should be grouped together.
EXERCISES SECTION 1.6
Exercise 1.25: Using the Sales_item.hheader from the Web site, compile and execute the bookstore program presentedin this section.
#include "stdafx.h"
#include<iostream>
#include"Sales_item.h"
int _tmain(int argc,_TCHAR*argv[])
{
Sales_item total;// variable to hold data for the nexttransaction
// read the first transaction andensure that there are data to process
if (std::cin >> total) {
Sales_item trans;// variable to hold the running sum
// read and process the remainingtransactions
while (std::cin >> trans) {
// if we're still processingthe same book
if (total.isbn() == trans.isbn())
total += trans; // update the running total
else {
// print results for theprevious 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 user
std::cerr << "Nodata?!"<< std::endl;
return -1; //indicate failure
}
return 0;
}
- C++ Primer5 第一章练习题答案ps肯定有错误,只为交流,望各位大神指正
- c++primer5 第一章
- c++primer5 3.20答案
- 浅谈一下weex,如有错误,请阿里大神指正
- c语言试题整理(如有错误还望指正)
- 简述 对UITableView的封装 有改进或者错误的地方请各位大大指正
- JS写的一个日期选择控件(移动网页的),写得不当的地方,望各位大神指正
- C程序设计语言练习题.第一章
- 快学scala 第一章练习题课后答案
- c语言练习题+答案分享
- C/C++ 练习题及答案
- c++primer部分练习题答案
- 各位大神,求解求解!!小女初接触编程,有多多不解,该程序显示无错误,为什么无论输入a>b还是b>a输出结果均为A>B??
- 欢迎交流指正
- 正则表达式练习题以及答案(如果有错误的话帮忙更正)
- c++Primer5,高级主题
- c++primer5第二章
- c++primer5 3.17
- ios和android游戏纹理优化和内存优化
- java微信公众平台开发实战(一)——图书馆微信查书助手
- 结束QQ即时通信垄断,开辟即时通信互联互通instantnet时代
- win 7 db2 catalog 没有权限问题解决方法
- Http访问过程 原理
- C++ Primer5 第一章练习题答案ps肯定有错误,只为交流,望各位大神指正
- iOS 使用使用Swizzling 全局修改Navigation的返回按钮
- 笨笨熊搬家交通篇
- 一键清除SVN信息
- jqgridStruts1
- 键盘读入学生,并输出到TXT中
- Android开发之如何保证Service不被杀掉(broadcast+system/app)
- Eclipse上安装GIT插件EGit及使用
- UIStepper的使用方法