您的位置:首页 > 编程语言 > C语言/C++

C++ primer Plus(第六版)中文版 第三章 处理数据 编程练习答案

2018-12-20 10:40 701 查看

第三章 编程练习
1. 编写一个小程序吗,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸,
   改程序使用下划线字符来指示输入的位置。另外,使用一个const符号常量来表示转换因子。
   1米等于39.37英寸(in),1英尺(ft)等于12英寸(in)

1.1 基础版

[code]#include <iostream>

int main()
{
using namespace std;

int input_in;
int ft;
int in;
const int factor = 12;

cout << "请输入您的身高:__英寸\b\b\b\b\b\b";
cin >> input_in;

ft = input_in / factor;
in = input_in % factor;

cout << "您的身高为:" << ft << "英尺" << in << "英寸";

return 0;
}

1.2 函数+while版

[code]#include <iostream>

int to_ft(int);
int to_in(int);

int main()
{
using namespace std;

int input_in;

cout << "请输入您的身高:__英寸\b\b\b\b\b\b";
while(cin >> input_in && input_in != 'q')
{
cout << "您的身高为:" << to_ft(input_in) << "英尺" << to_in(input_in) << "英寸\n";
cout << "请输入您的身高:__英寸\b\b\b\b\b\b";
}
cout << "end";
return 0;
}

int to_ft(int input_in)
{
int ft;
const int factor = 12;
ft = input_in / factor;
return ft;
}

int to_in(int input_in)
{
int in;
const int factor = 12;
in = input_in % factor;
return in;
}

2. 编写一个小程序要求以几英尺几英寸的方式输入其身高(单位为英寸),并以磅为单位输入其体重。
   (使用三个变量来存储这些信息),该程序报告其BMI。
   为了计算BMI,该程序以英寸的方式指出用户的身高,并将以英寸为单位的身高转换为以米为单位的身高
   (1英尺为12英寸,1英寸为0.0254 米)
   然后将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2 磅)
   最后计算相应的BMI,即:体重(kg)除以身高(m)的平方

2.1基础版
 

[code]#include <iostream>

int main()
{
using namespace std;

int height_input_ft;
int height_input_in;
int weight_input_lb;

int height_in;
double height_m;
double weight_kg;
double bmi;

const int factor_ft_to_in = 12;
const double factor_in_to_m = 0.0254;
const double factor_kg_to_lb = 2.2;

cout << "请按以下格式输入您的身高:\n";
cout << "英尺    英寸    ";
cin >> height_input_ft >> height_input_in;

cout << "请输入您的体重:";
cin >> weight_input_lb;

height_in = height_input_ft * factor_ft_to_in + height_input_in;
height_m = height_in * factor_in_to_m;
weight_kg = weight_input_lb / factor_kg_to_lb;

bmi = weight_kg/(height_m * height_m);

cout << "您的身高为:" << height_in << " in,即" <<  height_m << " m\n";
cout << "您的体重为:" << weight_kg << " kg\n";
cout << "您的IBM指数为:" << bmi;

return 0;
}

2.2 函数版

[code]#include <iostream>

int height_input_to_height_in(int, int);
double height_in_to_height_m(int);
double weight_input_lb_to_weight_kg(int);
double get_bmi(double, double);
void show(int, double, double, double);

int main()
{
using namespace std;

int height_input_ft;
int height_input_in;
int weight_input_lb;

cout << "请按以下格式输入您的身高:\n";
cout << "英尺(空格)英寸:";
cin >> height_input_ft >> height_input_in;

cout << "请输入您的体重:";
cin >> weight_input_lb;

int height_in = height_input_to_height_in(height_input_ft, height_input_in);
double height_m = height_in_to_height_m(height_in);
double weight_kg = weight_input_lb_to_weight_kg(weight_input_lb);
double bmi = get_bmi(weight_kg, height_m);

show(height_in, height_m, weight_kg, bmi);

return 0;
}

