《C++Primer Plus》 第二章 |开始学习C++| 编程练习题

问题一:编写一个C++程序,它显示您的姓名和地址。

源代码如下:

#include <iostream>//编译预处理指令
using namespace std;  //使用std命名空间
int main()    //主函数
{
    cout<<"姓名,"<<" 地址."<<endl;
    
    cin.get();  //按任意键继续
    return 0;

ps:很简单的入门程序。

问题二:编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于220码)。

源代码如下:

#include <iostream>
using namespace std;
int m_length(int n)   //定义m_length()函数,函数值
{
    return n*220;
}
int main()
{
    int length;
    cout<<"请输入有几long"<<endl;
    cin>>length;
    cin.get();
    int code=m_length(length);  //调用函数来给变量初始化
    cout<<length<<" long"<<" = "<<code<<" 码"<<endl;
    cin.get();
    return 0;
}

ps:创建函数,通过返回值来实现 1long=220码的公式。

问题三:编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:

Three blind mice

Three blind mice

See how they run

See how they run

其中一个函数要被调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。

源代码如下:

#include <iostream>
using namespace std;
void text01()//创建函数来实现输出打印的信息
{
    cout<<"Three blind mice"<<endl;
}
void text02()
{
    cout<<"See how they run"<<endl;
}
int main()
{
    text01();//调用函数
    text01();
    text02();
    text02();
    cin.get();
}

ps:学习用户自定义函数的创建和函数的调用。        

问题四:编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:

Enter your age: 29

Your age in months is 348.

源代码如下

#include <iostream>
using namespace std;
int m_month(int n)  //定义m_month()的函数,并返回年龄*12的值
{
    return n*12;
}
int main()
{
    int age;
    cout<<"Enter your age: ";
    cin>>age;
    cin.get();
    int month=m_month(age);//   调用函数给month赋值
    cout<<"Your age in month is "<<month<<endl;
    cin.get();
    return 0;
}

ps:这道题和第二题类似,利用返回值很轻松。

问题五:编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:

Please enter a Celsius value: 20

20 degrees Celsius is 68 degrees Fahrenheit.

下面是转化公式:

                                        华氏温度=1.8 x 摄氏温度+32.0

源代码如下:

#include <iostream>
using namespace std;
double m_Fahrenheit(double Celsius)//定义函数,以摄氏温度为参数,返回华氏温度
{
    return 1.8*Celsius+32.0;
}
int main()
{
    double celsius;//定义变量,表示摄氏温度
    cout<<"Enter the number of light years: ";
    cin>>celsius;
    cin.get();
    cout<<celsius<<" degrees Celsius is "<<m_Fahrenheit(celsius)<<" degrees Fahrenheit"<<endl;
    cin.get();
}

ps:依旧用返回值去解。

问题六:编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。

该程序按下面的格式要求用户输入光年值,并显示结果:

        Enter the number of light years: 4.2

        4.2 light years = 265608 astronomical units.

天文单位是从地球到太阳的平均距离(约150000000 公里或93000000 英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式为:

                                                                1光年=63240天文单位

源代码如下:

#include <iostream>
using namespace std;
double m_Aml(double Linghtyears)//定义一个函数,以光年值为单位,并返回一个天文单位的值
{
    return Linghtyears *63240;
}
int main()
{
    double lightyears; //定义变量,表示光年
    cout<<"Enter the number of light years: ";
    cin>>lightyears;
    cin.get();
    cout<<lightyears<<" light years = "<<m_Aml(lightyears)<<" astronomical units."<<endl;
    cin.get();
}

ps:看似复杂,但其实和上面的一样,掌握定义函数,并返回对应值真的很轻松。

问题七:编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void 函数,后者以下面这样的格式显示这两个值:

        Enter the number of hours : 9

        Enter the number of minutes : 28

        Time : 9:28

源代码如下:

#include <iostream>
using namespace std;
void m_Time(int h,int m)//  定义空类型函数,参数是小时和分钟
{
    cout<<"Enter the number of hours : "<<h<<endl;
    cout<<"Enter the number of minutes : "<<m<<endl;
    cout<<"Time:  "<<h<<":"<<m<<endl;
}
int main()
{
    int H;//定义变量 小时
    int M;//定义变量 分钟
    cout<<"请输入小时数: ";
    cin>>H;
    cin.get();
    cout<<"请输入分钟数: ";
    cin>>M;
    cin.get();
    m_Time(H, M);//调用函数
    cin.get();
    return 0;

ps:《C++Primer Plus》第二章的章节练习题就练习完了,博主由于刚踏入C++不久,一些标准用语还不太熟悉,注释也是按照自己能够理解的含义进行标注的,本博客也仅供学习和参考,难免会有错误,请诸位多多包涵,如有侵权敬请告知。

(参考资料《C++Primer Plus》第六版中文版)

创作不易,如对您有所帮助,还望点赞+收藏

希望能在以后的日子里学习到更多有用的知识,欢迎大家来到评论区留言您宝贵的建议

与君共勉!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值