栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

第一题

题目描述

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

代码片段
#include 

using namespace std;

int main()
{
    cout<<"Please enter your height using inch as unit:";int c;cin>>c;
    const int inchtofoot=12;
    cout<<"Your height is "< 


第二题

题目描述

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

代码片段
#include 

using namespace std;

int main()
{
    const int chicun=12;
    const float kg=2.2;
    const float cunmi=0.0254;
    int chi;int cun;
    cout<<"Please enter your height:___foot ___inchnfoot: ";cin>>chi;
    cout<<"inch: ";cin>>cun;
    cun=chi*chicun+cun;
    float m=cun*cunmi;
    float bang;cout<<"Please enter your weight using pound as unit: ";cin>>bang;
    bang/=kg;
    double BMI=bang/m/m;
    cout<<"Your BMI is "< 

在此注意,良好的代码风格要求使用含义明确的标识符,千万不要像我这个一样,到最后搞不清楚哪个标识符是哪个含义。



第三题

题目描述

编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。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 

using namespace std;

int main()
{
    const int conver=60;
    int degree,minute,second;
    cout<<"Enter a latitude in degrees, minutes, and seconds:nFirst, enter the degrees:";cin>>degree;
    cout<<"Next, enter the minutes of arc: ";cin>>minute;
    cout<<"Finally, enter the seconds of arc: ";cin>>second;
    double result=(degree*conver*conver+minute*conver+second)/1.0/conver/conver;
    cout< 


第四题

题目描述

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

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

代码片段
#include 

using namespace std;

int main()
{
    int dtoh=24;
    int hmsc=60;
    long second;
    cout<<"Enter the number of seconds: ";cin>>second;
    long secondpre=second,minute,hour,day;
    minute=second/hmsc;
    second%=hmsc;
    hour=minute/hmsc;
    minute%=hmsc;
    day=hour/dtoh;
    hour%=dtoh;
    cout< 


第五题

题目描述

编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信 息存储在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 

using namespace std;

int main()
{
    long long all,part;
    cout<<"Enter the world's population: ";cin>>all;
    cout<<"Enter the population of the US: ";cin>>part;
    cout<<"The population of the US is "< 


第六题

题目描述

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

代码片段
#include 

using namespace std;

int main()
{
    float mile,gallon;
    cout<<"Please enter the miles: ";cin>>mile;
    cout<<"Please enter the gallon: ";cin>>gallon;
    cout<<"The car runs "<>km;
    cout<<"Please enter the L: ";cin>>L;
    cout<<"The car costs "< 


第七题

题目描述

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

注意: 此处课本上应该是写错了,这里应该是27mpg大约合8.71/100km而不是127.

代码片段
#include 

using namespace std;

int main()
{
    float icon1;
    cout<<"Please enter the cost of gas in European style: ";cin>>icon1;
    double icon2=62.14/(icon1/3.875);
    cout<<"The distance of the cost of gas is "<

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958029.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号