您的位置:首页 > 编程语言 > C语言/C++

【第二章】C++ Primer plus 的编程练习题(选取部分)

2018-01-25 14:22 381 查看
/***********************************
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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: