c++ primer plus 习题 capture2

该系列C++代码实现了不同单位之间的转换,包括将长距离转换为码,定义并调用函数,年龄转换为月,华氏温度转换为摄氏温度,光年转换为天文单位,以及输出时间。每个程序都包含输入、计算和输出相应转换结果的逻辑。
摘要由CSDN通过智能技术生成

//2.7.2transformLongToCode

#include <iostream>

int main() {
	using namespace std;
	cout << "Please enter a distance in long: ";
	int longDistance;
	cin >> longDistance;
	cout << longDistance << " long = " << longDistance * 220 << " yard ";
	cout << endl;

	return 0;
}

//2.7.3defineFunction

#include <iostream>

void function1();
void function2();		// function prototype

using namespace std;

int main() 
{
	function1();
	function1();
	function2();
	function2();

	return 0;
}

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

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

//2.7.4transformAgeToMonth

#include <iostream>

int main()
{
	int age;
	std::cout << "Enter your age : ";
	std::cin >> age;
	std::cout << "Your age in month is " << age * 12 << "." << std::endl;
	return 0;
}

//2.7.5transformFahrenheitToCelsius

#include <iostream>
double fahrenheit(double);
using namespace std;

int main() {
	double celsius;
	cout << "Please enter a Celsius value: ";
	cin >> celsius;
	cout << celsius 
		<< " degrees Ceisius is " 
		<< fahrenheit(celsius) 
		<< " degrees Faherenheit.";
	return 0;
}

double fahrenheit(double celsius) {
	double fahrenheit;
	fahrenheit = 1.8 * celsius + 32.0;
	return fahrenheit;
}

// 2.7.6trancformLightYearToAU

#include <iostream>
double transformLightYearToAU(double);

int main()
{
	double lightYear;
	std::cout << "Enter thr number of light year: ";
	std::cin >> lightYear;
	std::cout << lightYear << " light year = " 
		<< transformLightYearToAU(lightYear) << " astronomical units." 
		<< std::endl;
		return 0;
}

double transformLightYearToAU(double lightYear)
{
	double AU;
	AU = lightYear * 63240.0;
	return AU;
}

//2.7.7outputTime

#include <iostream>
void outputTime(int, int);

using namespace std;

int main()
{
	int hour, min;
	cout << "Enter the number of hours: ";
	cin >> hour;
	cout << "Enter the number of minutes: ";
	cin >> min;
	outputTime(hour, min);
	return 0;
}

void outputTime(int hour, int min)
{
	cout << "Time: " << hour << ":" << min;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值