C++ Prime Plus第3章编程练习答案
1
#include <iostream>
using namespace std;
const int inch_per_feet = 12;//1英尺=12英寸
int main()
{
int height = 0;
cout << "请输入一个整数身高(英寸feet):___\b\b\b";//书51页有说到\b的用法
cin >> height;
cout << endl << "身高为:" << height / inch_per_feet << "英尺" << height % inch_per_feet << "英寸";
return 0;
}
结果为:
2
#include<iostream>
using namespace std;
const int inch_per_feet = 12;//1英尺=12英寸
const double feet_per_meter = 0.0254;//1英寸=0.0254米
const double kilo_per_pound = 2.2;//1千克=2.2磅
int main()
{
int heightInch = 0;
int heightFeet = 0;
int weight = 0;
cout << "请以*英尺*英寸的方式输入您的身高:";
cin >> heightInch >> heightFeet;
cout << "请输入您的体重(磅):";
cin >> weight;
int heightF = heightInch * inch_per_feet + heightFeet;//英寸身高
cout << "您的身高是:" << heightF<< "英寸" << endl;
double heightM = heightF * feet_per_meter;//米身高
double kilogram = weight / kilo_per_pound;//千克重
double BMI = kilogram / heightM;
cout << "您的BMI为:" << BMI << endl;
return 0;
}
结果为:
3
#include<iostream>
using namespace std;
const int factor = 60;//转化因子60
int main()
{
double deg, min, sec;//度,分,秒
cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: ";
cin >> deg;
cout << "Next, enter the minutes of arc: ";
cin >> min;
cout << "Finally, enter the seconds of arc: ";
cin >> sec;
double totaldeg = sec / pow(factor, 2) + min / factor + deg;
cout << deg << " degrees, " << min << " minutes, " << sec << " seconds = " << totaldeg << " degrees." << endl;
return 0;
}
结果为:
4
#include<iostream>
using namespace std;
const int H = 24;//1天=24小时
const int factor = 60;//转化因子60
int main()
{
long sec = 0;//秒数
cout << "请输入整数的秒数: ";
cin >> sec;
int days = sec / (factor*factor*H);//天数
double tmp = sec - days*factor*factor*H;//剩余的秒数
int hours = int(tmp / (factor * factor));//小时
tmp = tmp - hours * factor*factor;//剩余的秒数
int minutes = int(tmp / factor);//分钟
tmp = tmp - minutes * factor;//分钟的小数部分
int seconds = tmp ;//秒数
cout << sec << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds." << endl;
return 0;
}
结果为:
5
#include<iostream>
using namespace std;
int main()
{
long long worldnum = 0;
long long Chinanum = 0;//全球人数和中国人数
cout << "Enter the world's popolation: ";
cin >> worldnum;
cout << "Enter the popolation of China: ";
cin >> Chinanum;
double rate = (double(Chinanum) / double(worldnum));
cout << "The poplation of the China is " << rate * 100 << "% of the worls poplation." << endl;
return 0;
}
结果为:
这里要注意的是:
double rate = (double(Chinanum) / double(worldnum));
需要把long long 型转化为double型,再进行除法,因为除法运算是取决于操作数的,操作数如果是整型,结果肯定是0;只有转换为double型再进行除法才会得到小数形式!!!
6
#include<iostream>
using namespace std;
int main()
{
double kilometer = 0;//公里
double volumn = 0;//升
cout << "请输入以公里为单位的距离: ";
cin >> kilometer;
cout << "请输入以升为单位的汽油量: ";
cin >> volumn;
double consum = volumn / kilometer * 100;
cout << "每100公里的耗油量: " << consum << endl;
return 0;
}
结果为:
7
#include<iostream>
using namespace std;
int main()
{
double consum = 0;//每100公里耗油量
double mpg = 0;
cout << "请输入每100公里消耗的汽油量: ";
cin >> consum;
mpg = 62.14 / consum * 3.875;
cout << "转换为每加仑的英里数为: " << mpg << endl;
return 0;
}
结果为:
书上描述的127mpg大约合8.7l/100km有错误,应该是27mpg大约合8.7l/100km。