最近开始学习C++,用C++primer plus 这本书,里面每个章节有复习题和编程练习题,这里只记录一下书中的编程练习题。(复习题都有参考答案,网上很容易就能找到,有需要的我发你)。 第二章的练习题与比第三章的简单,所以直接从第三章开始记录,第三章的搞懂做第二章的习题没有任何问题。 第三章 处理数据 3.7 编程练习 3.1
#include<iostream>
int main()
{
using namespace std;
const int conversion_factor = 12;
int body_height_inchs;
int body_height_foots;
int inch1;
cout << "Enter your height is " << "__\b\b" << " inchs ";
cin >> body_height_inchs;
cout << "your height is " << body_height_inchs << " inchs "<<endl;
body_height_foots = body_height_inchs / conversion_factor;
inch1 = body_height_inchs % conversion_factor;
cout << body_height_inchs << " inchs are " << body_height_foots << " foots " << "and " << inch1 << " inchs\n";
return 0;
}
3.2
#include<iostream>
int main()
{
using namespace std;
const int in_t_fo_conversion_factor = 12;
const double m_to_in_conversion_factor = 0.0254;
const double k_to_p_conversion_factor = 2.2;
cout.setf(ios_base::fixed, ios_base::floatfield);
int body_height_inchs;
int body_height_foots;
int body_weights_p;
double body_height_m;
double body_weights_k;
double BMI;
cout << "Enter your height is (units foots)";
cin >> body_height_foots;
cout << "Enter your height is (units inch)";
cin >> body_height_inchs;
cout << "Enter your weight is (units pounds)";
cin >> body_weights_p;
cout << "your height is " << body_height_foots << " foot "<<"and "<< body_height_inchs<<" inchs.\n";
cout << "your weight is " << body_weights_p << " pounds. " << endl;
cout << "start convert !!" << endl;
body_height_m = (body_height_foots * in_t_fo_conversion_factor + body_height_inchs) * m_to_in_conversion_factor;
cout << "your heignt is " << body_height_m << " m " << endl;
body_weights_k = body_weights_p /k_to_p_conversion_factor;
cout << "your weight is " << body_weights_k << " kg " << endl;
BMI = body_weights_k / body_height_m / body_height_m;
cout << "your BMI is " << BMI << endl;
return 0;
}
3.3
#include<iostream>
int main()
{
using namespace std;
const int degrees_to_minutes_conversion_factor = 60;
const int minutes_to_seconds_conversion_factor = 60;
int degrees;
int minutes;
int seconds;
double act_latitude;
cout << "Enter a latitude in degrees, minutes, and seconds:\n";
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;
act_latitude = double(degrees) + (double(minutes) / degrees_to_minutes_conversion_factor) + (double(seconds) / minutes_to_seconds_conversion_factor / degrees_to_minutes_conversion_factor);
cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << act_latitude << " degrees\n";
return 0;
}
3.4
习题3.4
#include<iostream>
int main()
{
using namespace std;
const int perday_to_perhour = 24;
const int perhour_to_perminute = 60;
const int perminute_to_second = 60;
int days;
int minutes;
int hours;
int seconds;
long long total_seconds;
cout << "Enter the number of seconds: ";
cin >> total_seconds;
days = total_seconds /(perday_to_perhour * perhour_to_perminute * perminute_to_second);
hours = total_seconds / perminute_to_second / perhour_to_perminute % perday_to_perhour;
minutes = total_seconds / perminute_to_second % perhour_to_perminute;
seconds = total_seconds % perminute_to_second;
cout << total_seconds << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, "<< seconds<<" seconds \n";
return 0;
}
3.5
习题3.5
#include<iostream>
int main()
{
using namespace std;
long long world_population;
long long native_population;
double percengtage;
cout << "Enter the world population: ";
cin >> world_population;
cout << "Enter the population of the US: ";
cin >> native_population;
percengtage = double(native_population) / (world_population) * 100;
cout << " The population of the US is " << percengtage << "% of the world population.\n";
return 0;
}
3.6
习题3.6
#include<iostream>
int main()
{
using namespace std;
char SIGH;
cout << "Choice your input 'm'indicates that the units entered are miles and gallons. OR input 'k' indicates the units entered are kilometers and liters " << endl;
cout << "input is _\b";
cin >> SIGH;
if (SIGH == 'm')
{
double m_distance;
double m_gasoline;
cout << "Enter the miles of distance you have driven:";
cin >> m_distance;
cout << "Enter the gallons of gasoline you have used:";
cin >> m_gasoline;
cout << "Your car can run " << m_distance / m_gasoline << " miles per gallon\n";
}
if (SIGH == 'k')
{
double k_distance;
double k_gasoline;
cout << "Enter the distance in kilometers:";
cin >> k_distance;
cout << "Enter the petrol in liters:";
cin >> k_gasoline;
cout << "In European style: " << "your can used " << 100 * k_gasoline / k_distance << " liters of petrol per 100 kilometers\n";
}
return 0;
}
3.7
#include<iostream>
int main()
{
using namespace std;
cout << "Enter the automobile gasoline consumption figure in\n";
cout << "Europeanstyle(liters per 100 kilometers):";
double European_style;
cin >> European_style;
cout << "Converts to U.S. style(miles per gallon):" << endl;
cout << European_style << "L/100Km=" << 62.14 * 3.875 / European_style << "mpg\n";
return 0;
}
|