C++学习笔记第四篇:
C++ primer plus 第四版第2章习题
//习题1:显示姓名和地址
#include <iostream>
using namespace std;
int main(void)
{
cout << "My name is cppshell.\n I live in Beijing.\n";
}
//习题2:浪转换为码(1浪=220码)
#include <iostream>
using namespace std;
int main(void)
{
int lang = 0;
cout << "Enter the lang : ";
cin >> lang;
cout << endl << lang << " lang = " << lang * 220 << " ma.\n";
}
//习题3:自定义函数
#include <iostream>
using namespace std;
//函数原型
void tbm(void);
void shtr(void);
int main(void)
{
//函数调用
tbm();
tbm();
shtr();
shtr();
}
//函数定义
void tbm(void)
{
cout << "Three blind mice\n";
}
void shtr(void)
{
cout << "See how they run\n";
}
//习题4:自定义函数实现温度转换(华氏=1.8*摄氏+32.0)
#include <iostream>
using namespace std;
double c2f(double); //函数原型
int main(void)
{
cout << "Please enter a Celsius value: ";
double celsius = 0.0;
cin >> celsius; //输入摄氏
cout << celsius << " degrees Celsius is " << c2f(celsius) << " degrees Fahrenheit.";
}
//函数定义 摄氏转华氏
double c2f(double c)
{
return c*1.8+32.0;
}
//习题5:自定义函数实现光年转天文单位(1光年=63240天文单位
#include <iostream>
using namespace std;
double ly2au(double); //函数原型
int main(void)
{
cout << "enter the number of light years: ";
double ly=0;
cin >> ly; //输入光年
cout << ly << " light years are " << ly2au(ly) << " astronomical units.";
}
//函数定义
double ly2au(double l)
{
return l*63240;
}