C++PrimerPlus中文第六版第3章编程练习答案

1、

#include<iostream>
using namespace std;
int main()
{
    const int RAT = 14;
    int height_ft, height_in;
    cout << "Please input your height in feets:___\b\b\b";
    cin >> height_ft;
    cout << height_ft << " feets includes " << height_ft/RAT << "inches and "<<height_ft%RAT<<"feets.\n";
    return 0;
    
} 

2、

#include<iostream>
using namespace std;
int main()
{
    const int INCH_PER_FEET = 12; //每英尺12英寸
    const double METER_PER_INCH = 0.0254; //每英寸0.0254米
    const double POUND_PER_KG = 2.2; //每千克2.2磅

    int height_ft, height_in; //身高几英尺几英寸
    cout << "Please input your height in feets and inches:";
    cin >> height_ft >> height_in;
    double height_m = (height_ft * INCH_PER_FEET + height_in) * METER_PER_INCH; //身高(米)
    cout << "Your height is " << height_ft << "feet and " << height_in << "inches, equals to " << height_m << "meters.\n";
    double weight_pd, weight_kg; //体重磅,体重千克
    cout << "Please input your weight in pounds:";
    cin >> weight_pd; 
    weight_kg = weight_pd / POUND_PER_KG;
    double BMI = weight_kg / (height_m * height_m);
    cout << "BMI=" << BMI << endl;
    return 0;

} 

3、

#include <iostream>
using namespace std;

int main()
{
    const int MINUTE_PER_DEGREE = 60;
    const int SECOND_PER_MINUTE = 60;
    int degree, minute, second;
    cout << "Enter a latitude in degrees, miinutes, and seconds:\n";
    cout << "First, enter the degree:";
    cin >> degree;
    cout << "Next, enter the minutes of arc:";
    cin >> minute;
    cout << "Finally, enter the seconds of arc:";
    cin >> second;
    double degrees = degree + (minute + double(double(second) / SECOND_PER_MINUTE)) / MINUTE_PER_DEGREE;
    cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = " << degrees << " degrees.\n";
    return 0;
}

4、

#include <iostream>
using namespace std;

int main()
{
    const int HOURS_PER_DAY = 24;
    const int MINUTES_PER_HOUR = 60;
    const int SECONDS_PER_MINUTE = 60;
    int seconds, day, hour, minute, second;
    cout << "Enter the number of seconds: ";
    cin >> seconds;
    second = seconds % SECONDS_PER_MINUTE;
    minute = (seconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
    hour = (seconds / SECONDS_PER_MINUTE / MINUTES_PER_HOUR) % HOURS_PER_DAY;
    day = seconds / SECONDS_PER_MINUTE / MINUTES_PER_HOUR / HOURS_PER_DAY;
    cout << second << " seconds = " << day << " days, " << hour << " hours, " << minute << " minutes, " << second << " seconds.\n";
    return 0;
}

5、

#include <iostream>
using namespace std;

int main()
{
    long long population_world, population_China;
    cout << "Enter the world's population: ";
    cin >> population_world;
    cout << "Enter the population of the China: ";
    cin >> population_China;
    cout << "The population of the China is " << 100*double(population_China) / population_world << "% of the world population.\n";
    return 0;
}

6、

#include <iostream>
using namespace std;

int main()
{
    double km, liter;
    cout << "Please enter the distance in kilometers, and fuel consumption in liters: ";
    cin >> km >> liter;
    cout << liter / km * 100 << "km/100L\n";
    return 0;
}

7、

#include <iostream>
using namespace std;

int main()
{
    const double MILES_PEER_HUNDREDMETERS = 62.14; //每100公里为62.14英里
    const double LITERS_PER_GALLON = 3.785; //每加仑为3.785升
    double km, liter, mile, gallon; //千米,升,英里,加仑
    cout << "Please enter the distance in kilometers, and fuel consumption in liters: ";
    cin >> km >> liter;
    mile = km / 100 * MILES_PEER_HUNDREDMETERS;
    gallon = liter / LITERS_PER_GALLON;
    cout << "The fuel consumption is "<<liter / km * 100 << "L/100km, equals to "<<mile/gallon<<"mpg.\n";
    return 0;
}

 

posted @ 2022-11-07 17:12  Triphan  阅读(27)  评论(0)    收藏  举报