1.編寫一個小程序,要求用户使用一個整數指出自己的身高(單位為英寸),然後將身高轉換為英尺和英寸。

該程序使用下劃線字符來指示輸入位置。另外,使用一個const符號常量來表示轉換因子。

/*1.編寫一個小程序,要求用户使用一個整數指出自己的身高(單位為英寸),然後將身高轉換為英尺和英寸。
該程序使用下劃線字符來指示輸入位置。另外,使用一個const符號常量來表示轉換因子。*/
#include<iostream>
using namespace std;

const int FOOT_TO_INCH = 12;

int main() {
    // 1. 提示用户輸入 (使用下劃線字符來指示輸入位置)
    cout << "Please enter your height int inches_";
    int height;
    cin >> height;
    
    // 2. 轉換成英尺和英寸(const符號常量來表示轉換因子)
    int foot = height / FOOT_TO_INCH;
    int inch = height % FOOT_TO_INCH;
    // 3. 輸出
    cout << "Your height convert to " << foot << " foot and " << inch << " inch height." << endl;
    return 0;
}
xaye@orange:~/code/dev/7$ ./a.out 
Please enter your height int inches_90
Your height convert to 7 foot and 6 inch height.

2.編寫一個小程序,要求以幾英尺幾英寸的方式輸入其身高,並以磅為單位輸入其體重。

(使用3個變量來存儲這些信息。)該程序報告其BMI(BodyMassIndex,體重指數)。

為了計算BMI,該程序以英寸的方式指出用户的身高(1英尺為12英寸),

並將以英寸為單位的身高轉換為以米為單位的身高(1英寸=0.0254米)。

然後,將以磅為單位的體重轉換為以千克為單位的體重(1千克=2.2磅)。

最後,計算相應的BMI體重(千克)除以身高(米)的平方。用符號常量表示各種轉換因子。

#include<iostream>
using namespace std;


int main() {
    // 1. 提示接收用户的輸入,三個參數
    cout << "Please enter your height foot:";
    int height_foot;
    cin >> height_foot;

    cout << "Please enter your height inch:";
    int height_inch;
    cin >> height_inch;

    cout << "Please enter your height pound:";
    int weight_pount;
    cin >> weight_pount;
    // 2. 英寸的身高轉換為米,磅轉為千克
    double height = (height_foot * 12 + height_inch) * 0.0254;
    double weight = weight_pount / 2.2;
    
    // 3. 計算 bmi, 體重(千克)除以身高(米)的平方
    double bmi = height / (weight * weight);
    // 4. 輸出
    cout << "bmi = " << bmi << endl;
    return 0;
}
xaye@orange:~/code/dev/7$ ./a.out 
Please enter your height foot:40
Please enter your height inch:30
Please enter your height pound:200
bmi = 0.00156743
  1. 編寫一個程序,要求用户以度、分、秒的方式輸入一個緯度,然後以度為單位顯示該緯度。

1度為60分,1分等於60秒,請以符號常量的方式表示這些值。

對於每個輸入值,應使用一個獨立的變量存儲它。

下面是該程序運行時的情況:

Enter a latitude in degrees,minutes, and seconds:

First, enter the degrees:37

Next,enter the minutes of arc:51

Finally, enter the seconds of arc:19

37 degrees,51 minutes,19 seconds = 37.8553 degrees

#include<iostream>
using namespace std;


int main() {
    // 用來設置輸出的格式
    cout.setf(ios_base::fixed,ios_base::floatfield);
    
    // 1. 提示和接收用户輸入的度、分、秒
    cout << "Enter a latitude in degrees,minutes, and seconds:" << endl;
    cout << "First, enter the degrees:";
    int degrees;
    cin >> degrees;
    cout << "Next,enter the minutes of arc:";
    int minutes;
    cin >> minutes;
    cout << "Finally, enter the seconds of arc:";
    int seconds;
    cin >> seconds;

    // 2. 根據輸入計算最終的度(1度為60分,1分等於60秒,請以符號常量的方式表示這些值)
    const double DEGREES_TO_MINUTES = 60;
    const double DEGREES_TO_SECONDS = 3600;
    double result_degrees = degrees + minutes / DEGREES_TO_MINUTES + seconds / DEGREES_TO_SECONDS;
    cout << degrees << " degrees,"<< minutes <<" minutes,"<< seconds << " seconds = " << result_degrees <<" degrees" << endl;
    
    return 0;
}
xaye@orange:~/code/dev/7$ ./a.out 
Enter a latitude in degrees,minutes, and seconds:
First, enter the degrees:37
Next,enter the minutes of arc:51
Finally, enter the seconds of arc:19
37 degrees,51 minutes,19 seconds = 37.855278 degrees

