c++ primer plus 第六版第三章编程练习答案

来源:互联网 发布:淘宝运营公司靠谱吗 编辑:程序博客网 时间:2024/04/30 14:03

3.7.1


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
//一米八是71英寸,foot 英尺,inch英寸
int foot, inch;
cout << "Enter your height(inch):__\b\b";
cin >> inch;
foot = inch / 12;
inch = i
nch % 12;
cout << "Your height is " << foot << " foot " << inch << " inch " << endl;
system("pause");
return 0;
}


3.7.2


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
//一米八是五英尺十一英寸,75kg是165磅
int foot, inch;
float meter, pound, kilogram;
cout << "Input your foot and inch:";
cin >> foot;
cin >> inch;
cout << "Input your pound:";
cin >> pound;

meter = 0.3048*foot + (inch*1.0 / 12)*0.3048;
kilogram = pound / 2.2;

cout << "The BMI of me is " << kilogram / (meter * meter);

system("pause");
return 0;
}


3.7.3


#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
const double change = 60.0; //度分秒之间的进位为60
double degrees, minute, seconds;
cout << "Enter a latitude in degrees, minutes, and seconds: " << endl;
cout << "First, enter the degrees: ";
cin >> degrees;
cout << "Next, enter the minytes of arc: ";
cin >> minute;
cout << "Finally, enter the seconds of arc: ";
cin >> seconds;

cout << degrees << " degrees, "
<< minute << " minute, "
<< seconds << " seconds = ";

       degrees = degrees + (minute) / change + (seconds) / (change * change);

       cout << degrees << " degrees" << endl;

system("pause");
return 0;
}


3.7.4

#include <iostream>

#include <cstdlib>
int main()
{
using namespace std;
const float S_P_M = 60.0; //seconds per minute
const float M_P_H = 60.0; //minutes per hour
const float H_P_D = 24.0; //hours per day

int day, hour, minute, second;

cout << "Enter the number of seconds:";
cin >> second;
day = second / (S_P_M*M_P_H*H_P_D);

hour = (second - day*S_P_M*M_P_H*H_P_D) *1.0 / (S_P_M*M_P_H);

minute = (second - day*S_P_M*M_P_H*H_P_D - hour * S_P_M*M_P_H) / S_P_M;

cout << second << " seconds = ";
second = (second - day*S_P_M*M_P_H*H_P_D - hour * S_P_M*M_P_H - minute * S_P_M);
cout << day << " days, "
<< hour << " hours, "
<< minute << " minutes, "
<< second << " seconds" << endl;

system("pause");
return 0;
}


3.7.5

#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
long long population, US;
cout << "Enter the world's population :";
cin >> population;
cout << "Enter the population of the US:";
cin >> US;
cout << "The population of the US is "
<< US * 100.0 / population
<< "%"
<< "of the world population."
<< endl;
system("pause");
return 0;
}


3.7.6

#include <iostream>
#include <cstdlib>
const double KM100_TO_MILES = 62.14;
const double LITERS_PER_GALLON = 3.875;
int main(void)
{
using namespace std;
double euro_rating;
double us_rating;
cout << "Enter fuel consumption in liters per 100 km: ";
cin >> euro_rating;
us_rating = (LITERS_PER_GALLON * KM100_TO_MILES) / euro_rating;
cout << euro_rating << " liters per 100 km is ";
cout << us_rating << " miles per gallon.\n";
system("pause");
return 0;
}

0 0
原创粉丝点击