C++学习笔记第四篇(转)

本文提供C++ Primer Plus第四版第2章的习题解答,包括显示个人信息、长度单位转换、自定义函数实现温度及距离单位转换等示例代码。
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;
}

转载于:https://www.cnblogs.com/silvester/archive/2007/05/19/1694806.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值