C++ Primer Plus 第六版 第二章练习答案

C++ Primer Plus 第六版 第二章练习答案


#include <iostream>
using namespace std;

// practice 2.1
void p2_1(void){
	cout << "Raywit" << endl;
	cout << "China\n";
	return;
}


// practice 2.2
void p2_2(void){
	int Longs = 0;
	int Yards = 0;
	cout << "Input the distance in long: ";
	cin >> Longs;
	Yards = 220 * Longs;
	cout << "The distance in yards is: " << Yards << endl;
	return;
}


// practice 2.3
void function1(void){
	cout << "Three blind mice" << endl;
	return;
}
void function2(void){
	cout << "See how they run\n";
	return;
}
void p2_3(void){
	function1();
	function1();
	function2();
	function2();
	return;
}


// practice 2.4
void p2_4(void){
	int years = 0;
	int months = 0;
	cout << "Enter your age: ";
	cin >> years;
	months = years * 12;
	cout << "Your age is " << months << " monthes!" << endl;
	return;
}


// practice 2.5
double Cel2Fah(double Celsius){
	return (1.8 * Celsius + 32.0);
}
void p2_5(void)
{
	double Celsius = 0.0;
	double Fahrenheit = 0.0;
	cout << "Please enter a Celsius value: ";
	cin >> Celsius;
	Fahrenheit = Cel2Fah(Celsius);
	cout << Celsius << " degrees Celsius is " << Fahrenheit << " defrees Fahrenheit." << endl;
	return;
}


// practice 2.6
double LightYears2Astronomical(double LightYears){
	return (LightYears * 63240);
}
void p2_6(void){
	double lightyears = 0;
	double astronomical = 0;

	cout << "Enter the number of light years: ";
	cin >> lightyears;

	astronomical = LightYears2Astronomical(lightyears);
	cout << lightyears << " light years = " << astronomical << " astronomical units." << endl;
	return;
}


// practice 2.7
void DispTime(int hours, int minutes){
	cout << "Time: " << hours << ":" << minutes << endl;
	return;
}
void p2_7(void){
	int hours = 0;
	int minutes = 0;
	cout << "Enter the number of hours: ";
	cin >> hours;
	cout << "Enter the number of minutes: ";
	cin >> minutes;
	DispTime(hours, minutes);
	return;
}


int main(int argc, char **argv){
	cout << "=====================================\n"
		<< "============  Chapter2:  ============\n"
		<< "=====================================\n\n";
	cout << "==>> Practice 2_1:\n";
	p2_1();
	cout << endl;

	cout << "==>>Practice 2_2:\n";
	p2_2();
	cout << endl;

	cout << "==>>Practice 2_3:\n";
	p2_3();
	cout << endl;

	cout << "==>>Practice 2_4:\n";
	p2_4();
	cout << endl;

	cout << "==>>Practice 2_5:\n";
	p2_5();
	cout << endl;

	cout << "==>>Practice 2_6:\n";
	p2_6();
	cout << endl;

	cout << "==>>Practice 2_7:\n";
	p2_7();
	cout << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值