第三章习题 C++ Primer 第六版

1.使用一个整数输入自己的身高(单位为cm),并将此身高转化为米和厘米共同表示的形式,使用下划线字符来指示输入的位置,使用一个const符号常量来表示转换因子。

#include<iostream>
using namespace std;
const int transform = 100;
int main()
{   
    cout << "请输入自己的身高,单位为厘米:___" ;//使用下划线字符来指示输入的位置
    int height;
    cin >> height;
    cin.get();
    cout << "你的身高是: " << height << "公分" << endl;
    int meter;
    meter = height / transform;
    int cm;
    cm = height % transform;
    cout << height << "公分是" << meter << "米加" << cm << "厘米" << endl;
    cin.get();
    return 0;
}  

运行结果:  

  请输入自己的身高,单位为厘米:190
  你的身高是: 190公分
  190公分是1米加90厘米

2.要求以几英尺几英寸的方式输入身高,并以磅为单位输入其体重,计算IBM指数。

#include<iostream>
using namespace std;
const double Inch2Meter = 0.254;
const double Kilo2Pound = 2.2;
const int Foot2Inch = 12;

int main()
{   
    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;
        cin.get();
        cout << "Second enter the inch: ";
        cin >> height_inch;
        cin.get();
        cout << "Enter you weight in pound: ";
        cin >> weight_pound;
        cin.get();
        height_meter = (height_foot * Foot2Inch + height_inch) * Inch2Meter;
        weight_kilo = weight_pound / Kilo2Pound;

        BMI = weight_kilo / (height_meter * height_meter);

        cout << "Your BIM is " << BMI << endl;
        cin.get();
        return 0;
}

运行结果:  

  Enter your height in foot and inch
  First enter the foot: 12
  Second enter the inch: 45
  Enter you weight in pound: 6
  Your BIM is 0.00118342

3.要求用户以度,分,秒的方式输入一个纬度,然后最终以度为单位显示该纬度。

#include<iostream>
using namespace std;
int main()
{   
    double degress = 0.0;
    double minutes = 0.0;
    double seconds = 0.0;

    cout << "Enter a latitude in degrees,minutes,and seconds:" << endl;
    cout << "First enter the the degress: ";
    cin >> degress;
    cin.get();
    cout << "Second enter the minutes: ";
    cin >> minutes;
    cin.get();
    cout << "Finally ,enter the seconds : ";
    cin >> seconds;
    cin.get();

    double last = seconds / 60 / 60 + minutes / 60 + degress;

    cout << degress << " degress, " << minutes << " minutes, " << seconds << " seconds = " << last << " degrees" << endl;
    cin.get();
    return 0;
}

运行结果:

  Enter a latitude in degrees,minutes,and seconds:
  First enter the the degress: 37
  Second enter the minutes: 51
  Finally ,enter the seconds : 19
  37 degress, 51 minutes, 19 seconds = 37.8553 degrees、

4. 要求用户以整数方式输入秒数,然后以天、小时、分钟和秒的方式显示这段时间。

#include<iostream>
using namespace std;
int main()
{   
    long total = 0.0;
    cout << "请输入秒数,我给您以天、小时、分钟和秒的方式显示这段时间: " ;
    cin >> total;
    cin.get();
     
    int days;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;

    days = total / (24 * 60 * 60);
    hours = ((total % (24 * 60 * 60)) / (60 * 60));
    minutes = ((total % (60 * 60)) / 60);
    seconds = (total % 60);

    cout << "该段时间为:" << days << "" << hours << " 小时 " << minutes << " 分钟 " << seconds << "" << endl;
    cin.get();
    return 0;
}

运行结果:  

  请输入秒数,我给您以天、小时、分钟和秒的方式显示这段时间: 4556652
  该段时间为:52 天 17 小时 44 分钟 12 秒

原文地址:https://www.cnblogs.com/carlber/p/9852825.html