习题3.1
#include<iostream>
using namespace std;
int main()
{
const int FeetTransInches = 12;
int height_inches, height_feet, remain_inches;
cout << "Please enter height(inches):__\b\b ";
cin >> height_inches;
height_feet = height_inches / FeetTransInches;
remain_inches = height_inches % FeetTransInches;
cout << "My height is " << height_feet << " feet, and " << remain_inches << " inches.\n";
return 0;
}
执行结果
Please enter height (inches): 68
My height is 5 feet, and 8 inches.
习题3.2
#include<iostream>
using namespace std;
int main()
{
const int FeetTransInches = 12;
const double InchesTransMeter = 0.0254;
const double KiloTransPound = 2.2;
int height_feet, height_inches;
double weight_pound, weight_kilo;
double height_meter;
double BMI;
cout << "Enter your height of feet part: ";
cin >> height_feet;
cout << "Enter your height of inches part: ";
cin >> height_inches;
cout << "Enter your weight (Pound):";
cin >> weight_pound;
height_meter = (height_feet * FeetTransInches + height_inches) * InchesTransMeter;
weight_kilo = weight_pound / KiloTransPound;
BMI = weight_kilo / (height_meter * height_meter);
cout << "Your height is " << height_meter << " (meter)\n";
cout << "Your weight is " << weight_kilo << " (kilogram)\n";
cout << "Your body mass index (BMI) is " << BMI << endl;
return 0;
}
执行结果
Enter your height of feet part: 5
Enter your height of inches part: 12
Enter your weight (Pound): 140
Your height is 1.8288 (meter)
Your weight is 63.6364 (kilogram)
Your body mass index (IBM) is 19.0271
习题3.3
#include<iostream>
using namespace std;
int main()
{
const int deg_of_min = 60;
const int min_of_sec = 60;
int degress, minutes, seconds;
double degr;
cout << "Enter a latitude in degrees, minutes, and seconds:\n";
cout << "First, enter the degrees: ";
cin >> degress;
cout << "Next, enter the minutes of arc: ";
cin >> minutes;
cout << "Finally, enter the seconds of arc: ";
cin >> seconds;
degr = degress + double(minutes) / deg_of_min + double(seconds) / (deg_of_min * min_of_sec);
cout << degress << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << degr << " degrees";
return 0;
}
执行结果
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.4
#include<iostream>
using namespace std;
int main()
{
const int day_of_hours = 24, hour_of_minutes = 60, minute_of_seconds = 60;
long seconds;
int Days, Hours, Minutes, Seconds;
cout << "Enter the number of seconds: ";
cin >> seconds;
Days = seconds / (day_of_hours * hour_of_minutes * minute_of_seconds);
Hours = (seconds % (day_of_hours * hour_of_minutes * minute_of_seconds)) / (hour_of_minutes * minute_of_seconds);
Minutes = ((seconds % (day_of_hours * hour_of_minutes * minute_of_seconds)) % (hour_of_minutes * minute_of_seconds)) / (minute_of_seconds);
Seconds = ((seconds % (day_of_hours * hour_of_minutes * minute_of_seconds)) % (hour_of_minutes * minute_of_seconds)) % (minute_of_seconds);
cout << seconds << " seconds = " << Days << " days, " << Hours << " hours, " << Minutes << " minutes, " << Seconds << " seconds\n";
return 0;
}
执行结果
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
习题3.5
#include<iostream>
using namespace std;
int main()
{
long long World_Popula, US_Popula;
double rate;
cout << "Enter the world's population: ";
cin >> World_Popula;
cout << "Enter the population of the US: ";
cin >> US_Popula;
rate = double(US_Popula) / World_Popula * 100;
cout << "The population of the US is " << rate << "% of the world population.\n";
return 0;
}
执行结果
Enter the world's population: 6898758899
Enter the population of the US: 310783781
The population of the US is 4.50492% of the world population