C++ Primer Plus(第六版)第三章课后习题
3.7.1
#include
int main()
{
using namespace std;
double height;
const int change=12;
int foot, inch;
cout <<“Please enter your height:_______\b\b\b\b\b\b\b” ;
cin >> height;
foot= height;
inch= (height- foot)*change;
cout <<“Your height is " << foot <<” foots and “<<inch<<” inches."<<endl;
return 0;
}
3.7.2
#include
int main()
{
using namespace std;
const double foot_inch=12, inch_meter=0.0254, pound_kilogram=1.0/2.2;
double meter, kilogram;
int foot, inch, pound;
cout <<" Please enter your height(feet and inches):\b\b\b\b\b";
cin >> foot;
cout <<"\b\b\b\b\b";
cin >> inch;
cout <<" Please enter your weight(pounds):_____\b\b\b\b";
cin >>pound;
meter=(foot_inch*foot+inch)inch_meter;
kilogram=pound_kilogrampound;
double BMI=kilogram/meter;
cout << "Your BMI is: "<< BMI <<endl;
return 0;
}
3.7.3
#include
int main()
{
using namespace std;
const double degree_minute=60, minute_second=60;
int degree, minute, second;
cout <<" Enter a latitude in degrees, minutes, and seconds:\n";
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 change=degree+(second/minute_second+minute)/degree_minute;
cout << degree << " degrees, “<< minute <<” minutes, “<< second <<” seconds = "
<< change <<” degrees”;
return 0;
}
3.7.4
#include
int main()
{
using namespace std;
const int day_hour=24, hour_minute=60,minute_second=60;
long second;
cout << "Enter the number of seconds: “;
cin >> second;
int day, hour, minute, seconds;
day=second/(day_hourhour_minuteminute_second);
long a=second-day*(day_hourhour_minuteminute_second);
hour=a/(hour_minuteminute_second);
a=a-hour(hour_minuteminute_second);
minute=a/minute_second;
a=a-minuteminute_second;
seconds=a;
cout << second <<” seconds= “<< day << " days, “<< hour << " hours, " << minute << " minutes, " << seconds <<” seconds”;
return 0;
}
3.7.5
#include
int main()
{
using namespace std;
long long a, b;
cout << "Enter the world’s population: ";
cin >> a;
cout << "Enter the population of the US: ";
cin >> b;
double c=(double) b/ (double) a*100;
cout << "The population of the US is " << c << “% of the world population.”;
return 0;
}
3.7.6
#include
int main()
{
using namespace std;
double mile, gallon;
cout << “Enter the distance that you drive:”;
cin >> mile;
cout << “Enter the weight of gasline that you use:”;
cin >> gallon;
double mile_gallon=mile/gallon;
cout << “When you consume one gallon of gasline, you can go far as " << mile_gallon << " miles”;
return 0;
}
3.7.7
#include
int main()
{
using namespace std;
const double k_m=62.14,g_l=3.875;
double mpg,l_km;
cout <<“Enter the oil consuption in European style:”;
cin >> l_km;
mpg=1/l_kmk_mg_l;
cout << “The oil consuption in American style is " << mpg <<” mpg";
return 0;
}