Chapter 3

  • 1. 将身高(单位为英寸) 转化成几英尺几英寸的形式
  • 2. 计算体重指数BMI的值
  • 3. 将“度-分-秒”格式的纬度转换成“度”格式的纬度
  • 4. 将“秒”格式的时间转换成“天-时-分-秒”格式
  • 5. 显示美国人口占全球人口百分比
  • 6. 求出单位耗油量
  • 7. 耗油量欧洲风格转化为美国风格

1. 将身高(单位为英寸) 转化成几英尺几英寸的形式

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

  • 编程前需要的信息:1英尺(foot)=12英寸(inch)
#include <iostream>
using namespace std;
const int inch_feet = 12;int main()
{int height;cout << "Please enter your height in inches:___\b\b\b";cin >> height;int feet = height / inch_feet;int inch = height % inch_feet;cout << "Your height is "<<feet<<" feet,and "<<inch<<" inches\n";return 0;
}

2. 计算体重指数BMI的值

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

#include <iostream>
using namespace std;const int inch_feet = 12;
const double feet_meter = 0.0254;
const double kilo_pound = 2.2;int main()
{int inch, foot;                              //英寸,英尺  double pound;                                //磅cout << "Please enter your height(feet and inches)" << endl;cout << "Please enter feet of your height:___\b\b\b";cin >> foot;cout << "Please enter inches of your height:___\b\b\b";cin >> inch;cout << "Please enter your weight(pound):___\b\b\b";cin >> pound;double inches = foot * inch_feet + inch;     //总英寸(inch)double meteres = inches * feet_meter;        //身高(米)double kilo = pound / kilo_pound;            //体重(kg)double BMI = kilo / (meteres *meteres);cout << "Your BMI:" << BMI << endl;return 0;
}

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 seconds of arc:19
37 degrees,51 minutes,19 seconds =37.8553 degrees

#include <iostream>
using namespace std;const double factor = 60;int main()
{double degrees, minutes, seconds;                //度,分,秒  cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;cout << "First, enter the degrees: __\b\b";cin >> degrees;cout << "Next, enter the minutes of arc: __\b\b";cin >> minutes;cout << "Finally, enter the seconds of arc: __\b\b";cin >> seconds;double result = degrees + minutes / factor + seconds / (factor * factor);cout <<degrees<< " degrees,"<<minutes<<" minutes,"<<seconds<<" seconds = " << result <<" degrees"<< endl;return 0;
}

4. 将“秒”格式的时间转换成“天-时-分-秒”格式

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

Enter the number of seconds:3160000
3160000 seconds = 365 days,17 hours,46 minutes,40 seconds

#include <iostream>
using namespace std;const int d_h = 24;
const int h_m = 60;
const int m_s = 60;int main()
{long long total_seconds = 0;int days ;int hours ;int minutes ;int seconds ;cout << "Enter the total number of seconds: ";cin >> total_seconds;days = total_seconds / (d_h * h_m * m_s);hours = ((total_seconds % (d_h * h_m * m_s)) / (h_m * m_s));minutes = ((total_seconds % (h_m * m_s)) / m_s);seconds = (total_seconds % m_s);cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;return 0;
}

5. 显示美国人口占全球人口百分比

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

Enter the world’s population:6898758899
Enter the population of the US:310783781
The population of the US is 4.50492% of the world population.

#include <iostream>
using namespace std;int main() {long long world_population, US_population;cout << "Enter the world's population:___\b\b\b";cin >> world_population;cout << "Enter the population of the US:___\b\b\b";cin >> US_population;double rate = double(US_population) / double(world_population);cout << "The population of the US is " << rate * 100 << "% of the world population." << endl;return 0;
}

6. 求出单位耗油量

题目:编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果-即每100公里的耗油量(升)。

#include <iostream>
using namespace std;int main() {cout << "Please enter the distances(miles):";double distance;cin >> distance;cout << "Please enter volume of gasoline(gallon):";double volume;cin >> volume;cout << "Average fuel comsuption: " << distance / volume << " miles per gallon" << endl;return 0;
}

7. 耗油量欧洲风格转化为美国风格

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

  • mpg的意思是1加仑的油能跑多少英里
#include <iostream>
using namespace std;int main()
{double EU, US;cout << "Please enter fuel consumption per 100 km(UN):";cin >> EU;US = 1 / EU * 62.14 * 3.875;cout << "Gallons per mile(US) : " << US << endl;return 0;
}

