我的練習,僅供參考,歡迎你們提出意見,指出錯誤!喜歡的點個贊吧!
Practice 1
//要求用戶以整數輸入英寸爲單位的身高,將其轉換爲英尺和英寸
#include <iostream>
using namespace std;
int main(){
const int foot = 12;
int inches = 0;
cout << "Please enter your height: __ inches:" << endl;
cin >> inches;
cout << "Your height is " << inches / foot << "feet and " << inches % foot << " inches!" << endl;
return 0;
}
Practice 2
//以英尺英寸的形式輸入身高,以磅爲單位輸入體重,計算BMI
#include <iostream>
using namespace std;
int main()
{
const int inches_per_foot = 12;
const double meters_per_inch = 0.0254;
const double kg_per_pound = 2.2;
//身高
int feet = 0;
int inches = 0;
cout << "Please enter your height: _ feet and _ inches:" << endl;
cin >> feet;
cin >> inches;
int height_by_inches = feet * inches_per_foot;
double height_by_meters = height_by_inches * meters_per_inch;
cout << "Your height is " << height_by_meters << " meters;" << endl;
//體重
double weight_by_pounds = 0.0;
cout << "Please enter your weight: _ pounds:" << endl;
cin >> weight_by_pounds;
double weight_by_kg = weight_by_pounds * kg_per_pound;
cout << "Your weight is " << weight_by_kg << " kg;" << endl;
//BMI
double bmi = weight_by_kg / (height_by_meters * height_by_meters);
cout << "Your BMI is " << bmi << endl;
return 0;
}
Practice 3
//輸入度分秒,以度數輸出
#include <iostream>
using namespace std;
int main()
{
const int minutes_per_degree = 60;
const int seconds_per_minute = 60;
int degrees = 0;
double minutes = 0;
double seconds = 0;
cout << "Enter the degrees:" << endl;
cin >> degrees;
cout << "Enter the minutes:" << endl;
cin >> minutes;
cout << "Enter the seconds:" << endl;
cin >> seconds;
double seconds_to_minutes = seconds / seconds_per_minute;
//踩坑:int除以int必定獲得整數(即便結果將會賦值給double),所以上句參與運算的兩個數必需要有一個是非int類型,不然上句結果爲0
cout << "seconds_to_minutes: " << seconds_to_minutes << endl;
double minutes_to_degrees = (minutes + seconds_to_minutes) / minutes_per_degree;
cout << "minutes: " << minutes + seconds_to_minutes << endl;
double final_degrees = degrees + minutes_to_degrees;
cout << degrees << " degrees " << minutes << " minutes " << seconds << " seconds = "
<< final_degrees << " degrees" << endl;
return 0;
}
Practice 4
//整數方式輸入秒數,而後以天時分秒的方式輸出
#include <iostream>
using namespace std;
int main()
{
const int hours_per_day = 24;
const int minutes_per_hour = 60;
const int seconds_per_minute = 60;
long long time_by_seconds = 0;
cout << "Please enter time by seconds:" << endl;
cin >> time_by_seconds;
long long time_by_minutes = time_by_seconds / seconds_per_minute;
int seconds = time_by_seconds % seconds_per_minute;
long long time_by_hours = time_by_minutes / minutes_per_hour;
int minutes = time_by_minutes % minutes_per_hour;
int time_by_days = time_by_hours / hours_per_day;
int hours = time_by_hours % hours_per_day;
cout << time_by_seconds << " seconds = " << time_by_days << " days "
<< hours << " hours " << minutes << " minutes " << seconds << " seconds;" << endl;
}
Practice 5
//輸入全球人口和國家人口,並計算百分比輸出
#include <iostream>
using namespace std;
int main()
{
long long total = 0;
long long single = 0;
cout << "Please enter total population:" << endl;
cin >> total;
cout << "Please enter the population of your country:" << endl;
cin >> single;
long double double_single = (long double) (single);
//踩坑:強制類型轉換不會修改變量自身,須要一個新變量來存儲
double percent = (double_single / total) * 100;
//long long 爲整型,因此必須將其中一個 long long 強制類型轉換爲 long double 後才能獲得 double 類型結果
cout << "The percent is " << percent << endl;
return 0;
}
Practice 6
//耗油量計算
#include <iostream>
using namespace std;
int main()
{
int calc_type = 0;
cout << "Enter 1 or 2 to choose type:" << endl;
cout << "1 - miles/gallon" << endl;
cout << "2 - liters/100km" << endl;
cin >> calc_type;
if (calc_type == 1) {
double miles = 0;
double gallons = 0;
cout << "Enter miles and gallons:" << endl;
cin >> miles;
cin >> gallons;
cout << "The fuel consumption is " << miles / gallons << " miles per gallon" << endl;
return 0;
} else if (calc_type == 2) {
double kms = 0;
double liters = 0;
cout << "Enter kms and liters:" << endl;
cin >> kms;
cin >> liters;
cout << "The fuel consumption is " << (liters * 100) / kms << " liters per 100km" << endl;
return 0;
} else {
cout << "Invalid input" << endl;
return 0;
}
}
Practice 7
//耗油量轉換
#include <iostream>
using namespace std;
int main()
{
const double miles_per_100km = 62.14;
const double liters_per_gallon = 3.875;
double eu_fuel_consumption = 0;
double us_fuel_consumption = 0;
cout << "Enter fuel consumption by eu: __ liters per 100km" << endl;
cin >> eu_fuel_consumption;
us_fuel_consumption = miles_per_100km / (eu_fuel_consumption / liters_per_gallon);
cout << "Fuel consumption by us is " << us_fuel_consumption << " miles per gallon" << endl;
return 0;
}
歡迎訪問 Github 倉庫地址ios