0
点赞
收藏
分享

微信扫一扫

C++ Primer Plus(第6版)中文版 第三章学习课后题代码答案

生活记录馆 2022-03-12 阅读 58
c++学习

第三章学习开始学习,课后题代码答案

#include <iostream>
using std::cout;
using std::cin;

const int Foot2Inch = 12;
void practice1(void)
{
    cout << "Input your height in inch :____\b\b\b";
    int inch, Inch, Foot = 0;
    cin >> inch;
    Foot = inch / Foot2Inch;
    Inch = inch % Foot2Inch;
    cout << "Your height is " << Foot << " foot " << Inch << " inch " << std::endl;
    return;
}

void practice2(void)
{
    const double Inch2Meter = 0.0254;
    const double KG2P = 2.2;
    cout << "Enter your height in foot and inch\n"
        << "Enter the foot : ";
    double Hfoot, Hinch, Hmeter = 0.0, Wpound, Wkg = 0.0, BMI = 0.0;
    cin >> Hfoot;
    cout << "Enter the inch : ";
    cin >> Hinch;
    cout << "Enter the weight in pound: ";
    cin >> Wpound;
    Hmeter = (Hfoot * Foot2Inch + Hinch) * Inch2Meter;
    Wkg = Wpound / KG2P;
    BMI = Wkg / (Hmeter * Hmeter);
    cout << "Your BMI is " << BMI << std::endl;
    return;
}

void practice3(void)
{
    int degrees, minutes, seconds;
    double Degrees = 0.0;
    cout << "Enter a latitude in degrees, minutes , and seconds\n"
        << "Enter the degrees: ";
    cin >> degrees;
    cout << "Enter a minutes of arc: ";
    cin >> minutes;
    cout << "Enter a seconds of arc: ";
    cin >> seconds;
    Degrees = minutes / 60.0 + seconds / 3600.0;
    cout << degrees << " degrees, "
        << minutes << " minutes, "
        << seconds << " seconds = "
        << degrees + Degrees << " degrees, " << std::endl;
    return;
}

void practice4(void)
{
    long long totalSeconds;
    cout << "Enter the number of seconds: ";
    cin >> totalSeconds;
    int days = 0, hours = 0, minutes = 0, seconds = 0;
    seconds = totalSeconds % 60;
    minutes = (totalSeconds % (60 * 60)) / 60;
    hours = (totalSeconds % (60 * 60 * 24)) / (60 * 60);
    days = totalSeconds / (60 * 60 * 24);
    cout << totalSeconds << " seconds = "
        << days << " days, "
        << hours << " hours, "
        << minutes << " minutes, "
        << seconds << " seconds\n";
    return;
}

void practice5(void)
{
    long long worldPopulation,USpopulation; 
    cout << "Enter the world's population: ";
    cin >> worldPopulation;
    cout << "Enter the population of the US: ";
    cin >> USpopulation;
    double rate = 0.0;
    rate = double(USpopulation) / double(worldPopulation);
    cout << "The population of the US is " << rate * 100 << "% of the world population.\n";
    return;
}

void practice6(void)
{
    cout << "Enter the distance in kilometer:";
    double kilometers, liter, oil = 0.0;
    cin >> kilometers;
    cout << "Enter the comsumption of oil in liter:";
    cin >> liter;
    oil = kilometers / liter;
    cout << "Average fuel comsumption : " << oil << " (kilometer/liter)\n";
    return;
}

void practice7(void)
{
    double comsumptionUS = 0.0, comsumptionEU;
    cout << "Enter the fuel comsumption in EU:";
    cin >> comsumptionEU;
    comsumptionUS = comsumptionEU / 19 * 12.41;
    cout << "the fuel comsumption in US standard is " << comsumptionUS << " L / 100 KM\n";
    return;
}

int main()
{
    practice7();
    return 0;
}

自写代码仅供参考,如有问题麻烦请指出谢谢!

举报

相关推荐

0 条评论