C++ primer plus (第六版) 编程练习第二章答案

C++ primer plus (第六版) 编程练习第二章答案

//第一题
#include<iostream>
using namespace std;
int main()
{
	string name, address;
	cout << "请输入您的名字:";
	cin >> name;
	cout << "请输入您的地址:";
	cin >> address;
	cout << "姓名:" << name << endl;
	cout << "地址:" << address << endl;
	return 0;
}
//第二题
#include<iostream>
#define length_long 220
using namespace std;
int main()
{
	float distance;
	cout << "请输入距离(单位:long):";
	cin >> distance;
	cout << "转换为码后为:" << distance * length_long << endl;
	return 0;
}
//第三题
#include<iostream>
using namespace std;
void show01();//函数定义
void show02();//被调用用于输出的函数
int main()
{
	for (int i = 0; i < 2; i++)
	{
		show01();
	}

	for (int i = 0; i < 2; i++)
	{
		show02();
	}
	
	return 0;
}

void show01()
{
	cout << "Three blind mice" << endl;
}
void show02()
{
	cout << "See how they run" << endl;
}
//第四题
#include<iostream>
using namespace std;
int main()
{	
	int age;
	cout << "Enter your age:";
	cin >> age;
	cout << "Your age in months is " << age * 12 << endl;
	return 0;
}
//第五题
#include<iostream>
using namespace std;
void tem_convert(float celsius);//用于将摄氏温度转化为华氏温度
int main()
{	
	float celsius;//摄氏度
	cout << "Please enter a Celsius value: ";
	cin >> celsius;
	tem_convert(celsius);//调用函数转化为华氏度
	return 0;
}
void tem_convert(float celsius)
{
	cout << celsius << "degrees Celsius is " << celsius * 1.8 + 32.0 << " degrees Fahrenheit.";
}
//第六题
#include<iostream>
constexpr auto ast_unit = 63240;
using namespace std;
void cal_distance(float light_year);
int main()
{	
	float l_year;//将要输入的光年值
	cout << "Enter the number of light years:";
	cin >> l_year;
	cal_distance(l_year);
	return 0;
}
void cal_distance(float light_year)
{
	cout << light_year << " light years = " << light_year * ast_unit << " astronomical units.";
}
//第七题
#include<iostream>
using namespace std;
void show_time(int h, int m);
int main()
{	
	int hour, minute;
	cout << "Enter the number of hours:";
	cin >> hour;
	cout << "Enter the number of minutes:";
	cin >> minute;
	show_time(hour, minute);
	return 0;
}
void show_time(int h, int m)
{
	cout << "Time: " << h << ":" << m << endl;
}

注:以上代码均可在VS2019中正常运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值