//输入的英尺英寸数转英寸
int height_input_to_height_in(int height_input_ft, int height_input_in)
{
const int factor_ft_to_in = 12;
int height_in = height_input_ft * factor_ft_to_in + height_input_in;

return height_in;
}
//英寸转米
double height_in_to_height_m(int height_in)
{
const double factor_in_to_m = 0.0254;
double height_m = height_in * factor_in_to_m;

return height_m;
}
//磅转千克
double weight_input_lb_to_weight_kg(int weight_input_lb)
{
const double factor_kg_to_lb = 2.2;
double weight_kg = weight_input_lb / factor_kg_to_lb;

return weight_kg;
}
//计算BMI
double get_bmi(double weight_kg, double height_m)
{
double bmi = weight_kg/(height_m * height_m);
return bmi;
}
//显示
void show(int height_in, double height_m, double weight_kg, double bmi)
{
using namespace std;
cout << "您的身高为:" << height_in << " in,即" <<  height_m << " m\n";
cout << "您的体重为:" << weight_kg << " kg\n";
cout << "您的IBM指数为:" << bmi;
}

3. 编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。
   1 度为60分,1分等于60秒,请以符号常量的方式表示这些值,对于每个输入值,应使用一个独立的变量存储它。
   下面是该程序运行的情况:
    Enter a latitude in degrees,  minutes, and seconds:
    First ,enter the degrees: 37
    Next ,enter the minutes of arc: 51
    Finally, enter the seconds of arc: 19
    37 degrees, 51 minutes, 19 seconds = 37.8553 degrees

3.1 基础版

[code]#include <iostream>

int main()
{
using namespace std;

double degrees;
double minutes;
double seconds;
double output_degrees;
const int factor = 60;

cout << "Enter a latitude in degrees,  minutes, and seconds:\n";
cout << "First ,enter the degrees: ";
cin >> degrees;
cout << "Next ,enter the minutes of arc: ";
cin >> minutes;
cout << "Finally, enter the seconds of arc: ";
cin >> seconds;

output_degrees = degrees + minutes / factor + seconds / (factor*factor);
cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = "
<< output_degrees << " degrees";

return 0;
}

3.2 字符串数组+函数版

[code]#include <iostream>

void get_degrees(double degrees, double minutes, double seconds, const int factor);

int main()
{
using namespace std;

double degrees;
double minutes;
double seconds;
const int factor = 60;

char tips[100] = "Enter a latitude in degrees,  minutes, and seconds:\n";
char first[100] = "First ,enter the degrees: ";
char next[100] = "Next ,enter the minutes of arc: ";
char finally[100] = "Finally, enter the seconds of arc: ";

cout << tips;
cout << first;
cin >> degrees;
cout << next;
cin >> minutes;
cout << finally;
cin >> seconds;

get_degrees(degrees, minutes, seconds, factor);

return 0;
}

void get_degrees(double degrees, double minutes, double seconds, const int factor)
{
using namespace std;

double output_degrees = degrees + minutes / factor + seconds / (factor*factor);
cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = "
<< output_degrees << " degrees";
}

4. 编写一个程序,要求用户以整数的方式输入秒数(使用long或long long变量存储),
   然后以天,小时、分钟和秒的方式显示这段时间。
   使用符号常量来表示每条有多少小时,每小时有多少分钟以及每分钟有多少秒
   该程序的输出应与下面类似:
   Enter the number of second: 31600000
   31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

4.1 基础版

[code]#include <iostream>

int main()
{
using namespace std;

long input_seconds;
const int factor1 = 24;
const int factor2 = 60;

int days;
int hours;
int minutes;
int seconds;

cout << "Enter the number of second: ";
cin >> input_seconds;

days = input_seconds / (factor1*factor2*factor2);
hours = input_seconds % (factor1*factor2*factor2) / (factor2*factor2);
minutes = input_seconds % (factor1*factor2*factor2) % (factor2*factor2) / factor2;
seconds  = input_seconds % (factor1*factor2*factor2) % (factor2*factor2) % factor2;

cout << input_seconds << "seconds = " << days << " days, "
<< hours << " hours, " << minutes << " minutes, "
<< seconds << " seconds ";

return 0;
}

