习题2.2
#include<iostream>
using namespace std;
int main()
{
const int Long_of_Yard = 220;
int LongDistance;
cout << "Please enter a distance (long): ";
cin >> LongDistance;
cout << LongDistance << " long is equal to " << Long_of_Yard * LongDistance << " Yard\n";
return 0;
}
执行结果:
Please enter a distance (long): 25
25 long is equal to 55500 Yard
习题2.3
#include<iostream>
using namespace std;
void PrintFunction1();
void PrintFunction2();
int main()
{
PrintFunction1();
PrintFunction1();
PrintFunction2();
PrintFunction2();
return 0;
}
void PrintFunction1()
{
cout << "Three blind mice\n";
}
void PrintFunction2()
{
cout << "See how they run\n";
}
习题2.4
#include<iostream>
using namespace std;
int main()
{
const int Year_of_Months = 12;
int age;
cout << "Enter your age: ";
cin >> age;
cout << age << " years is equal to " << age * Year_of_Months << " months\n";
return 0;
}
执行结果为
Enter your age: 25
25 years is equal to 300 months
习题2.5
#include<iostream>
using namespace std;
double CelsiusTransFahren(double);
int main()
{
double Celsius, Fahrenheit;
cout << "Please enter a Celsius values: ";
cin >> Celsius;
Fahrenheit = CelsiusTransFahren(Celsius);
cout << Celsius << " degrees Celsius is " << Fahrenheit << " degrees Fahrenheit.\n";
return 0;
}
double CelsiusTransFahren(double celsius)
{
return 1.8 * celsius + 32.0;
}
执行结果
Please enter a Celsius values: 20
20 degrees Celsius is 68 degrees Fahrenheit.
习题2.6
#include<iostream>
using namespace std;
double LightTransAstronmical(double);
int main()
{
double LightYears, AstronmicalUints;
cout << "Enter the number of light years: ";
cin >> LightYears;
AstronmicalUints = LightTransAstronmical(LightYears);
cout << LightYears << " light years = " << AstronmicalUints << " astronmical units.\n";
return 0;
}
double LightTransAstronmical(double years)
{
return years * 63240;
}
执行结果
Enter the number of light years: 4.2
4.2 light years = 265608 astronmical units.
习题2.7
#include<iostream>
using namespace std;
void TimeShow(int, int);
int main()
{
int hours, minutes;
cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
TimeShow(hours, minutes);
return 0;
}
void TimeShow(int Hours, int Minutes)
{
cout << "Time: " << Hours << ":" << Minutes << endl;
}
执行结果
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28