1. 程式人生 > >C++primer plus 第三章程式設計練習

C++primer plus 第三章程式設計練習

本人用code::block 編寫,如需參考,善用Ctrl+shift+C 和 Ctrl + shift + X 快捷鍵 如有任何錯誤或疑問,歡迎留言

////3.1 inches to feet
//#include <iostream>
//const int Inch_Feet_Transfer = 12;
//int main()
//{
//    using namespace std;
//    float inch;
//    int feet;
//    cout << "Please enter your height in inches : ______\b\b\b\b\b\b";
//    cin >> inch;
//    feet = int(inch) / Inch_Feet_Transfer;
//    inch = inch - feet*Inch_Feet_Transfer;
//    cout << "Your height is " << feet << " feet " << inch << " inches.";
//    return 0;
//}

        //3.2 BMI
//#include <iostream>
//
//const int feet_inches = 12;
//const float inches_metres = 0.0254;
//const float pounds_kg = 2.2;
//int main()
//{
//    using namespace std;
//    int feet;
//    float inches;
//    float metres;
//    float pounds, kg;
//    float BMI;
//    cout << "Plesae enter your height in format like : feet inches" << endl;
//    cin >> feet >> inches;
//    inches = feet * feet_inches + inches;
//    metres = inches * inches_metres;
//
//    cout << "Please enter your mass in pounds : " <<endl;
//    cin >> pounds;
//    kg = pounds / pounds_kg;
//    BMI = kg / (metres*metres);
//    cout << "Your BMI is : " << BMI;
//    return 0;
//}
        //3.3 latitude
//#include <iostream>
//
//
//int main()
//{
//    using namespace std;
//    int Lttd_degree;
//    int Lttd_minute;
//    float Lttd_second;
//    double Indegree;
//
//    cout << "Please enter a latitude in degrees, minutes and seconds." << endl;
//    cout << "First is degrees:" << endl;
//    cin >> Lttd_degree;
//    cout << "Next is minutes:" << endl;
//    cin >> Lttd_minute;
//    cout << "Third is seconds:" << endl;
//    cin >> Lttd_second;
//
//    Indegree = Lttd_degree + Lttd_minute/60.0 + Lttd_second/3600;
//    cout << Lttd_degree << " degrees, " << Lttd_minute << " minutes, "
//     << Lttd_second << " seconds " << "= " << Indegree << " degrees" << endl;
//
//     return 0;
//
//}
        //3.4 seconds to day
//#include <iostream>
//const int s_d = 86400;
//const int s_h = 3600;
//const int s_m = 60;
//int main()
//{
//    using namespace std;
//    long long seconds = 0;
//    int days = 0;
//    int hours = 0;
//    int minutes = 0;
//    int save_seconds = 0;
//    cout << "Enter the number of seconds: ";
//    cin >> seconds;
//    save_seconds = seconds;
//    days = seconds / s_d;
//    seconds -= days * s_d;
//    hours = seconds / s_h;
//    seconds -= hours * s_h;
//    minutes = seconds / s_m;
//    seconds -= minutes * s_m;
//    cout << save_seconds << " seconds = " << days << " days, " << hours
//    << " hours, " << minutes << " minutes, " << seconds << " seconds";
//    return 0;
//}
            //3.5 The percentage of the population of the United States in the world
//#include <iostream>
//int main()
//{
//    using namespace std;
//    long long total;
//    long long US;
//    double RatioUS;
//    cout << "Enter the world's population:";
//    cin >> total;
//    cout << "Enter the population of the US:";
//    cin >> US;
//    RatioUS = double(US)/total * 100;
//    cout << "The population of the US is " << RatioUS << "% of the world population.";
//    return 0;
//}
        //3.6 fuel consumption per mile
//#include <iostream>
//int main()
//{
//    using namespace std;
//    double mile;
//    double gallon;
//    double mile_per_gallon;
//
//    cout << "Enter your mileage: ";
//    cin >> mile;
//    cout << "Enter your fuel consumption(in gallon): ";
//    cin >> gallon;
//    mile_per_gallon = mile / gallon;
//    cout << "Your car will ride " << mile_per_gallon << " miles per gallon.";
//    return 0;
//}

        //3.7 fuel consumption transfer
#include <iostream>

const double liter_mile_gallon_mile = 3.875 * 62.14;
int main ()
{
    using namespace std;
    double liter_kilo = 0;
    double gallon_mile = 0;
    cout << "Please enter your fuel consumption in EU(liters per hundred kilometers): ";
    cin >> liter_kilo;
    gallon_mile = liter_mile_gallon_mile / liter_kilo;
    cout << "Fuel consumption in US(miles per gallon): " << gallon_mile << "mpg";
    return 0;
}