C++ Primer Plus第三章编程练习

2024-09-03 04:32

本文主要是介绍C++ Primer Plus第三章编程练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C++ Primer Plus第三章编程练习

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1

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

#include <iostream>const int translation = 12;
int main()
{using namespace std;int len = 0, inch = 0;double foot = 0;cout<<"Please input your height(inch):____\b\b\b\b";cin>>len;foot = (double)len / (double)translation;cout<<"Your height in inch :"<<len<<endl;cout<<"Your height in foot_inch :"<<foot<<endl;return 0;
}

2

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

#include <iostream>const float inch_metre = 0.0254;
const float foot_inch = 12;
const float kg_pound = 2.2;int main()
{using namespace std;float height_foot, height_inch, height_metre, weight_pound, weight_kg, BMI;cout<<"Please input the foot:____\b\b\b\b";cin>>height_foot;cout<<"Please input the inch:____\b\b\b\b";cin>>height_inch;height_metre = (height_foot * foot_inch + height_inch) * inch_metre;cout<<"Please input the weight:____\b\b\b\b";cin>>weight_pound;weight_kg = weight_pound / kg_pound;BMI = weight_kg / (height_metre * height_metre);cout<<"The BMI = "<<BMI<<endl;return 0;
}

3

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

#include <iostream>const int tranlation = 60;
using namespace std;
int calculate_degree(float degrees, float minutes, float seconds)
{float result;result = degrees + minutes / tranlation + seconds / tranlation / tranlation;cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds = "<<result<<" degrees."<<endl;return 0;
}int main()
{float degrees, minutes, seconds;cout<<"Enter a latitude in degrees, minutes, and seconds:"<<endl;cout<<"First, enter the degrees:___\b\b\b";cin>>degrees;cout<<"Next, enter the minutes of arc:___\b\b\b";cin>>minutes;cout<<"Finally, enter the seconds of arc:___\b\b\b";cin>>seconds;calculate_degree(degrees, minutes, seconds);return 0;
}

下图为输出结果:
输出结果

4

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

#include <iostream>const int day2hour = 24;
const int hour2min = 60;
const int min2second = 60;int main()
{using namespace std;long long input_seconds = 0, temp = 0;int day = 0, hour = 0, min = 0, seconds = 0;cout<<"Enter the number of seconds:____\b\b\b\b";cin>>input_seconds;day = input_seconds / (day2hour*hour2min*min2second);temp = input_seconds - day*day2hour*hour2min*min2second;hour = temp / (hour2min*min2second);temp = temp - hour * hour2min * min2second;min = temp / min2second;seconds = temp - min*min2second;cout<<input_seconds<<" seconds = "<<day<<" days, "<<hour<<" hours, "<<min<<" minutes, "<<seconds<<" seconds."<<endl;return 0;
}

下图为输出结果:
输出结果

5

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

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

6

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

#include <iostream>int main()
{using namespace std;double mile, gallon, km, litre;cout<<"Enter the mile of distance:____\b\b\b\b";cin>>mile;cout<<"Enter the gas of gallon:____\b\b\b\b";cin>>gallon;cout<<"Your can run "<<mile/gallon<<" mile with one gallon gas."<<endl;cout<<"Enter the km of distance:____\b\b\b\b";cin>>km;cout<<"Enter the gas of litre:____\b\b\b\b";cin>>litre;cout<<"100 km cost "<<100*litre/km<<"L gas."<<endl;return 0;
}

7

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

#include <iostream>int main()
{using namespace std;double Euro_style, US_style;cout<<"Enter the European style(liters per 100km):____\b\b\b\b";cin>>Euro_style;US_style = 62.14 * 3.875 / Euro_style;cout<<"Trans to US style(miles per gallon): "<<US_style<<" mpg"<<endl;return 0;
}

这篇关于C++ Primer Plus第三章编程练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132027

相关文章

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

mybatis-plus如何根据任意字段saveOrUpdateBatch

《mybatis-plus如何根据任意字段saveOrUpdateBatch》MyBatisPlussaveOrUpdateBatch默认按主键判断操作类型,若需按其他唯一字段(如agentId、pe... 目录使用场景方法源码方法改造首先在service层定义接口service层接口实现总结使用场景my

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应