/***********************************
2017年9月23日13:12:16
Athor:xiyuan255
Course:C++
Contain:myfirst.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第二章的练习题(选取部分)
【 P35 】
*************************************/
// review2.cpp -- C++ Primer plus 第二章练习题
#include <iostream>
void ageToMonth(void);
void temperatureFunc(void);
void timeFunc(void);
using namespace std;
int main(void)
{
//ageToMonth(); // test 1
//temperatureFunc(); // test 2
timeFunc(); // test 3
return 0;
}
void ageToMonth(void)
{
cout << "Enter your age: ";
int age;
cin >> age;
cout << age << " age = " << (age*12) << " month." << endl;
}
/**
输出结果:
Enter your age: 29
29 age = 348 month.
*/
int celsiusToFahrenheit(int celsius)
{
return (int)(celsius * 1.8 + 32.0);
}
void temperatureFunc(void)
{
cout << "Please enter a Celsius value: ";
int celsius;
cin >> celsius;
int fahrenheit;
fahrenheit = celsiusToFahrenheit(celsius);
cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit.\n";
}
/**
输出结果:
Please enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit.
*/
void hoursAndMinutesToTime(int hours, int minutes)
{
cout << "Time: " << hours << ":" << minutes << endl;
}
void timeFunc(void)
{
cout << "Enter the number of hours: ";
int hours;
cin >> hours;
cout << "Enter the number of minutes: ";
int minutes;
cin >> minutes;
hoursAndMinutesToTime(hours, minutes);
}
/**
输出结果:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
*/
【第二章】C++ Primer plus 的编程练习题(选取部分)
最新推荐文章于 2023-02-03 15:43:03 发布