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

《C++ Primer Plus 第六版》编程练习参考答案(第三章)

2018-12-06 18:19 330 查看

第三章

第一题:

[code]#include <iostream>
using namespace std;
const int TransFactor=12;

int main()
{
int height=0;
int inch=0;
int foot=0;

cout<<"Enter the height of you in inch: ___\b\b\b";
cin>>height;
foot=height/TransFactor;
inch=height%TransFactor;
cout<<"Your height is "<<foot<<" foot and "<<inch<<" inch";
}

第二题:

[code]#include <iostream>
using namespace std;
const double HeightTrans=0.0254;
const double WeightTrans=2.2;
const int InchFootTrans=12;

int main()
{
int inch=0;
int foot=0;
double pound=0;
double meter=0;
double kilogram=0;
double BMI;

cout<<"Enter the height of you in foot and inch: ";
cin>>foot>>inch;
meter=HeightTrans*(foot*InchFootTrans+inch);
cout<<"Enter the weight of you in pound: ";
cin>>pound;
kilogram=pound/WeightTrans;

BMI=kilogram/(meter*meter);
cout<<"The BMI index of you is: "<<BMI;
}

第三题:

[code]#include <iostream>
using namespace std;
const double TransFactor=60;

int main()
{
double degrees=0;
double minutes=0;
double seconds=0;
double degrees_trans=0;

cout<<"Enter a latitude in degrees,minutes,and seconds: "<<endl;
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;

degrees_trans=degrees+minutes/TransFactor+seconds/(TransFactor*TransFactor);
cout<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds= "
<<degrees_trans<<" degrees.";
}

第四题:

[code]#include <iostream>
using namespace std;
const long TransFactorMinutes=60;
const long TransFactorHours=60*60;
const long TransFactorDays=60*60*24;

int main()
{
long seconds_full=0;
long days=0;
long hours=0;
long minutes=0;
long seconds=0;

cout<<"Enter the number of seconds: ";
cin>>seconds_full;
days=seconds_full/TransFactorDays;
hours=(seconds_full%TransFactorDays)/TransFactorHours;
minutes=((seconds_full%TransFactorDays)%TransFactorHours)/TransFactorMinutes;
seconds=((seconds_full%TransFactorDays)%TransFactorHours)%TransFactorMinutes;

cout<<seconds_full<<" seconds= "<<days<<" days, "<<hours<<" hours, "
<<minutes<<" minutes, "<<seconds<<" seconds.";
}

第五题:

[code]#include <iostream>
using namespace std;

int main()
{
long long USA=0;
long long world=0;
double percent=0;

cout<<"Enter the world's population: ";
cin>>world;
cout<<"Enter the population of the US: ";
cin>>USA;
percent=((double)USA/world)*100;
cout<<"The population of the US is "
<<percent<<"% of the world population.";
}

第六题:

[code]#include <iostream>
using namespace std;

int main()
{
double distance;
double gasoline;
double consumption;

cout<<"Enter the distance in kilometer: ";
cin>>distance;
cout<<"Enter the gasoline in litre: ";
cin>>gasoline;
consumption=(gasoline/distance)*100;
cout<<"The gasoline consumption of your car is "
<<consumption<<" litre per 100 kilometer";
}

第七题:

[code]#include <iostream>
using namespace std;
const double DistanceTrans=62.14;
const double GasolineTrans=3.875;

int main()
{
double litre=0;
double kilometer=0;
double EUconsumption=0;
double gallon=0;
double mile=0;
double USconsumption=0;

cout<<"Enter the distance in kilometer: ";
cin>>kilometer;
cout<<"Enter the gasoline in litre: ";
cin>>litre;
EUconsumption=(litre/kilometer)*100;
cout<<"In EU, The gasoline consumption of your car is "
<<EUconsumption<<" litre per 100 kilometer"<<endl;

gallon=litre/GasolineTrans;
mile=(kilometer/100)*DistanceTrans;
USconsumption=mile/gallon;
cout<<"In the US, The gasoline consumption of your car is "
<<USconsumption<<" mile per gallon";
}

 

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