C++ Primer Plus(第六版) 第三章课后练习题

暑假学习计划 - 0x04

自己的C++太菜,导致学很多东西都心有余而力不足,就干脆费点时间,从基础再来一遍吧

原文链接:http://blog4jimmy.com/2017/10/71.html

本文内容几乎和原文一致,只是在main()函数里添加点东西方便大家调试

C++ Primer Plus(第六版) 第三章课后练习题

#include <iostream>
using namespace std;

const int foot2inch = 12;
void Q1(void)
{
    int inch_input = 0;
    int inch_output = 0;
    int foot = 0;

    cout << "Please input your height in inch:___\b\b\b";
    cin >> inch_input;
    foot = inch_input / foot2inch;
    inch_output = inch_input % foot2inch;
    cout << "Your height in inch was: " << inch_input 
        << "; Your height in foot and inch was: " << foot << " foot " 
        << inch_output << " inch!" << endl;
    return;
}

const double inch2foot = 0.254;
const double kilo2pound = 2.2;
void Q2(void)
{
    double height_foot = 0.0;
    double height_inch = 0.0;
    double height_meter = 0.0;
    double weight_pound = 0.0;
    double weight_kilo = 0.0;
    double BMI = 0.0;

    cout << "Enter your height in foot and inch" << endl;
    cout << "First enter the foot: ";
    cin >> height_foot;
    cout << "Second enter the inch: ";
    cin >> height_inch;

    cout << "Enter your weight in pound: ";
    cin >> weight_pound;

    height_meter = (height_foot*foot2inch + height_inch)*inch2foot;
    weight_kilo = weight_pound / kilo2pound;

    BMI = weight_kilo / (height_meter*height_meter);
    cout << "Your BIM is " << BMI << endl;
    return;
}

void Q3(void)
{
    int degrees = 0;
    int minutes = 0;
    int seconds = 0;
    double total = 0.0;

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

    total = degrees + minutes / 60.0 + seconds / 3600.0;
    cout << degrees << " degrees, " << minutes << " miutes, " << seconds << " seconds"
        << " = " << total << " degrees." << endl;
    return;
}

void Q4(void)
{
    long long total_seconds = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;

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

    days = total_seconds / (24 * 60 * 60);
    hours = ((total_seconds) % (24 * 60 * 60) / (60 * 60));
    minutes = ((total_seconds % (60 * 60)) / 60);
    seconds = total_seconds % 60;
    cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " 
        << minutes << " minutes, " << seconds << " seconds" << endl;
    return;
}
void Q5(void)
{
    long long world_population = 0;
    long long us_population = 0;
    double rate = 0.0;

    cout << "Enter the world's population: ";
    cin >> world_population;
    cout << "Enter the population of the US: ";
    cin >> us_population;

    rate = double (us_population) / (world_population);
    cout << "The population of the US is " << rate * 100 << "% of the world population." << endl;
    return;
}
void Q6(void)
{
    double mile = 0.0;
    double gallon = 0.0;
    double mile_per_gallon = 0.0;

    cout << "Enter the distance in mile you drive: ";
    cin >> mile;
    cout << "Enter the comsumption of oil: ";
    cin >> gallon;
    mile_per_gallon = mile / gallon;
    cout << "Average fuel comsuption: " << mile_per_gallon << " mile/gallon" << endl;
    return;
}

void Q7(void)
{
    double fuel_comsuption_eu = 0.0;
    double fuel_comsuption_us = 0.0;

    cout << "Enter the fuel comsuption in Europ standard: ";
    cin >> fuel_comsuption_eu;

    fuel_comsuption_us = fuel_comsuption_eu / 19 * 12.41;
    cout << "the fuel comsuption in US standard is " << fuel_comsuption_us << "/100KM" << endl;
}
int main()
{
    int i = 0;
    while (true)
    {
        cout << "Please input a number: ";
        cin >> i;
        cout << endl;
        switch (i)
        {
        case 1:
            Q1();
            break;
        case 2:
            Q2();
            break;
        case 3:
            Q3();
            break;
        case 4:
            Q4();
            break;
        case 5:
            Q5();
            break;
        case 6:
            Q6();
            break;
        case 7:
            Q7();
            break;
        default:
            break;
        }
        cout << endl;
        cout << "End!!!!!!!!!!" << endl;
        cout << endl;
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Pwnpanda/article/details/81566781