ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ Primer Plus 编程练习3

2021-05-24 15:57:30  阅读:197  来源: 互联网

标签:cout int double C++ seconds Plus inch Primer minute


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

#include "iostream"
using namespace std;

const int inch_to_feet = 12;

int main()
{
    cout << "输入你的身高(英寸):___\b\b\b";
    int inch;
    cin >> inch;
    int feet = inch / inch_to_feet;
    inch = inch % inch_to_feet;
    cout << "你的身高为:" << feet << "英尺加上" << inch << "英寸" << endl;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
using namespace std;

const int trans = 12;
const double inch_to_meter = 0.0254;
const double pound_to_kilogram = 2.2;

int main()
{
    cout << "输入身高:" << endl;
    cout << "输入英尺部分:_\b";
    int feet;
    cin >> feet;
    cout << "输入英寸部分:_\b";
    int inch;
    cin >> inch;
    cout << "输入体重(磅):_\b";
    double pound;
    cin >> pound;
    int height;
    height = feet * trans + inch;
    double meter;
    meter = height * inch_to_meter;
    double kilogram;
    kilogram =pound / pound_to_kilogram;
    cout << endl;
    cout << "你的身体数据如下:" << endl;
    cout << "身高:" << height << "英尺,即" << meter << "米\n";
    cout << "体重" << kilogram << "千克\n";
    double BMI;
    BMI = kilogram / (meter * meter);
    cout << "你的体重指数为:"<<BMI << endl;
    return 0;
}

在这里插入图片描述

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

const int degree_to_minutes = 60;
const int minutes_to_seconds = 60;

int main()
{
    cout << "Enter a latitude in degrees,minutes,and seconds:\n";
    cout<<"First,enter the degrees:";
    int degree;
    cin>>degree;
    cout<<"Next,enter the minutes of arc:";
    int minute;
    cin>>minute;
    cout<<"Fianlly,enter the seconds of arc:";
    int second;
    cin>>second;
    double show_in_degree;
    show_in_degree=(double)degree+(double)minute/degree_to_minutes+(double)second/degree_to_minutes/minutes_to_seconds;
    cout<<degree<<" degrees,"<<minute<<" minutes,"<<second<<" seconds = "<<show_in_degree<<" degrees\n";
    return 0;
}

在这里插入图片描述

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

Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
#include "iostream"
using namespace std;

const int day_to_hour = 24;
const int hour_to_minute = 60;
const int minute_to_second = 60;

int main()
{
    cout << "Enter the number of seconds: ";
    long seconds;
    cin >> seconds;
    int day, hour, minute,second;
    day = seconds / minute_to_second / hour_to_minute / day_to_hour;
    hour = seconds / minute_to_second / hour_to_minute % day_to_hour;
    minute = seconds / minute_to_second % hour_to_minute;
    second = seconds % minute_to_second;
    cout << seconds << " seconds = " << day << " days," << hour << " hours," << minute << " minutes," << second << " seconds\n";
    return 0;
}

在这里插入图片描述

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

int main()
{
    cout << "Enter the world population:__\b\b";
    long long world_population;
    cin >> world_population;
    cout << "Enter the population of the US:__\b\b";
    long long US_population;
    cin >> US_population;
    double proportion;
    proportion = (double)US_population / (double)world_population * 100;
    cout << "The population of the US is "<<proportion <<"% of the world population."<<endl;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
using namespace std;

int main()
{
    cout << "输入耗油量(加仑): ";
    double gasoline;
    cin >> gasoline;
    cout << "输入已经开完的路程(英里): ";
    int distance;
    cin >> distance;
    cout << "你的车一加仑油可以开" << distance / gasoline << "英里" << endl;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
using namespace std;

int main()
{

    cout<<"输入欧洲风格的耗油量计算\n"<<"(liters per 100 kilometers):";
    double Euro_style;
    cin>>Euro_style;
    cout<<"转化为(miles per gallon):"<<endl;
    cout<<Euro_style<<" L/100Km = "<<62.14*3.785/Euro_style<<" mpg\n";
    return 0;
}

在这里插入图片描述

标签:cout,int,double,C++,seconds,Plus,inch,Primer,minute
来源: https://blog.csdn.net/qq_45824568/article/details/117166797

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有