(二)《C++ Primer Plus》 | 第 3 章:处理数据

(1)编写 C++ 程序,要求用户输入表示身高的整数,单位为英寸,然后将身高转换为英尺。使用下划线来指示输入位置,并输出身高。

  • 在 cout 中插入 \b 表示退格,本题使用退格将光标退至横线最开始。
#include <iostream>

using namespace std;

int main()
{
    // 转换因子,1 英尺等于 12 英寸
    const int c = 12;

    // \b 为退格符
    cout << "Please enter your height as an integer inches: _____\b\b\b\b\b";
    int h;
    cin >> h;
    cout << "Your height are " << h << " inches." << endl;
    cout << "Your height are " << float(h) / 12 << " feet." << endl;
    return 0;
}

(2)编写 C++ 程序,输入身高和体重,并返回体重指数 BMI。

  • BMI 的计算公式:体重(千克)除以身高(米)的平方。
#include <iostream>

using namespace std;

int main()
{
    // 转换因子,1 英尺等于 12 英寸
    const int c1 = 12;

    // 转换因子,1 英寸等于 0.0254 米
    const float c2 = 0.0254;

    // 转换因子,1 千克等于 2.2 磅
    const float c3 = 2.2;

    cout << "Please enter your height as an integer in feet and inches: ";
    int h1, h2;
    cin >> h1 >> h2;
    cout << "Please enter your weight as an integer in pounds: ";
    int p;
    cin >> p;

    // 身高,以米为单位
    float hs = (12 * h1 + h2) * c2;
    cout << "Your height are " << hs << " meters." << endl;

    // 体重,以千克为单位
    float ps = float(p) / 2.2;
    cout << "Your weight are " << ps << " kg." << endl;

    cout << "Your BMI are " <<  ps  / (hs * hs) << "." << endl;
    return 0;
}

(3)编写 C++ 程序,要求用户输入以度、分、秒的形式表示的维度,并以度的形式输出。

#include <iostream>

using namespace std;

int main()
{
    // 1 度等于 60 分,1 分等于 60 秒
    const int c = 60;

    cout << "Enter a latitude in degrees, minutes, and seconds: " << endl;
    cout << "First, enter the degrees: ";
    int d;
    cin >> d;
    cout << "Next, enter the minutes of arc: ";
    int m;
    cin >> m;
    cout << "Finally, enter the seconds of arc: ";
    int s;
    cin >> s;

    float dms = d + float(m) / 60 + float(s) / 3600;
    cout << d << " degrees, " << m << " minutes, " << s << " seconds = " << dms << " degrees." << endl;
    return 0;
}

(4)编写 C++ 程序,要求用户输入秒数,然后以天、小时、分钟和秒的方式输出。

#include <iostream>

using namespace std;

int main()
{
    // 1 天等于 24 小时
    const int d2h = 24;

    // 1 小时等于 60 分,1 分等于 60 秒
    const int c = 60;

    cout << "Enter the number of seconds: ";
    long seconds;
    cin >> seconds;

    // 天
    int days = seconds / 3600 / 24;

    // 小时
    int hours = (seconds - days * 24 * 3600) / 3600;

    // 分
    int minutes = (seconds - days * 24 * 3600 - hours * 3600) / 60;


    cout << seconds << " seconds = " << days << " days, " << hours << " hours, " \
         << minutes << " minutes, " << seconds - days * 24 * 3600 - hours * 3600 - minutes * 60 << " seconds." << endl;
    return 0;
}

(5)编写 C++ 程序,要求用户输入全球当前人口和美国当前人口,并输出美国人口所占比例。

#include <iostream>

using namespace std;

int main()
{
    cout << "Enter the world's population: ";
    long wp;
    cin >> wp;
    cout << "Enter the population of the US: ";
    long up;
    cin >> up;
    cout << "The population of the US is " << float(up) / wp * 100 << "\% of the world population." << endl;
    return 0;
}

(6)编写 C++ 程序,要求用户输入驱车里程和使用汽油量,输出汽车耗油量为一加仑的里程。

#include <iostream>

using namespace std;

int main()
{
    cout << "Enter the driving distance in miles: ";
    int miles;
    cin >> miles;
    cout << "Enter fuel consumption in gallons: ";
    int gallons;
    cin >> gallons;
    cout << "The mileage for a gallon of fuel consumption are " << float(gallons) / miles << endl;
    return 0;
}

(7)编写 C++ 程序,要求用户输入汽车每 100 公里的耗油量,然后输出汽车每耗油一加仑能跑多少英里。

#include <iostream>

using namespace std;

int main()
{
    // 100 公里等于 62.14 英里
    const float c1 = 62.14;

    // 1 加仑等于 3.875 升
    const float c2 = 3.875;

    // 100 公里的耗油量(升)
    cout << "Please enter the car's fuel consumption per 100 kilometers: ";
    float littre_per_100_kms;
    cin >> littre_per_100_kms;

    // 1 英里的耗油量(升)
    float littre_per_mile = littre_per_100_kms / c1;

    // 1 英里的耗油量(加仑)
    float gallon_per_mile = littre_per_mile / c2;

    // 输出每耗费一加仑的距离
    cout << "The distance per gallon consumed are " << 1 / gallon_per_mile << "." << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值