C++ Primer Plus 编程练习题 第三章 处理数据
1.身高转换
#include <iostream>
using namespace std;
int main()
{
const int yingchi = 12;
const double yingcun = 0.0254;
double height;
cout << "请输入你的身高(米):____\b\b\b\b";
cin >>height;
cout << "您的升高为" << height / yingcun << "英寸";
cout << "您的升高为" << height / yingcun / yingchi << "英尺";
return 0;
}
2.BMI计算
#include <iostream>
using namespace std;
int main()
{
const int yingchi = 12;
const double yingcun = 0.0254;
const double bang = 2.2;
double height_yingchi;
double height_yingcun;
cout << "请输入你的身高(英尺):____\b\b\b\b";
cin >> height_yingchi;
cout << "请输入你的身高(英寸):____\b\b\b\b";
cin >> height_yingcun;
double height_mi;
height_mi = (height_yingchi * yingchi + height_yingcun) * yingcun;
double weight_bang;
cout << "请输入你的体重(磅):____\b\b\b\b";
cin >> weight_bang;
double weight_kg;
weight_kg = weight_bang / bang;
double BMI;
BMI = weight_bang / (height_mi * height_mi);
cout << "您的BMI为" << BMI;
return 0;
}
3.纬度转换
#include <iostream>
using namespace std;
int main()
{
const int degree_minute = 60;
const int minute_second = 60;
int degree;
float minutes;
float seconds;
cout << "请输入度";
cin >> degree;
cout << "请输入分";
cin >> minutes;
cout << "请输入秒";
cin >> seconds;
cout << degree << "度" << minutes << "分" << seconds << "秒 = " << degree + minutes / degree_minute + seconds / (degree_minute * minute_second) << "度";
return 0;
}
4.表示时间
#include <iostream>
using namespace std;
int main()
{
long seconds;
cout << "输入秒数";
cin >> seconds;
const int day_seconds = 24*60*60;
const int hour_seconds = 60*60;
const int minute_seconds = 60;
cout << seconds / day_seconds<<"days,"<< seconds % day_seconds/hour_seconds<<"hours,"<< seconds % hour_seconds /minute_seconds<<"minutes,"<< seconds % hour_seconds % minute_seconds;
return 0;
}
5.人口计算
强制转换
#include <iostream>
using namespace std;
int main()
{
long long people_world;
cout << "请输入全球人口";
cin >> people_world;
cout << "请输入全国人口";
long long people_nation;
cin >> people_nation;
cout << "全国人口占全球人口百分比为" << (double(people_nation) / double(people_world))*100<<"%";
return 0;
}
6.油耗计算
#include <iostream>
using namespace std;
int main()
{
double mile;
cout << "请输入您的里程(英里)";
cin >> mile;
double gas;
cout << "请输入您的耗油量(加仑)";
cin >> gas;
cout << "您耗油量为一加仑的里程为" << mile / gas<<"英里";
return 0;
}
7.欧美耗油量转换
#include <iostream>
using namespace std;
int main()
{
const float mile_convert = 62.14;
const float gas_convert = 3.875;
float gas_mile;
cout << "请输入欧洲风格的耗油量(升/百公里)";
cin >> gas_mile;
cout << "美国风格的耗油量为" << mile_convert/(gas_mile / gas_convert)<< "(gpm)";
return 0;
}