C++ Primer Plus 第六版 第二章练习答案
#include <iostream>
using namespace std;
void p2_1 ( void ) {
cout << "Raywit" << endl;
cout << "China\n" ;
return ;
}
void p2_2 ( void ) {
int Longs = 0 ;
int Yards = 0 ;
cout << "Input the distance in long: " ;
cin >> Longs;
Yards = 220 * Longs;
cout << "The distance in yards is: " << Yards << endl;
return ;
}
void function1 ( void ) {
cout << "Three blind mice" << endl;
return ;
}
void function2 ( void ) {
cout << "See how they run\n" ;
return ;
}
void p2_3 ( void ) {
function1 ( ) ;
function1 ( ) ;
function2 ( ) ;
function2 ( ) ;
return ;
}
void p2_4 ( void ) {
int years = 0 ;
int months = 0 ;
cout << "Enter your age: " ;
cin >> years;
months = years * 12 ;
cout << "Your age is " << months << " monthes!" << endl;
return ;
}
double Cel2Fah ( double Celsius) {
return ( 1.8 * Celsius + 32.0 ) ;
}
void p2_5 ( void )
{
double Celsius = 0.0 ;
double Fahrenheit = 0.0 ;
cout << "Please enter a Celsius value: " ;
cin >> Celsius;
Fahrenheit = Cel2Fah ( Celsius) ;
cout << Celsius << " degrees Celsius is " << Fahrenheit << " defrees Fahrenheit." << endl;
return ;
}
double LightYears2Astronomical ( double LightYears) {
return ( LightYears * 63240 ) ;
}
void p2_6 ( void ) {
double lightyears = 0 ;
double astronomical = 0 ;
cout << "Enter the number of light years: " ;
cin >> lightyears;
astronomical = LightYears2Astronomical ( lightyears) ;
cout << lightyears << " light years = " << astronomical << " astronomical units." << endl;
return ;
}
void DispTime ( int hours, int minutes) {
cout << "Time: " << hours << ":" << minutes << endl;
return ;
}
void p2_7 ( void ) {
int hours = 0 ;
int minutes = 0 ;
cout << "Enter the number of hours: " ;
cin >> hours;
cout << "Enter the number of minutes: " ;
cin >> minutes;
DispTime ( hours, minutes) ;
return ;
}
int main ( int argc, char * * argv) {
cout << "=====================================\n"
<< "============ Chapter2: ============\n"
<< "=====================================\n\n" ;
cout << "==>> Practice 2_1:\n" ;
p2_1 ( ) ;
cout << endl;
cout << "==>>Practice 2_2:\n" ;
p2_2 ( ) ;
cout << endl;
cout << "==>>Practice 2_3:\n" ;
p2_3 ( ) ;
cout << endl;
cout << "==>>Practice 2_4:\n" ;
p2_4 ( ) ;
cout << endl;
cout << "==>>Practice 2_5:\n" ;
p2_5 ( ) ;
cout << endl;
cout << "==>>Practice 2_6:\n" ;
p2_6 ( ) ;
cout << endl;
cout << "==>>Practice 2_7:\n" ;
p2_7 ( ) ;
cout << endl;
}