C ++ primer plus 第二章课后练习

复习题

1.C++程序的模块叫什么?

由于函数创建C++程序的模块,对于C++的OOP至关重要。
  函数是一组一起执行一个任务的语句。每个 C++ 程序都至少有一个函数,即主函数 main () ,所有简单的程序都可以定义其他额外的函数。甚至也可以是一个函数,只要它完成一个功能,它就可以视为一个模块。

2.下面的预编译器指令是做什么用的?

#include <iostream>

该编译指令使预编译器把iostream文件的内容添加到程序中,#include指令会使iostream文件的内容和源文件的内容一起发送给编译器编译,iostream文件的io指s输入、输出,包含该文件可以使用cout、cin,接收显示外部信息。

3.下面语句是做什么用的

using namespace std;

使用std的名称空间,它使得程序可以使用 std 名称空间中的定义。不必写std::cout就可以直接写cout。

4.什么语句可以打印“hello world”和换行。

cout << "hello world" << endl;

5.什么语句可以创建名为 cheeses的整型变量?

int cheeses;

6.什么语句可以用来将值32赋给变量cheese?

cheeses = 32;

7.什么语句可以将键盘输入的值读入变量cheeses?

cin >> cheeses;

8.什么语句可以打印“we have X varieties of cheese,”其中变量X为cheese当前值

cout << "we have "<<cheese<<" varieties of cheese,"<<endl;

9.下面函数原型指出了关于函数的哪些信息?

int froop(double t);

void rattle(int n);

int prune(void);

调用函数 froop () 时,应提供一个参数,该参数的类型为 double,而该函数将返回一个 int 值。例如,可以像下面这样使用它:int gval=froop (3.14159);

函数 rattle () 接受一个 int 参数且没有返回值。例如,可以这样使用它:rattle (37);

函数 prune () 不接受任何参数且返回一个 int 值。例如可以这样使用它:int residue=prune ();

10.定义函数时,在什么情况下不必使用关键字 return?

当函数的返回类型为 void 时,不用在函数中使用 return。然而,如果想不提供返回值,则可以使用它:return ,则不会返回任何东西。

11.假设您编写的 main () 函数包含如下代码:

cout<<"Please enter your PIN:";

而编译器指出 cout 是一个未知标识符。导致这种问题的原因很可能是什么?指出 3 种修复这种问题的方法。

答:很可能是因为缺少 using 编译指令:using namespace std;

三种解决方法:

1、加上 using namespace std;

2、加上 using std::cout

3、使用 std::cout<<"Please enter your PIN:";

编程练习

将全部题目写在了一个文件里。

#include <iostream>
using namespace std;

void print01();

void print02();

double celsiusTrans(double cel);

double lightTrans(double light);

void time(int hours, int min);

int main()
{
	//第一题
	cout << "my name is 刘衡,my address is hefei." << endl;

	//第二题
	cout << "请输入你想要的距离(单位为long):";
	int distance = 0;
	cin >> distance;
	cout << "你输入的距离为:" << distance * 220 << "码" << endl;

	//第三题
	print01();
	print01();
	print02();
	print02();

	//第四题
	cout << "你的年龄:";
	int age;
	cin >> age;
	cout << "your age in months is " << age * 12 << endl;

	//第五题
	cout << "please enter a celsius value :" ;
	double celsius;
	cin >> celsius;
	celsiusTrans(celsius);

	//第六题
	cout << "enter the number of light years :" ;
	double lightYears;
	cin >> lightYears;
	lightTrans(lightYears);

	//第七题
	cout << "enter the number of hours:" ;
	int hours;
	cin >> hours;
	cout << "enter the number of mintues:" ;
	int min;
	cin >> min;
	time(hours, min);
	return 0;
}

void print01() {
	cout << "Three blind mice" << endl;
}

void print02() {
	cout << "see how they run" << endl;
}

double celsiusTrans(double cel) {
	double fahrenheit = cel * 1.8 + 32;
	cout << cel << " degrees celsius is " << fahrenheit << " degrees fahrenheit" << endl;
	return fahrenheit;
}

double lightTrans(double light) {
	double astUnits = light * 63240;
	cout << light << " light years = " << astUnits << " astronomical units" << endl;
	return astUnits;
}

void time(int hours, int min) {
	cout << "Time:" << hours << ":" << min;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值