C++ Primer Plus (第6版)习题之开始学习C++

习题2.2

#include<iostream>
using namespace std;

int main()
{
	const int Long_of_Yard = 220;
	int LongDistance;
	cout << "Please enter a distance (long): ";
	cin >> LongDistance;
	cout << LongDistance << " long is equal to " << Long_of_Yard * LongDistance << " Yard\n";
	return 0;
}

执行结果:

Please enter a distance (long): 25
25 long is equal to 55500 Yard

习题2.3

#include<iostream>
using namespace std;

void PrintFunction1();
void PrintFunction2();

int main()
{
	PrintFunction1();
	PrintFunction1();
	PrintFunction2();
	PrintFunction2();
	return 0;
}

void PrintFunction1()
{
	cout << "Three blind mice\n";
}

void PrintFunction2()
{
	cout << "See how they run\n";
}

习题2.4

#include<iostream>
using namespace std;

int main()
{
	const int Year_of_Months = 12;
	int age;
	cout << "Enter your age: ";
	cin >> age;
	cout << age << " years is equal to " << age * Year_of_Months << " months\n";
	return 0;
}

执行结果为

Enter your age: 25
25 years is equal to 300 months

习题2.5

#include<iostream>
using namespace std;

double CelsiusTransFahren(double);
int main()
{
	double Celsius, Fahrenheit;
	cout << "Please enter a Celsius values: ";
	cin >> Celsius;
	Fahrenheit = CelsiusTransFahren(Celsius);
	cout << Celsius << " degrees Celsius is " << Fahrenheit << " degrees Fahrenheit.\n";
	return 0;
}

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

执行结果

Please enter a Celsius values: 20
20 degrees Celsius is 68 degrees Fahrenheit.

习题2.6

#include<iostream>
using namespace std;

double LightTransAstronmical(double);
int main()
{
	double LightYears, AstronmicalUints;
	cout << "Enter the number of light years: ";
	cin >> LightYears;
	AstronmicalUints = LightTransAstronmical(LightYears);
	cout << LightYears << " light years = " << AstronmicalUints << " astronmical units.\n";
	return 0;
}

double LightTransAstronmical(double years)
{
	return years * 63240;
}

执行结果

Enter the number of light years: 4.2
4.2 light years =  265608 astronmical units.

习题2.7

#include<iostream>
using namespace std;

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

void TimeShow(int Hours, int Minutes)
{
	cout << "Time: " << Hours << ":" << Minutes << endl;
}

执行结果

Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值