个人练习,仅供参考,欢迎大家提出意见,指出错误!喜欢的点个赞吧!

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 仓库地址


CheeReus_11
140 声望5 粉丝

学会爱人,成为大人