C++ Primer Plus第二章课后编程答案

1、

#include<iostream>

using namespace std;

int main()
{
	cout << "我的名字是:帅哥" << endl;
	cout << "我家的地址是:地球" << endl;
	return 0;
}

输出为: 

2、 

#include<iostream>

using namespace std;

int main()
{
	int dis;
	cout << "请输入以long为单位的距离:";
	cin >> dis;
	cout << "把单位转换为码:" << 220 * dis << endl;
	return 0;
}

输出为:

3、

#include<iostream>
using namespace std;
int first()
{
	cout << "Three blind mice" << endl;
	return 0;
}
int second()
{
	cout << "See how they run" << endl;
	return 0;
}
int main()
{
	first();
	first();
	second();
	second();
	return 0;
}

输出为:

 

4、

#include <iostream>
using namespace std;
int main()
{
	int age;            //定义变量age存储年龄
	cout << "请输入您的年龄:";        //打印提示信息
	cin >> age;         //输入年龄 
	cout << "您的年龄包含" << age * 12 << "个月" << endl;   //打印结果
	return 0;
}

输出为:

扫描二维码关注公众号,回复: 9339537 查看本文章

 

 5、

#include <iostream>
using namespace std;

int Huashi(double c)
{
	double h;
	h = 1.8 * c + 32.0;
	return h;
}
int main()
{
	double c1;
	cout << "Please enter a Celsius value:";
	cin >> c1;
	cout << endl;
	cout << c1 << " degrees Celsius is " << Huashi(c1) << " degrees Fahrenheit." << endl;
	return 0;
}

 输出为:

6、

#include <iostream>
using namespace std;
double change(double n);//函数原型
int main()
{
	cout << "Enter the number of light years: ";        //打印提示信息
	double ly;         //定义变量,存储光年
	cin >> ly;          //输入数据
	cout << ly << " light years = " << change(ly) << " astronomical units." << endl;    //打印结果
	return 0;
}

double change(double n)        //定义函数
{
	return n * 63240;
}

输出为: 

7、

#include <iostream>
using namespace std;
void print_time(int h, int m);  //函数原型
int main()
{
	cout << "Enter the number of hours: ";    //打印提示信息
	int hours;  //定义变量,存储小时数
	cin >> hours;    //输入小时数
	cout << "Enter the number of minutes: ";     //打印提示信息
	int minutes;         //定义变量,存储分钟数
	cin >> minutes;         //输入分钟数
	print_time(hours, minutes);      //调用函数
	return 0;
}

void print_time(int h, int m)   //定义函数
{
	cout << "Time: " << h << ":" << m << endl;
}

输出为:

发布了139 篇原创文章 · 获赞 49 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/xiewenrui1996/article/details/103304978