C++ Primer Plus(三)编程练习相关推荐

  1. 跟着c++Primer Plus学编程--- 4.13编程练习答案详解】

    跟着c++Primer Plus学编程--- 4.13编程练习答案详解 1.编写一个C++程序,如下述输出事例所示那样请求并显示信息. 2.修改程序清单4.4,使用C++ string 类而不是 ch ...

  2. C++ Primer 第三版 读书笔记

    1.如果一个变量是在全局定义的,系统会保证给它提供初始化值0.如果变量是局部定义的,或是通过new表达式动态分配的,则系统不会向它提供初始值0 2.一般定义指针最好写成:" string * ...

  3. matlab双极性眼图,实验报告三编程做单极性码眼图仿真

    <实验报告三编程做单极性码眼图仿真>由会员分享,可在线阅读,更多相关<实验报告三编程做单极性码眼图仿真(4页珍藏版)>请在人人文库网上搜索. 1.1.实验目的(1)掌握单极性码 ...

  4. C++ Primer Plus课后编程练习第6章参考代码

    (C++ Primer Plus课后编程练习第6章参考代码) 声明: 作者入门小白,将学习过程中的代码做一些分享,仅供大家参考,欢迎大家交流指正.全部编译运行过,水平有限,不喜勿喷. 环境: Wind ...

  5. C++ Primer Plus P31 编程练习(调用一个用户自定义的函数(以光年值为参数,并返回对应天文单位的值)——中职

    C++ Primer Plus P31 编程练习 第六题 编写一个程序,其main()调用一个用户自定义的函数(以光年值为参数,并返回对应天文单位的值).该程序按下面格式要求输入光年值,并显示结果: ...

  6. C++ primer 第三章 字符串、向量和数组 练习题

    3.1节练习 练习3.1:使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习. 略. 3.2.2 节练习 练习3.2:编写一段程序从标准输入中一次读入一整行,然后修改 ...

  7. [Python黑帽] 三.编程实现IP及端口扫描器、实现多线程C段扫描器

    Python黑帽第三篇文章将分享网络扫描基础知识,编程实现IP及端口扫描器.实现多线程C段扫描器.本文参考了<Python绝技>书籍和i春秋ADO老师的课程内容,这里真心推荐大家去学习ic ...

  8. C++ Primer Plus章节编程练习(第五章)

    第五章 循环和关系表达式 1.编写一个要求用户输入两个整数的程序.该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和.这里假设先输入较小的整数.例如,如果用户输入的是2和9,则程序将指出2 ...

  9. c语言作业 身高预测,C语言的那些题(三) —— 编程计算身高问题

    今天,再和大家分享一道关于编程计算身高的问题. 每个做父母的都关心自己孩子成人后的身高,据有关生理卫生知识与数理统计分析表明,影响小孩成人后身高的因素有遗传.饮食习惯与坚持体育锻炼等.小孩成人后身高与 ...

最新文章

  1. 灰色预测原理及JAVA实现
  2. [Hadoop][笔记]4个节点搭建Hadoop2.x HA测试集群
  3. python datetime timedelta函数_Python Pandas DatetimeIndex.to_perioddelta()用法及代码示例
  4. Shell输入输出重定向:Shell Here Document,/dev/null文件
  5. AWD-LSTM为什么这么棒?
  6. WEB服务器技术名词
  7. Android中Json数据解析
  8. Python学习_进程multiprocessing 多进程 协程
  9. 如何清除以前连接到Mac的WiFi网络
  10. php模板建站seo,phpwin建站教程,phpwind模板
  11. 凸优化第四章凸优化问题 4.5 几何规划
  12. 用FastStone Capture录屏如何发声
  13. dht11 新手原理详解(附代码)
  14. python基础题目练习,购买猕猴桃
  15. Typora高亮颜色设置
  16. Hamilton Jacobi
  17. 记录下UE4解决EQS Bug的过程
  18. 网络劫持是什么?网页被篡改劫持怎么修复(终级方案)网页劫持如何修复?
  19. mocha 测试 mysql_GitHub - nodejs-xx/lei: 整合Express mysql ioredis ejs 的一开发框架,使用mocha对api进行测试...
  20. UTC时间与北京时间

热门文章

  1. 下载!《Nacos 架构与原理》pdf
  2. Android自定义系列——13.Matrix Camera
  3. 数据结构与算法:冒泡排序、插入排序、选择排序
  4. 群晖安装和插件库系列笔记
  5. TC214B直流电机控制芯片
  6. sonic云真机通过linux系统接入苹果手机
  7. 本地编译执行 Kubernetes e2e 测试
  8. 在php中调用java的方法
  9. 详解Python中的文本处理
  10. ADSL/光纤 双拨,多拨