笔记较为零散,都是自己不熟悉的知识点。
习题答案至于一个.cc中,需要运行某一题直接修改#define NUM**, 如运行第一题为#define NUM11,题1.24定义为NUM124
chapter 1
1、std::cout << "Entertwo numbers:" << std::endl;等价于
std::cout<< "Enter two numbers:";
std::cout << std::endl;
2、命名空间的作用是防止与库中定义的名字发生冲突,其副作用是,当使用标准库中的名字时必须显示地表达出使用的是命名空间std下的名字。
3、for (int val = 1; val <= 10; ++val)
对于for循环内的val变量,当结束循环后不再可访问
4、文件结束符
ctrl+d for unix/linux/ etc
ctrl+z for windows/dos
一旦测试失败,while 终止并退出循环体,执行 while 之后的语句。该语
句在输出 sum 后输出 endl,endl 输出换行并刷新与 cout 相关联的缓冲区。
最后,执行 return,通常返回零表示程序成功运行完毕。
5、 exit(-1)或者return(-1)为什么shell得到的退出码是255?
http://www.cnblogs.com/tangdoudou/p/3385149.html
exit或return,只能使用0~255之间的值,-1的unsigned值就是255.
#include<iostream>
#include "Sales_item.h"
#define NUM120
//
using namespace std;
int answerEample(int value1,int value2);
int main(){
/*1.1*/
#ifdef NUM11
cout<<"C++程序一般涉及两类文件:头文件和源文件。文件后缀通常表明文件的类型,"
"如头文件后缀可以是.h或.hpp;源文件的后缀可以是.cc或.cpp,具体的后缀与使用的编译器有关"<<endl;
#endif
/*1.2*/
#ifdef NUM12
cout<<"window和linux系统下不会报告main函数的运行失败,所以程序返回-1或返回1在运行效果上并没有什么不同,"
"但是,DOS命令下运行程序,然后键入echo %ERRORLEVEL%,则会显示返回值-1,linux系统下,键入echo $? 则会返回255"<<endl;
#endif
/*1.3*/
#ifdef NUM13
cout<<"Hello world"<<endl;
#endif
/*1.4*/
#ifdef NUM14
cout<<"Enter two number: "<<endl;
int v1, v2;
cin >> v1 >> v2;
cout<< "The product of " <<v1<<" and "<<v2<<" is "<<v1*v2<<endl;
#endif
/*1.5*/
#ifdef NUM15
cout<<"Enter two number: "<<endl;
int v1, v2;
cin >> v1 >> v2;
cout<< "The product of ";
cout<<v1;
cout<<" and ";
cout<<v2;
cout<<" is ";
cout<<v1*v2;
cout<<endl;
#endif
/*1.6*/
#ifdef NUM16
cout<<"代码不合法"
"第1,2,3行的末尾有分号,表示这段代码分别构成三条语句."
"'<<'是二元操作符,在第2,3两行中,第一个'<<'缺少左操作数,因此不合法。应该在2,3行开头加上cout. "<<endl;
#endif
/*1.7*/
#ifdef NUM17
/*
* commet pairs /(两个**) /cannot nest
* "cannot nest" is considered source code.
* as is the rest of the problem
*/
cout<<"错误信息:注释不可嵌套。"<<endl;
#endif
/*1.8*/
#ifdef NUM18
cout<< "第1,2,4行合法。"
"第3行<<操作符之后到第二个双引号之前的部分被注释掉了,导致<<操作符的右操作数不是一个完整的字符串."<<endl;
#endif
/*1.9*/
#ifdef NUM19
int sum(0), val(50);
while(val <= 100){
sum += val;
++val;
}
cout<<" Sum of 50 to 100 is: "<<sum<<endl;
#endif
/*1.10*/
#ifdef NUM110
int i(10);
while(i >= 0){
cout<< i << " ";
--i;
}
cout<<endl;
#endif
/*1.11*/
#ifdef NUM111
int v1,v2,low,high;
cout<< "Enter two numbers: "<<endl;
cin >> v1 >> v2;
while(v1 <= v2){
cout<< v1 <<" ";
++v1;
}
#endif
/*1.12*/
#ifdef NUM112
int sum = 0;
for(int i = -100; i<=100; ++i)
sum += i;
cout<<"-100 到 100 整数相加: "<<sum<<endl;
#endif
/*1.13*/
#ifdef NUM113
int sum(0), val(50);
for(val=50; val <= 100; ++val)
sum += val;
cout<< "The sum of numbers from 50 to 100: "<<sum <<endl;
int i(10);
for(i=10; i>=0; --i)
cout<< i <<" ";
cout<<endl;
int v1,v2,low,high;
cout<< "Enter two numbers: "<<endl;
cin >> v1 >> v2;
if(v1 <= v2){
low = v1;
high = v2;
}else{
low = v2;
high = v1;
}
for(;v1 <= v2; ++v1)
cout<<v1 <<" ";
cout<<endl;
#endif
/*1.14*/
#ifdef NUM114
cout<<" for循环,循环控制变量的初始化和修改都放在语句头部分,形式简洁,特别适用于循环次数已知的情况,"
" while循环,循环控制变量的初始化一般放在while循环的之前,循环控制变量的修改一般放在循环体中,"
"形式不如for简洁,但是比较适用于循环次数不易预知的情况下。"<<endl;
#endif
/*1.16*/
#ifdef NUM116
int sum(0), val;
while(cin >> val)
sum += val;
cout<< "The sum is: "<<sum <<endl;
#endif
/*1.17*/
#ifdef NUM117
cout<<"下载我上传资源中的第五版源代码测试。"<<endl;
#endif
/*1.19*/
#ifdef NUM119
int v1,v2,low,high;
cout<< "Enter two numbers: "<<endl;
cin >> v1 >> v2;
if(v1 <= v2){
low = v1;
high = v2;
}else{
low = v2;
high = v1;
}
while(v1 <= v2){
cout<< v1 <<" ";
++v1;
}
#endif
/*1.20*/
#ifdef NUM120
Sales_item book;
cout<<"Enter transactions: "<<endl;
while(cin>> book){
cout << "ISBN, number of copies sold, "
" total revenue, and average price are: "<<endl;
cout<< book <<endl;
}
#endif
/*1.21*/
#ifdef NUM121
Sales_item book1, book2;
cin >> book1 >> book2;
if(book1.same_isbn(book2))
cout<<book1 + book2 <<endl;
else
cout<<"The two books have different ISBN "<<endl;
#endif
/*1.22*/
#ifdef NUM122
Sales_item total, book;
cout<<" Enter transactions: "<<endl;
if(cin>>total){
while(cin >> book){
if(total.same_isbn(book))
total = total + book;
else
cout<<"differ ISNB "<<endl;
return -1;
}
cout << "ISBN, number of copies sold, "
" total revenue, and average price are: "<<endl;
cout<<total<<endl;
}else
cout<<"Input error " <<endl;
return -255;
#endif
/*1.23 */
#ifdef NUM123
Sales_item book1,book2;
int count(0);
cout<<"Enter transctions: "<<endl;
cin >> book1;
count = 1;
while(cin >> book2)
if(book1.same_isbn(book2))
++count;
else {
cout<<"Transaction amount of previous ISBN: "<<endl;
book1 = book2;
count = 1;
}
cout << "Transction amount of the last ISBN "<< count <<endl;
#endif
参考资料:
c++ primer中文版第五版,电子工业出版社。
c++ primer第四版习题解答,人民邮电出版社。