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++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

C++读写word文档(.docx)DuckX库的使用详解

《C++读写word文档(.docx)DuckX库的使用详解》DuckX是C++库,用于创建/编辑.docx文件,支持读取文档、添加段落/片段、编辑表格,解决中文乱码需更改编码方案,进阶功能含文本替换... 目录一、基本用法1. 读取文档3. 添加段落4. 添加片段3. 编辑表格二、进阶用法1. 文本替换2

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详