C++ primer plus复习题及练习程序答案第二章

第二章:

复习题:

1. 函数

2. 在最终编译之前,使用iostream中的内容替换该编译指令。

3. 程序可以使用std空间的定义,如cout或cin。

4. cout<<"Hello,world\n";或cout<<"Hello,world"<<endl;

5. int cheeses;

6.cheeses = 32;

7. cin>>cheeses;

8.cout<<"We have"<<cheeses<<"varieties of cheeses"<<endl;

9.int froop (double t);函数froop有返回值,且返回类型为int,函数参数类型为double。

  void rattle (int n);函数rattle没有返回值,参数类型为int。

  int prune(void);函数有返回值,且返回类型为int,没有参数。

10. 当函数返回类型为void时,不必使用return。

11.原因是程序中没有using namespace std语句。

编程练习

2.1
#include <iostream>
using namespace std;
int main(void)
{
	cout<<"name:DONGBYG"<<"Liaoning\n";
	return 0;
}
2.2 long2ma
#include<iostream>
using namespace std;
main()
{
	double Long,Ma;
	cin>>Long;
	Ma = 220*Long;
	cout<<Long<<"Long = "<<Ma<<"Ma\n";
	return 0;
}
2.3
#include<iostream>
using namespace std;
void mice();
void see();
int main()
{
	mice();
	mice();
	see();
	see();
	return 0;
}
void mice()
{
	cout<<"Three blind mice"<<endl;
}
void see()
{
	cout<<"See how they run"<<endl;
}
2.4
#include<iostream>
using namespace std;
int main()
{
	int age,month;
	cout<<"enter your age:";
	cin>>age;
	month = 12*age;
	cout<<"your age"<<age<<"="<<month<<"months"<<endl;
}

2.5
#include<iostream>
using namespace std;
double she2hua(double t);
int main()
{
	double she,hua;
	cout<<"Please enter a Celsius value:";
	cin>>she;
	hua = she2hua(she);
	cout<<"\n"<<she<<" degree Celsius is "<<hua<<" degrees Fahrenheit.\n";
    return 0;
}
double she2hua(double t)
{
	double n;
	n = 1.8*t+32.0;
	return n;
}
2.6
#include<iostream>
using namespace std;
double guang2tian(double t);
int main()
{
	double guang,tian;
	cout<<"Enter the number of light years:";
	cin>>guang;
	tian = guang2tian(guang);
	cout<<"\n"<<guang<<" light years = "<<tian<<" astronomical units.\n";
    return 0;
}
double guang2tian(double t)
{
	double n;
	n = 63240*t;
	return n;
}
2.7
#include<iostream>
using namespace std;
void showtime(int h,int m);
int main()
{
	int hours,minutes;
	cout<<"Enter the number of hours:";
	cin>>hours;
	cout<<"\nEnter the number of minutes:";
	cin>>minutes;
	showtime(hours,minutes);
    return 0;
}
void showtime(int h,int m)
{
	cout<<"\nTime :"<<h<<":"<<m<<endl;
}






  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ Primer习题集(第五版) , 带目录完整版。 --------------------------------------------------------------------------- 目录 第1章............................................................ 1 练习1.1 练 习1.25 第2 章变量和基本类型................................................. 12 练习2.1 练 习2.42 第3 章字符串、向量和数组..............................................37 练习3.1 练 习3.45 第4 章表达式......................................................... 80 练习4.1 练 习4.38 第5 章语句........................................................... 99 练习5.1 练 习5.25 第6 章函数.......................................................... 120 练习6.1 练 习6.56 m m m ...................................................................... 152 练习7.1 练 习7.58 第8 章1 0库..........................................................183 练习8.1 练 习8.14 第9 章顺序容器...................................................... 193 练习9.1 练 习9.52 第10章泛型算法..................................................... 234 练习10.1 练 习10.42 目录 ◄ v 第11章关联容器..................................................... 273 练习11.1 练 习11.38 第12章动态内存..................................................... 297 练习12.1 练 习12.33 第13章拷贝控制..................................................... 331 练习13.1 练 习13.58 第14章重载运算与类型转换............................................368 练习14.1 练 习14.53 第15章面向对象程序设计..............................................399 练习15.1 练 习15.42 第16章模板与泛型编程............................................... 424 练习16.1 练 习16.67 第17章标准库特殊设施............................................... 458 练习17.1 练 习17.39 第18章用于大型程序的工具............................................483 练习18.1 练 习18.30 第19章特殊工具与技术............................................... 502 练习19.1 练 习19.26

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值