4.編寫一個程序,要求用户以整數方式輸入秒數(使用1ong或ong1ong變量存儲),然後以天、小時、分鐘和秒的方式顯示這段時間。

使用符號常量來表示每天有多少小時、每小時有多少分鐘以及每分鐘有多少秒。

該程序的輸出應與下面類似:

Enter the number of seconds:31600000

31600000 seconds = 365 days,17 hours,46 minutes,40 seconds

#include<iostream>
using namespace std;

int main() {
    //用來設置輸出的格式
    cout.setf(ios_base::fixed,ios_base::floatfield);

    // 1. 提示和接收用户的輸入(使用long 或 long long變量存儲)
    cout << "Enter the number of seconds:";
    long long input_second;
    cin >> input_second;

    // 2. 計算出天、小時、分鐘和秒
    const int DAY_TO_HOURS = 24;
    const int HOURS_TO_MINUTES = 60;
    const int MINUTES_TO_SECONDS = 60;
    int days = input_second / (DAY_TO_HOURS * HOURS_TO_MINUTES * MINUTES_TO_SECONDS);
    long long remain_seconds = input_second % (DAY_TO_HOURS * HOURS_TO_MINUTES * MINUTES_TO_SECONDS);
    int hours = remain_seconds / (HOURS_TO_MINUTES * MINUTES_TO_SECONDS);
    remain_seconds = remain_seconds % (HOURS_TO_MINUTES * MINUTES_TO_SECONDS);
    int minutes = remain_seconds / MINUTES_TO_SECONDS;
    remain_seconds = remain_seconds % MINUTES_TO_SECONDS;

    // 3. 輸出
    cout << input_second <<" seconds = " << days <<" days," << hours
    << " hours," << minutes << " minutes," << remain_seconds << " seconds" << endl;
    
    return 0;
}

5.編寫一個程序,要求用户輸入全球當前的人口和美國當前的人口或其他國家的人口)。

將這些信息存儲在1ong1ong變量中,並讓程序顯示美國(或其他國家)的人口占全球人口的百分比。

該程序的輸出應與下面類似:

Enter the world's_population:6898758899

Enter the population of the US:310783781

The population of the US is 4.50492% of the world population.

#include<iostream>
using namespace std;

int main() {
    //用來設置輸出的格式
    cout.setf(ios_base::fixed,ios_base::floatfield);

    // 1. 接收輸入
    cout << "Enter the world's_population:";
    long long global_population;
    cin >> global_population;

    cout << "Enter the population of the US:";
    long long american_population;
    cin >> american_population;

    // 2. 計算
    double population_percent = 100.0 * american_population / global_population;

    // 3. 輸出
    cout << "The population of the US is " << population_percent << "% of the world population." << endl;
   
    return 0;
}
xaye@orange:~/code/dev/8$ ./a.out 
Enter the world's_population:6898758899
Enter the population of the US:310783781
The population of the US is 4.504923% of the world population.

6.編寫一個程序,要求用户輸入驅車裏程(英里)和使用汽油量(加侖),然後指出汽車耗油量為一加侖的里程。

如果願意,也可以讓程序要求用户以公里為單位輸入距離,並以升為單位輸入汽油量,然後指出歐洲風格的結果即每100公里的耗油量(升)。

#include<iostream>
using namespace std;
int main(){
    //用來設置輸出的格式
    cout.setf(ios_base::fixed,ios_base::floatfield);

    // 1. 接收用户的輸入(英里,汽油量)
    cout << "Enter the distance in miles:";
    int distance_miles;
    cin >> distance_miles;
    cout << "Enter the fuel consume in gallon:";
    double fuel_in_gallon;
    cin >> fuel_in_gallon;
    // 2. 計算輸出
    double fuel_consume = distance_miles / fuel_in_gallon;
    cout << "The fuel consume is " << fuel_consume << " miles/gallon." << endl;
    // 3. 接收用户的輸入(公里,汽油量)
    cout << "Enter the distance in kilometer:";
    int distance_kilometer;
    cin >> distance_kilometer;
    cout << "Enter the fuel consume in litre:";
    double fuel_in_litre;
    cin >> fuel_in_litre;
    // 4. 計算輸出
    fuel_consume = (fuel_in_litre / distance_kilometer) * 100;
    cout << "The fuel consume is " << fuel_consume << " miles/gallon." << endl;
    return 0;
}
xaye@orange:~/code/dev/8$ ./a.out 
Enter the distance in miles:100
Enter the fuel consume in gallon:10
The fuel consume is 10.000000 miles/gallon.
Enter the distance in kilometer:100
Enter the fuel consume in litre:8
The fuel consume is 8.000000 miles/gallon.