C++ Primer Plus 第3章 课后编程练习 代码

1#include <iostream>
using namespace std;
const int trans = 12;

typedef struct hight
{
    int inch;
    int foot;
} Hight;
void InchToFoot(double inch);
void InchToHight(int inch);

int main()
{
    int inch1;
    double inch2;
    cout << "Enter your hight please(unit in inch):____\b\b\b\b";
    cin >> inch1;
    inch2 = inch1;
    InchToFoot(inch2);
    InchToHight(inch1);

    return 0;
}

void InchToFoot(double inch)
{

    double foot = inch / trans;
    cout << inch << " inches = " << foot << " feet " << endl;
}

void InchToHight(int inch)
{

    Hight member;
    member.foot = inch / trans;
    member.inch = inch % trans;
    cout << inch << " inches = " << member.foot << " feet "
         << member.inch << " inches. " << endl;
}
第二题
#include <cmath>
#include <iostream>
using namespace std;

typedef struct member
{
    int foot;
    int inch;
    double hight;

    double wig_pound;
    double wig_kg;
    double BMI;
} Member;
double OutputBMI(Member *mem);

int main()
{
    Member *info = (Member *)malloc(sizeof(Member));

    cout << "Please input your hight(first input foot):" << endl;
    cin >> info->foot;
    cout << "Please input your hight(second input inch):" << endl;
    cin >> info->inch;
    cout << "Please input your wight(unit in pound):" << endl;
    cin >> info->wig_pound;

    cout << "YOUR BMI = " << OutputBMI(info) << endl;

    return 0;
}

double OutputBMI(Member *mem)
{
    mem->hight = (mem->foot * 12.0 + mem->inch) * 0.0254;
    mem->wig_kg = mem->wig_pound / 2.2;
    mem->BMI = mem->wig_kg / pow(mem->hight, 2);

    return mem->BMI;
}
3#include <iostream>
int main()
{
    using namespace std;

    const double trans = 60;
    int degrees, minutes, seconds;
    cout << "Enter a latitude in degrees,minutes,and seconds:" << endl
         << "First, enter the degrees: ";
    cin >> degrees;
    cout << "Next, enter the minutes of arc:";
    cin >> minutes;
    cout << "Finally, enter the seconds of arc:";
    cin >> seconds;
    double arc = degrees + minutes / trans + minutes / trans / trans;
    cout << degrees << " degrees , " << minutes << " minutes ,"
         << seconds << " seconds = " << arc << " degrees" << endl;
    return 0;
}
4#include <iostream>
int main()
{
    using namespace std;
    const int Hour_P_Day = 24;
    const int Min_P_Hour = 60;
    const int Sec_P_Min = 60;

    long Input;
    cout << "Enter the number of seconds:";
    cin >> Input;
    int days = Input / (Hour_P_Day * Min_P_Hour * Sec_P_Min);

    int mid_1 = Input % (Hour_P_Day * Min_P_Hour * Sec_P_Min);
    int hours = mid_1 / (Min_P_Hour * Sec_P_Min);

    int mid_2 = mid_1 % (Min_P_Hour * Sec_P_Min);
    int mins = mid_2 / Sec_P_Min;

    int sec = mid_2 % Sec_P_Min;

    cout << Input << " seconds = " << days << " days , "
         << hours << " hours , "
         << mins << " minutes , "
         << sec << " seconds " << endl;

    return 0;
}
5#include <iostream>

int main()
{
    using namespace std;
    cout << "Enter the world's population:";
    long long pup;
    cin >> pup;
    cout << "Enter the population of the US:";
    long long US;
    cin >> US;

    double rate = 100.0 * US / pup;
    cout << "The population of US is " << rate << "\% of the world population.";

    return 0;
}
6#include <iostream>

int main()
{
    using namespace std;
    cout << "Please choose the units:<A>  US   <B>  CHN" << endl;
    char ch;
    cin >> ch;
    if (ch == 'A')
    {
        cout << "Please input the total mileage(miles):_____\b\b\b\b\b";
        double miles;
        cin >> miles;
        cout << "Please input the gas(gallons):_____\b\b\b\b\b";
        double gals;
        cin >> gals;
        cout << "Ok, " << miles / gals << " miles per gallon.";
    }
    else if (ch == 'B')
    {
        cout << "Please input the total mileage(km):_____\b\b\b\b\b";
        double kilm;
        cin >> kilm;
        cout << "Please input the gas(litre):_____\b\b\b\b\b";
        double litre;
        cin >> litre;
        cout << "Ok, " << litre / (kilm / 100) << " litres gas per 100km.";
    }
    else
    {
        cout << "Your input is wrong!" << endl;
    }

    return 0;
}
7#include <iostream>

int main()
{
    using namespace std;
    cout << "Please input the total mileage(km):_____\b\b\b\b\b";
    double kilm;
    cin >> kilm;
    double miles = kilm * (100 / 62.14);

    cout << "Please input the gas(litre):_____\b\b\b\b\b";
    double litre;
    cin >> litre;
    double gallons = litre * (1 / 3.875);

    cout << "Ok, " << miles / gallons << " miles per gallons." << endl;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖啡与乌龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值