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

自己写的 欢迎探讨纠错呀

#include <iostream>
using namespace std; 

void question_1();
void question_2();
void question_3();
void question_4(); 
void question_5();
void question_6();
void question_7();

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

void question_1()
{
    int height;
    const int inch2foot = 12;
    cout << "Enter your height(inch) : ___\b\b\b";
    cin >> height;
    cout << endl;
    cout << "Your height is " << height/inch2foot << "\'" << height % inch2foot << "\""<< endl;
    return;
}

void question_2()
{
    int height_in , height_ft , height;
    double height_m , weight_pd , weight_kg;
    const int inch2foot = 12;
    const double foot2meter = 0.0254;
    const double kg2pound = 2.2;
    cout << "Enter your height(inch) : ___\b\b\b";
    cin >> height_in;
    cout << "height(foot) : __\b\b";
    cin >> height_ft;
    cout << endl;
    cout << "Enter your weight(pound) : ___\b\b\b";
    cin >> weight_pd;
    cout << endl;
    
    height = inch2foot * height_in + height_ft;
    height_m = inch2foot * height;
    cout << "Your height is " << height << " foot, which means " << height_m << " meters." << endl;
    weight_kg = weight_pd / kg2pound;
    cout << "Your BMI is " << weight_kg/(height_m * height_m) << endl;
    return;
}

void question_3()
{
    int degrees , minutes , seconds;
    const double convert = 60.0;
    cout << "Enter a latitude in degrees , minutes and seconds: " << endl;
    cout << "First, enter the degrees: ";
    cin >> degrees;

    cout << "Next, enter the minutes of arc: ";
    cin >> minutes;

    cout << "Finally, enter the seconds of arc: ";
    cin >> seconds;

    cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = ";
    cout << degrees + minutes / convert + seconds / convert / convert << " degrees." << endl;
    return;
}

void question_4()
{
    long long int time , time_left;
    long int days;
    int hours, minutes , seconds;
    cout << "Enter the number of seconds: ";
    cin >> time;
    days = time / (60 * 60 * 24);
    time_left = time - 60 * 60 * 24 * days;
    hours = time_left / (60 * 60);
    time_left -= 60 * 60 * hours;
    minutes = time_left / 60;
    seconds = time_left - 60 * minutes; 
    cout << time << "seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds." << endl;
    return;

void question_5()
{
    long long int pop_world , pop_us;
    cout << "Enter the world's population: ";
    cin >> pop_world;
    cout << "Enter the population of the US: ";
    cin >> pop_us;
    cout << "The population of the US is " << ((long double) pop_us / pop_world) * 100.0 << "% of the world population." << endl;
    return;
}

void question_6()
{
    double length , oil;
    cout << "Enter the district: ";
    cin >> length;
    cout << "Enter the oil used: ";
    cin >> oil;
    cout << "Your car uses oil " << oil / (length / 100.0) << "/100km." << endl;
    return;
}

void question_7()
{
    double oil;
    cout << "Enter the oil assumption of your car in European way(L/100km): ";
    cin >> oil;
    cout << "The oil assumption of your car in America way is " << 62.14 / (oil / 3.875) << "mpg" << endl;
    return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值