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通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

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方法。右键项目的属性: