您的位置:首页 > 编程语言 > C语言/C++

c++ primer plus(第6版)中文版 第三章编程练习答案

2014-05-02 22:01 871 查看

第三章编程练习答案

3.1编写一个程序,要求输入身高(__英寸),转换为英尺英寸

//3.1编写一个程序,要求输入身高(__英寸),转换为英尺英寸
#include <iostream>
using namespace std;

int main ()
{
const int	incun = 12;	// 1英尺 = 12英寸
int	inches;
cout << "输入身高(__英寸):";
cin >> inches;
cout << "你的身高为:" << inches / incun << "英尺又" << inches % incun << "英寸";
cout << endl;
}


3.2编写程序,输入身高英尺英寸和体重,计算BMI

//3.2编写程序,输入身高英尺英寸和体重,计算BMI
#include <iostream>
using namespace std;

int main ()
{
double	height, weight, BMI;
int feet, inches;
cout << "输入身高,__英尺又__英寸:";
cin >> feet >> inches;
// 1英尺 = 12英寸,1英寸*0.0254
height = feet * 12+ inches;
height = height * 0.0254;
int	pounds;
cout << "输入以磅为单位的体重:";
cin >> pounds;
// 1磅 = 1/2.2千克
weight = pounds * 1/2.3;
BMI = weight / (height * height);
cout << "BMI:" << BMI<< endl;
}


3.3输入一个纬度(度,分,秒),输出度

//3.3输入一个纬度(度,分,秒),输出度
#include <iostream>
using namespace std;

int main ()
{
int	degrees, minutes, seconds;
cout << "Enter a latitude in degrees, minutes, and seconds." << endl;
cout << "First, enter the degrees: ";
cin >> degrees;
cout << "Next, enter the minutes of arc: ";
cin >> minutes;
cout << "Finally, enter the seconds of arc: ";
cin >> seconds;
cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = "
<< degrees + 1.0*minutes/60 + 1.0*seconds/60/60<< endl;
}


3.4输入秒,输出天,时,分,秒

//3.4输入秒,输出天,时,分,秒
#include <iostream>
using namespace std;

int main ()
{
cout << "Enter the number of seconds: ";
long	secondsTotal;
cin >> secondsTotal;
long	seconds = secondsTotal % 60;
long	minutesTotal = secondsTotal /60;
long	minutes = minutesTotal % 60;
long	hoursTotal = minutesTotal / 60;
long	hours = hoursTotal % 24;
long	days = hoursTotal / 24;
cout << secondsTotal <<  " seconds = " << days << " days, " << hours << " hours, "
<< minutes << " minutes, " << seconds << " seconds" << endl;
}


3.5输入世界和美国人口,计算美国人口百分比

//3.5输入世界和美国人口,计算美国人口百分比
#include <iostream>
using namespace std;

int main ()
{
long long	worldPopulation, usaPopulation;
cout << "Enter the world's population: ";
cin >> worldPopulation;
cout << "Enter the population of the US: ";
cin >> usaPopulation;
cout << "The population of the US is " << 100.0 * usaPopulation / worldPopulation << "% of the world population.";
cout << endl;
}


3.6输入行驶公里数(公里)和耗油量(升),输出油耗(升/100公里)

//3.6输入行驶公里数(公里)和耗油量(升),输出油耗(升/100公里)
#include <iostream>
using namespace std;

int main ()
{
cout << "输入行驶公里数(公里)和耗油量(升):";
double	distance, petrol;
cin >> distance >> petrol;
cout << "油耗为" << 100 * petrol / distance << "(升/100公里)";
cout << endl;
}


3.7转换欧洲耗油量为美式表示

//3.7转换欧洲耗油量为美式表示
#include <iostream>
using namespace std;

int main ()
{
cout << "输入欧洲标准的油耗(升/100公里):";
double	gasConsumptionInEuropean;
cin >> gasConsumptionInEuropean;
const double	k_factorKmToMile = 0.6214;
const double	k_factorLiterToGallon = 1/3.875;
cout << gasConsumptionInEuropean << "(升/100公里)=" << 100 * k_factorKmToMile / (gasConsumptionInEuropean * k_factorLiterToGallon) << "(英里/加仑)";
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: