1. 程式人生 > >Sophia的程式設計師學習

Sophia的程式設計師學習

3.7.1

/*輸入身高(英寸),並轉換為英尺和英寸*/
#include <iostream>
int main()
{
    using namespace std;
    const int f_i = 12;        //轉換因子
    int height;
    cout << "Please enter your height (in inches): ____\b\b\b\b";    //使用下劃線來指示輸入位置
    cin >> height;
    cout << "Your height is " << height / f_i <<" feet";
    cout << height % f_i << " inches.\n";
    return 0;
}

3.7.2

/*計算BMI*/
#include <iostream>
const int Foot2Inch = 12;
const double Inch2Meter = 0.0254;
const double Kg2Pound = 2.2;
int main()
{
    using namespace std;
    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 << "Please enter your height in foot and inch.\n";
    cout << "First in foot: ";
    cin >> height_foot;
    cout << "Then in inch: ";
    cin >> height_inch;

    cout << "Now enter your weight in pound: ";
    cin >> weight_pound;
    cout << endl;

    height_inch = Foot2Inch * height_foot + height_inch;
    height_meter = height_inch * Inch2Meter;
    weight_kilo = weight_pound / Kg2Pound;
    BMI = weight_kilo / (height_meter * height_meter);

    cout << "So your height is " << height_inch << " inches, ";
    cout << "or " << height_meter << "meters.\n";
    cout << "And your weight is " << weight_kilo << " kilograms.\n\n";
    cout << "YOUR BMI IS " << BMI << "!\n";
    return 0;
}

3.7.3

/*度、分、秒之間的轉換*/
#include <iostream>
const double Degree2Minute = 60;
const double Minute2Second = 60;

int main()
{
    using namespace std;
    int degree = 0;
    int minute = 0;
    int second = 0;

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

    double ans = degree + (minute + second / Minute2Second) / Degree2Minute;
    cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = ";
    cout << ans << " degrees\n";
    return 0;
}