课后习题:
练习1.1: 查阅你使用的编译器的文档,确定它所使用的文件命名约定,编译并运行main程序。
我使用的编译器为visio studio2013 读者使用其他的编译器也是可以的只要能支持 c++11即可。
//练习1.1
int main()
{
return 0;
}
练习1.2:改写程序,让它返回-1,返回-1通常被当作程序错误的标识。重新编译运行你的程序,观察你的系统如何处理main返回的错误标识。
答:返回-1 和返回0 ,在单个函数中没看出有什么区别,系统也不会做什么处理。
练习 1.3:编写程序,在标准输出上打印Hello , World 。
//练习 1.3
#include <iostream>
int main()
{
std::cout << "Hello, word!" << std::endl;
std::cin.get();
return 0;
}
练习 1.4:我们的程序使用加法运算符+来将两个数相11口。编写程序使用乘法运算符*,来打印两个数的积。
//练习1.4
#include <iostream>
int main()
{
std::cout << "Enter two numbers: " << std::endl;
int a1 = 0, a2 = 0;
std::cin >> a1 >> a2;
std::cout << "The multiply of " << a1 << " and "
<< a2 << " is " << a1 * a2 << "." << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
练习 1.5:我们将所有输出操作放在一条很长的语句巾。重写程序, 将每个运算对象的打印操作放在一条独立的语句巾。
//练习 1.5
#include <iostream>
int main()
{
std::cout << "Enter two numbers: ";
std::cout << std::endl;
int a1 = 0, a2 = 0;
std::cin >> a1 >> a2;
std::cout << "The multiply of ";
std::cout << a1;
std::cout << " and ";
std::cout << a2;
std::cout << " is ";
std::cout << a1 * a2;
std::cout << ".";
std::cout << std::endl;
std::cin.get();
std::cin.get();
return 0;
}
练习 1.6:解释下面程序片段是否合法。
std::cout<<"the sum of"<<v1;
<<"and"<<v2;
<<"is"<<v1*v2<<std::endl;
如果程序是合法的,它输出千|么?如果程序不合法,原因何在?应该如何修正?
答:不合法,因为输出运算符左边没有运算符对象,即没有输出流对象,改正如下:
std::cout<<"the sum of"<<v1;
std::cout <<"and"<<