//2.7.2transformLongToCode
#include <iostream>
int main() {
using namespace std;
cout << "Please enter a distance in long: ";
int longDistance;
cin >> longDistance;
cout << longDistance << " long = " << longDistance * 220 << " yard ";
cout << endl;
return 0;
}
//2.7.3defineFunction
#include <iostream>
void function1();
void function2(); // function prototype
using namespace std;
int main()
{
function1();
function1();
function2();
function2();
return 0;
}
void function1(void)
{
cout << "Three blind mice" << endl;
}
void function2(void)
{
cout << "See how they run" << endl;
}
//2.7.4transformAgeToMonth
#include <iostream>
int main()
{
int age;
std::cout << "Enter your age : ";
std::cin >> age;
std::cout << "Your age in month is " << age * 12 << "." << std::endl;
return 0;
}
//2.7.5transformFahrenheitToCelsius
#include <iostream>
double fahrenheit(double);
using namespace std;
int main() {
double celsius;
cout << "Please enter a Celsius value: ";
cin >> celsius;
cout << celsius
<< " degrees Ceisius is "
<< fahrenheit(celsius)
<< " degrees Faherenheit.";
return 0;
}
double fahrenheit(double celsius) {
double fahrenheit;
fahrenheit = 1.8 * celsius + 32.0;
return fahrenheit;
}
// 2.7.6trancformLightYearToAU
#include <iostream>
double transformLightYearToAU(double);
int main()
{
double lightYear;
std::cout << "Enter thr number of light year: ";
std::cin >> lightYear;
std::cout << lightYear << " light year = "
<< transformLightYearToAU(lightYear) << " astronomical units."
<< std::endl;
return 0;
}
double transformLightYearToAU(double lightYear)
{
double AU;
AU = lightYear * 63240.0;
return AU;
}
//2.7.7outputTime
#include <iostream>
void outputTime(int, int);
using namespace std;
int main()
{
int hour, min;
cout << "Enter the number of hours: ";
cin >> hour;
cout << "Enter the number of minutes: ";
cin >> min;
outputTime(hour, min);
return 0;
}
void outputTime(int hour, int min)
{
cout << "Time: " << hour << ":" << min;
}
c++ primer plus 习题 capture2
最新推荐文章于 2024-09-17 16:12:30 发布
该系列C++代码实现了不同单位之间的转换,包括将长距离转换为码,定义并调用函数,年龄转换为月,华氏温度转换为摄氏温度,光年转换为天文单位,以及输出时间。每个程序都包含输入、计算和输出相应转换结果的逻辑。
摘要由CSDN通过智能技术生成