C++ Primer Plus第五版 第三章 编程练习答案

来源:互联网 发布:js scroll属性 编辑:程序博客网 时间:2024/05/16 18:26
/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第三章编程练习 第1题  Problem :  编写一个小程序。要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用 下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。*******************************************************************************************************************/#include <iostream>  const int CHG = 12;using namespace std;  int main()  {  cout << "Enter your height in inch:______\b\b\b\b\b\b";  float height;  cin >> height;  cout << "Your height " << height/CHG << " feet.\n";  system("pause");return 0;  }  


/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第三章编程练习 第2题  Problem :   编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。) 该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将 以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克= 2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。*******************************************************************************************************************/#include <iostream>  using namespace std; const int FT_TO_IN = 12; const double IN_TO_M = 0.0254;  const double P_TO_KG = 1 / 2.2;  int main()  {  cout << "Please enter your height in feet and inches:\n";  int h_ft = 0;  int h_in = 0;  cin >> h_ft >> h_in;  cout << "Your weight in pound:\n";  int w = 0;cin >> w;  cout << "Your BMI is:" << (w * P_TO_KG) / ((((h_ft * FT_TO_IN) + h_in) * IN_TO_M)*(((h_ft * FT_TO_IN) + h_in) * IN_TO_M)) << endl;  system("pause");return 0;  } 

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第三章编程练习 第3题  Problem :编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。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 , 51minutes , 19 seconds = 37.8553 degrees *******************************************************************************************************************/#include <iostream>using namespace std;int main(){int degree =0;int minute =0;int second =0;const double RATIO =60;cout << "Enter a latitude in degree, minutes, and seconds :" << endl;cout << "First, enter the degrees: ";cin >> degree;cout << "Next, enter the minutes of arc: ";cin >> minute;cout << "Finally, enter the seconds of arc: ";cin >> second;double latitude=0;latitude= degree+ minute/RATIO + minute/RATIO /RATIO ;cout << degree << " degrees," << minute << " minutes," << second << " seconds =" << latitude << " degrees ";system("pause");return 0;}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第三章编程练习 第4题  Problem :编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式 显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下列类似: Enter the number of seconds: 31600000 31600000 seconds = 365 days, 17 hours , 46 minutes , 40 seconds *******************************************************************************************************************/#include <iostream>  using namespace std; const int SECONDS_TO_MINUTES = 60; const int MINUTES_TO_HOURS = 60;  const int HOURS_TO_DAYS = 24;  int main()  {  cout << "Enter the number of seconds: ";  long long seconds = 0;cin >> seconds;  cout << seconds << " seconds = " << seconds / SECONDS_TO_MINUTES / MINUTES_TO_HOURS / HOURS_TO_DAYS << " days, " << seconds / SECONDS_TO_MINUTES / MINUTES_TO_HOURS % HOURS_TO_DAYS << " hours, " << seconds / SECONDS_TO_MINUTES %MINUTES_TO_HOURS << " minutes, " << seconds % SECONDS_TO_MINUTES << " seconds." <<endl;  system("pause");return 0;  }  

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第三章编程练习 第5题  Problem :编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意, 也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升)*******************************************************************************************************************/#include <iostream>using namespace std;int main(void){cout << "How many miles have you driven your car(in mile)? ";float miles;cin >> miles;cout << "How many gallons of gasoline did the car use(in gallon)? ";float gallons;cin >> gallons;cout << "Your car got " << miles / gallons;cout << " miles per gallon.\n";cout << "How many miles have you driven your car(in kilometer)? ";float miles;cin >> miles;cout << "How many gallons of gasoline did the car use(in litre)? ";float gallons;cin >> gallons;cout << "Your car got " << miles / gallons;cout << " kilometers per litre.\n";system("pause");return 0;}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第三章编程练习 第6题  Problem :编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的 耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里 等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km。 *******************************************************************************************************************/  #include <iostream>  using namespace std;  const double Y_TO_G = 62.14;  const double C_TO_V = 3.875; int main()  {  cout << "Please enter the European oil consumption: ";  double v = 0;  cin >> v;  cout << "The American oil consumption:: " << Y_TO_G * C_TO_V / v << endl;  system("pause");return 0;  }  


原创粉丝点击