C++ Primer Plus 第六版 第三章课后编程练习答案

1.

#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter height:_____\b\b\b\b\b";
    //题目要求使用下划线字符来指示输入位置,就是要求你打出下划线,然后使用“\b”字符来使光标回退到下划线的最前端
    int h;
    cin >> h;
    const int In_per_Ft = 12;
    //const常量: 其值在程序运行的过程中不会发生改变,但是定义常量的时候可以设置初始值
    int ft,in;
    ft = h / In_per_Ft;
    in = h % In_per_Ft;
    cout << "Ft = "<<ft<<" ,In = "<<in<<endl;
    return 0;
}

2.

#include <iostream>
int main()
{
        using namespace std;
        cout << "Enter  feet: ____\b\b\b\b";
        int feet;
        cin >> feet;
        cout << "Enter  inch: ____\b\b\b\b";
        int inch;
        cin >> inch;
        cout << "Enter  weight: ____\b\b\b\b";
        int weight;
        cin >> weight;
        cout << "Feet: " << feet << " ,Inch: " << inch << " ,Weight: " << weight << endl;
        int height_inch;
        const int In_per_Ft = 12;
        height_inch = feet * In_per_Ft + inch;
        const double In_per_M = 0.0254;
        double height_meter;
        height_meter = height_inch * In_per_M;
        const double Kg_per_p = 2.2;
        double weight_kg;
        weight_kg = weight / Kg_per_p;
        cout << "That means you are " << height_meter << " meters height and " << weight_kg << " kilograms weight." << endl;
        double BMI;
        BMI = weight_kg / (height_meter * height_meter);
        cout << "And your BMI is " << BMI << endl;
    return 0;
}

3.

//以度为输出,就是把分和秒都换算为度
#include <iostream>

int main()
{
        using namespace std;

        cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
        cout << "First, enter the degrees:__\b\b";
        int degree;
        cin >> degree;
        cout << "Next, enter the minutes:__\b\b";
        int minute;
        cin >> minute;
        cout << "Finally, enter the seconds:__\b\b";
        int second;
        cin >> second;

        const int Count = 60;
        double latitude;
// 错误  latitude = degree + double (minute/Count) + double (second/Count/Count);  注:minute/Count直接得到的是int,再加double也没意义了
        latitude = degree + double (minute)/Count + double (second)/Count/Count;
        cout << degree << " degrees, " << minute << " minute, " << second << " seconds = " << latitude << " degrees" << endl;
    return 0;
}

4. 

#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter the number of seconds: ";
    long second;
    cin >> second;
    const int Min_per_sec = 60;
    const int H_per_min = 60;
    const int D_per_h = 24;
    int s, d, h, min;
    s = second % Min_per_sec;
    int convert;
    convert = second / Min_per_sec;
    min = convert % H_per_min;
    convert = convert / H_per_min;
    h = convert % D_per_h;
    d = convert / D_per_h;
    cout << second << " seconds = " << d << " days, " << h << " hours, " << min << " minutes, " << s << " seconds" << endl;
    return 0;
}

5.

#include <iostream>

int main()
{
    using namespace std;

    cout << "Enter the world's population:__________\b\b\b\b\b\b\b\b\b\b";
    long long wp;
    cin >> wp;
    cout << "Enter the population of the US:__________\b\b\b\b\b\b\b\b\b\b";
    long long usp;
    cin >> usp;
    double percent;
// 亦可   percent = double (usp) /  wp * 100;
    percent = double (usp) / double (wp) * 100;
    cout << "The population of the US is " << percent << "% of the world population." << endl;
    return 0;
}

6.

#include<iostream>

int main() {
    using namespace std;
    float miles, gallons;
    cout << "nter the distance(mile): ";
    cin >> miles;
    cout << "Please enter the gasoline amount(gallon): ";
    cin >> gallons;
    cout << "Wasting a gallon gasoline run " << miles / gallons << " miles.\n";
    int number;
    cout << "Do you want to know how much oil is consumed per 100 kilometers?"
            " if you want, please enter 1: ";
    cin >> number;
    if (number == 1) {
        float kilometers, liter;
        cout << "Please enter the distance(kilometer):";
        cin >> kilometers;
        cout << "Please enter the gasoline amount(liter):";
        cin >> liter;
        cout << "Wasting gasoline per 100 kilometers is " << liter / kilometers * 100
             << " liters." << endl;
    }
    return 0;
}

7.

#include <iostream>

int main()
{
        using namespace std;

        cout << "Enter  kilometers:";
        float kilometer;
        cin >> kilometer;
        cout << "Enetr your litros:";
        float litro;
        cin >> litro;
        float eux;
        eux = litro / (kilometer / 100);
        cout << "Every 100 kilometers cost " << eux << " litros gas in Europe." << endl;
        const float Mile_per_100kilometers = 62.14;
        float mile;
        mile = (kilometer / 100) * Mile_per_100kilometers;
        const float Calon_per_litro = 3.875;
        float calon;
        calon = litro / Calon_per_litro;
        float usx;
        usx = mile / calon;
        cout << "That means you drive " << usx << " miles  using 1 calon gas in the US." << endl;
        cout << eux << "/100km = " << usx << "mpg" << endl;

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值