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

相关文章

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二