第三章
1 英尺英寸,转换,用 const
表示转换因子
#include <iostream>
using namespace std;
int main ( )
{
const int inch_per_foot = 12 ;
int height = 0 ;
cout << "输入你的身高,单位是英尺:___" << "\b\b\b" ;
cin >> height;
cout << "你的身高是:" << height << "英尺" << endl;
cout << "合计" << height/ inch_per_foot << "英尺" <<
height% inch_per_foot << "英寸" << endl;
return 0 ;
}
2 以几英尺几英寸输入身高,以磅输入同体重,输出BMI
#include <iostream>
using namespace std;
int main ( )
{
const double inch_per_foot = 12 ;
const double metre_per_inch = 0.0254 ;
const double pound_per_kilogram = 2.2 ;
double height_foot{ 0 } , height_inch{ 0 } , weight_pound{ 0 } ;
double height_m{ 0 } , weight_kg{ 0 } ;
cout << "依次输入身高的英尺和英寸:" << endl;
cin >> height_foot >> height_inch;
height_m = ( height_foot * 12 + height_inch) * metre_per_inch;
cout << "输入体重的磅数:" << endl;
cin >> weight_pound;
weight_kg = weight_pound / pound_per_kilogram;
double BMI = ( weight_kg) / ( height_m * height_m) ;
cout << "你的身高是:" << height_m << "米" << endl
<< "你的体重是:" << weight_kg << "千克" << endl
<< "你的BMI是:" << BMI << endl;
return 0 ;
}
3 以“度,分,秒”输入一个纬度,然后以度为单位显示
#include <iostream>
using namespace std;
int main ( )
{
cout << "Entet a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: " << endl;
int degrees = 0 ;
cin >> degrees;
cout << "Next, enter the minutes of arc: " << endl;
int minutes = 0 ;
cin >> minutes;
cout << "Finally, enter the seconds of arc: " << endl;
int seconds = 0 ;
cin >> seconds;
double result = 0 ;
result = seconds / 3600.0 + minutes / 60.0 + degrees;
cout << degrees << " degrees, "
<< minutes << " minutes, "
<< seconds << " seconds = "
<< result << " degrees."
<< endl;
return 0 ;
}
4 以整数方式输入秒数(使用 long
或 long long
存储),然后以天,小时,分钟和秒的方式显示
#include <iostream>
using namespace std;
int main ( )
{
const int hours_per_day = 24 ;
const int minutes_per_hours = 60 ;
const int seconds_per_minutes = 60 ;
cout << "Enter the number of seconds: " ;
long long seconds = 0 ;
cin >> seconds;
int minutes, hours, days;
minutes = seconds / seconds_per_minutes;
seconds = seconds % seconds_per_minutes;
hours = minutes / minutes_per_hours;
minutes = minutes % minutes_per_hours;
days = hours / hours_per_day;
hours = hours % hours_per_day;
cout << "seconds = " << days << " days, "
<< hours << " hours, "
<< minutes << " minutes, "
<< seconds << " seconds." << endl;
return 0 ;
}
5 输入全球人口和某国人口,输出百分比
#include <iostream>
using namespace std;
int main ( )
{
cout << "Enter the world's population: " ;
long long int world_population = 0 ;
cin >> world_population;
cout << "Enter the population of the US: " ;
long long int nation_population = 0 ;
cin >> nation_population;
double percent = 0 ;
percent = 100 * ( double ( nation_population) / double ( world_population) ) ;
cout << "The population of the US is "
<< percent << '%'
<< " of the world population." << endl;
return 0 ;
}
6 输入里程数(英里)和使用汽油量(加仑)然后指出汽车耗油量(里程每加仑或者升每100公里)
#include <iostream>
using namespace std;
int main ( )
{
int type = 0 ;
cout << "miles of km?\nmiles : 0 and km : 1" << endl;
cin >> type;
double males = 0 ;
double galens = 0 ;
double km = 0 ;
double litre = 0 ;
switch ( type)
{
case 0 :
cout << "输入英里和加仑,空格分隔: \n" ;
cin >> males >> galens;
cout << "您的爱车消耗1加仑油可以行驶 "
<< males / galens << " 英里.\n" ;
break ;
case 1 :
cout << "输入公里和升,空格分隔: \n" ;
cin >> km >> litre;
cout << "您的爱车百公里油耗是: "
<< litre / ( km / 100 ) << " 升"
<< endl;
break ;
default :
break ;
}
return 0 ;
}
7 输入百公里耗油量(升),转换成每加仑英里数
#include <iostream>
using namespace std;
int main ( )
{
const double mile_per_km = 0.6214 ;
const double liter_per_galen = 3.875 ;
cout << "输入风格选择:\n欧洲风格请按0,美国风格请按1: " ;
int type = 0 ;
cin >> type;
double km{ 0 } , liter{ 0 } , mile{ 0 } , galen{ 0 } ;
switch ( type)
{
case 0 :
cout << "输入油耗(升)和里程数(公里): " ;
cin >> liter >> km;
cout << "百公里油耗: " << liter/ ( km/ 100 ) << endl;
galen = liter/ liter_per_galen;
mile = km* mile_per_km;
cout << "等价于 "
<< mile/ galen << "英里每加仑"
<< endl;
break ;
case 1 :
cout << "输入里程数(英里)和油耗(加仑): " ;
cin >> mile >> galen;
cout << "每加仑 " << mile/ galen
<< " 英里" << endl;
liter = galen* liter_per_galen;
km = mile/ mile_per_km;
cout << "等价于百公里油耗 " << 100 * liter/ km << "升" << endl;
break ;
default :
break ;
}
return 0 ;
}