C++primer plus (第六版)习题答案——第2、3章

c++ primer plus (第六版) 2、3章答案

自己在平板上手敲的代码(部分是在VS上敲的看起来会清楚一点),已经运行过了

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

//3.4  秒数转换//
#include<iostream>
using namespace std;
const int DAY_TO_HOUR = 24;
const int HOUR_TO_MINUTE = 60;
const int MINUTE_TO_SECOND = 60;
int main()
{
	long long seconds;
	int days, hours, minutes;
	cout << "Enter the number of seconds:";
	cin >> seconds;
	cout << seconds << " seconds = ";
	days = seconds / (DAY_TO_HOUR * HOUR_TO_MINUTE * MINUTE_TO_SECOND);
	seconds = seconds % (DAY_TO_HOUR * HOUR_TO_MINUTE * MINUTE_TO_SECOND);
	hours = seconds / (HOUR_TO_MINUTE * MINUTE_TO_SECOND);
	seconds = seconds % (DAY_TO_HOUR* MINUTE_TO_SECOND);
	minutes = seconds / MINUTE_TO_SECOND;
	seconds = seconds % MINUTE_TO_SECOND;
	cout << days << "days, " << hours << " hours," << minutes << " minutes, " << seconds << " seconds." << endl;
	return 0;
}
//3.5 美国人口比例//
#include<iostream>
using namespace std;
int main()
{
	long long  global_amount , american_amount;
	double population_percent;
	cout << "Enter the world's population: ";
	cin >> global_amount;
	cout << "Enter the population of US: ";
	cin >> american_amount;
	population_percent = 100 * (double)american_amount / (double)global_amount;
	cout << "The population of the US is " << population_percent << "% of the world population.";
	return 0;
}
//3.6 汽油损耗单位换算//
#include<iostream>
using namespace std;
int main()
{
	float distance_in_mile, distance_in_km;
	float fuel_in_gallon, fuel_in_litre;
	float fuel_consume;
	cout << "Enter the distance in miles: ";
	cin >> distance_in_mile;
	cout << "Enter the fuel consume in gallon: ";
	cin >> fuel_in_gallon;
	fuel_consume = distance_in_mile / fuel_in_gallon;
	cout << "The fuel consume is " << fuel_consume << " mpg(mile/gallon)." << endl;
	cout << "Enter the distance in kilometer: ";
	cin >> distance_in_km;
	cout << "Enter the fuel consume in litre: ";
	cin >> fuel_in_litre;
	cout << "The fuel consume is " << fuel_consume << "L/100KM." << endl;
	return 0;
}
//3.7 耗油量单位转换//
#include<iostream>
using namespace std;
const float GALLON_TO_LITRE = 3.875;
const float HKM_TO_MILE = 62.14;
int main()
{
	float fuel_consume_eur, fuel_consume_us;
	cout << "Enter the fuel consume in europe(1/100km): ";
	cin >> fuel_consume_eur;
	fuel_consume_us = HKM_TO_MILE / (fuel_consume_eur / GALLON_TO_LITRE);
	cout << "The fuel consume is " << fuel_consume_eur << "L/100KM." << endl;
	cout << "The fuel consume is " << fuel_consume_us << " mpg(mile/gallon)." << endl;
	return 0;
}

🆗敲完了,csdn1上的代码片看起来还挺好看的。冲冲冲!!!2


  1. 之后应该会把整本书的代码放在csdn上 ↩︎

  2. 有错误的话是你错了 ↩︎

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值