C++ Primer Plus 入坑之路——C++ Primer Plus (第6版) 第3章 编程练习题目参考答案

1编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。

#include<iostream>
using namespace std;
int main()
{
const float yingchi = 12;//1英尺=12英寸
float yingcun;
cout << "请输入你的身高(英寸):_";
cin >> yingcun;
cout << "你的身高是 " << yingcun / yingchi << "英尺" << endl;
}

2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(BodyMass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的 BMI--体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。

#include<iostream>
using namespace std;
int main()
{
const float yingchi_t = 12;//1英尺=12英寸
const float shengao_t = 0.0254;//1英寸=0.00254米
const float tizhong_t = 2.2;//1千克=2.2磅
 

float yingcun,yingchi,shengao,tizhong,bang,mi,bmi;

cout << "请输入你的身高(英尺):_";
cin >> yingchi;
cout << "请输入你的身高(英寸):_";
cin >> yingcun;
cout << "请输入你的体重(磅):_";
cin >> bang;
shengao = shengao_t * (yingcun + yingchi_t * yingchi);
tizhong = bang / tizhong_t;
bmi = tizhong / shengao*shengao;
cout << "你的身高是: " << shengao << "米" << endl;
cout << "你的体重是:" << tizhong << "千克" << endl;
cout << "你的BMI指数为:" << bmi << endl;
}


3编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。下面是该程序运行时的情况:

Enter a latitude in degrees, minutes,and seconds: First, enter the degrees:37
Next,enter the minutes of arc:51
Finally, enter the secondsof arc:19
37 degrees,51 minutes19 seconds=37.8553 degrees

#include<iostream>
using namespace std;
int main()
{
const float t = 60;//1度=60分;一分=60秒;


float du, fen, miao;
cout << "输入度:_";
cin >> du;
cout << "输入分:_";
cin >> fen;
cout << "输入秒:_";
cin >> miao;
cout << endl;
cout << du << "度" << fen << "分" << miao << "秒=" << du + (fen + miao / t) / t;
}

4.编写一个程序,要求用户以整数方式输入秒数(使用long或longlong变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:

Enter the number ofseconds:31600000
31600000 seconds = 365 days17 hours,46 minutes,40 seconds

#include<iostream>
using namespace std;
int main()
{
const short min = 60;//一分=60秒;
const short hours = 3600;//一小时=60分=60*60=3600秒
const int day = 86400;// 一天=3600*24=86400秒
long long s;
cout << "Enter the number of seconds:";
cin >> s;
cout << s << "seconds = " << s / day << " days "
<< (s % day) / hours << " hours "
<< ((s % day) % hours) / min << " minutes "
<< ((s % day) % hours) % min << " seconds.";
}

5编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:

Enter the world's population:6898758899 Enter the population of theUS:310783781
The population of the US is 4.504928 of the world population.

#include<iostream>
using namespace std;
int main()
{
long long world, CH, US;
cout << "Enter the world's population:";
cin >> world;
cout << "Enter the population of the China:";
cin >> CH;
cout << "Enter the population of the american:";
cin >> US;
cout << "The population of the China is "
<< ((double)CH / (double)world)*100
<< "% of the world population";
cout << endl;
cout << "The population of the american is "
<< ((double)US / (double)world)*100
<< "% of the world population";
}

6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。

如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果--即每100公里的耗油量(升)。

//计算汽车耗油量为一加仑的里程
#include<iostream>
using namespace std;
int main()
{
float yingli, jialun;
cout << "please enter your 里程数(英里):";
cin >> yingli;
cout << "please enter your use 油量(加仑):";
cin >> jialun;
cout << "your car 一加仑的油可以跑:"
<< yingli/ jialun
<< " 英里。";
}
//计算百公里油耗
#include<iostream>
using namespace std;
int main()
{
float licheng, youliang;
cout << "please enter your 里程数:";
cin >> licheng;
cout << "please enter your use 油量(L):";
cin >> youliang;
cout << "your car 百公里油耗为:"
<< youliang / (licheng / 100)
<< ".L";
}


7.编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量--每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于6214英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合871/100km

#include<iostream>
using namespace std;
int main()
{
const float yingli = 0.6214;//1公里=0.6214英里
const float jialun = 1 / 3.875;//1加仑=3.875升,1升=1/3.875 加仑
float gongli, sheng;
cout << "please enter your 里程数(公里):";
cin >> gongli;
cout << "please enter your use 油量(L):";
cin >> sheng;
//float kml = sheng / (gongli / 100); //百公里油耗数(L)
float mpg;//每加仑英里数
mpg = (gongli * yingli) / (sheng * jialun);//将公里数转换成英里然后除以加仑(升转为加仑)
cout << "your car 的 油耗是"
<< mpg
<< " mpg";

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值