《C++ Primer Plus 第六版》编程练习参考答案(第二章)

第二章

第一题:

#include <iostream>
using namespace std;

int main()
{
	cout<<"Horace.Kem"<<" Ganjiang East Road NO.333, Suzhou, Jiangsu, China";
}

第二题:

#include <iostream>
using namespace std;

int main()
{
	int dist;
	cout<<"Please enter the distance in long:";
	cin>>dist;
	cout<<"The distance in yards is:"<<220*dist<<endl;
}

第三题:

#include <iostream>
using namespace std;

void fun1();
void fun2();

int main()
{
	fun1();
	fun1();
	fun2();
	fun2();
}

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

void fun2()
{
	cout<<"See how they run"<<endl;
}

第四题:

#include <iostream>
using namespace std;

int main()
{
	int age;
	cout<<"Enter your age: ";
	cin>>age;
	cout<<"The number of months is: "<<12*age<<endl;
}

第五题:

#include <iostream>
using namespace std;

int main()
{
	double Celsius,Fahrenheit;	
	cout<<"Please enter a Celsius value: ";
	cin>>Celsius;
	Fahrenheit=1.8*Celsius+32;
	cout<<Celsius<<" degrees Celsius is "<<Fahrenheit<<
	" degrees Fahrenheit"<<endl;
}

第六题:

#include <iostream>
using namespace std;

double astro(double);

int main()
{
	double lightyear,astronomical;
	cout<<"Enter the number of light years: ";
	cin>>lightyear;
	astronomical=astro(lightyear);
	cout<<lightyear<<" light years = "<<
	astronomical<<" astronomical units."<<endl;
}

double astro(double ly)
{
	return 63240*ly;
}

第七题:

#include <iostream>
using namespace std;

void clock(int,int);

int main()
{
	int hours,minutes;
	cout<<"Enter the number of hours: ";
	cin>>hours;
	cout<<"Enter the number of minutes: ";
	cin>>minutes;
	clock(hours,minutes);
}

void clock(int h,int m)
{
	cout<<"Time: "<<h<<":"<<m<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值