5. 编写一个程序,要求用户输入全球当前人口和美国当前的人口(或其他国家的人口),将这些信息存储在long long变量中,
   并让程序员显示美国(或其他国家)的人口占全球人口的百分比。
   该程序输出应与下面类似:
   Enter the world's population: 6898758899
   Enter the population of the US: 310789781
   The population of the US is 4.50492% of the world population.

5.1 基础版

[code]#include <iostream>

int main()
{
using namespace std;

long long world;
long long us;

cout << "Enter the world's population: ";
cin >> world;
cout << "Enter the population of the US: ";
cin >> us;
cout << "The population of the US is " << us * 100.000/ world << "% of the world population.";

return 0;
}

6. 编写一个程序,要求用户输入驱车里程(英里)和汽油使用量(加仑),然后指出汽车耗油量为一加仑的里程。
   如果愿意,也可以让程序要求用户以公里为单位输入距离,然后指出欧洲风格的结果————即每100公里的耗油量(升)。

6.1 基础版

[code]//输入驱车里程(英里)和汽油使用量(加仑)
#include <iostream>

int main()
{
using namespace std;
double mi;
double gal;

cout << "请输入驱车里程(英里): ";
cin >> mi;
cout << "请输入耗油量(加仑): ";
cin >> gal;

cout << "汽车的耗油量是" << mi / gal << "英里/加仑\n";

return 0;
}
//以公里为单位输入距离,然后指出欧洲风格的结果
#include <iostream>

int main()
{
using namespace std;
double km;
double l;

cout << "请输入驱车里程(公里): ";
cin >> km;
cout << "请输入耗油量(L): ";
cin >> l;

cout << "汽车的耗油量是" << l / km * 100 << "L/100km\n";

return 0;
}

6.2 while+switch+函数版

[code]#include <iostream>

void mi_and_gal();
void km_and_l();

int main()
{
using namespace std;

char choice;

cout << "请输入驱车里程和汽油使用量\n";
cout << "请选择您要输入的单位类型:\n";
cout << " a. 英里 加仑       b. 公里 升\n";
while(cin >> choice && choice != 'q')
{
switch (choice)
{
case 'A' :
case 'a' : mi_and_gal(); break;

case 'B' :
case 'b' : km_and_l(); break;

default     : cout << "请输入a或b: "; break;
}
}
cout << "end";
return 0;
}

void mi_and_gal()
{
using namespace std;
double mi;
double gal;

cout << "请输入驱车里程(英里): ";
cin >> mi;
cout << "请输入耗油量(加仑): ";
cin >> gal;

cout << "汽车的耗油量是" << mi / gal << "英里/加仑\n";
cout << "\n";
cout << "请选择您要输入的单位类型:\n";
cout << " a. 英里 加仑       b. 公里 升\n";
}

void km_and_l()
{
using namespace std;
double km;
double l;

cout << "请输入驱车里程(公里): ";
cin >> km;
cout << "请输入耗油量(L): ";
cin >> l;

cout << "汽车的耗油量是" << l / km * 100 << "L/100km\n";
cout << "\n";
cout << "请选择您要输入的单位类型:\n";
cout << " a. 英里 加仑       b. 公里 升\n";
}

7. 编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),
   然后将其转换为美国风格的耗油量————每加仑多少英里。
   注意,除了使用不同的单位外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反
   100 公里等于62.14 英里 ,1加仑等于3.875 升
   因此,19mpg合12.4 L/100 km,大约合8.71 L/100 km

7.1 基础版

[code]#include <iostream>

int main()
{
using namespace std;
double l_100km;
double mpg;
const double factor_100km_to_mi = 62.14;
const double factor_gal_to_l = 3.875;

cout << "请输入欧洲风格的耗油量(L/100 km): ";
cin >> l_100km;

mpg = 1/(l_100km/factor_gal_to_l/factor_100km_to_mi);

cout << "美国风格的耗油量为 "<< mpg << "mpg.";

return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