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

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

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

源代码如下:

#include //编译预处理指令
using namespace std;  //使用std命名空间
int main()    //主函数
{
    cout<<"姓名,"<<" 地址."<

ps:很简单的入门程序。

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

源代码如下:

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

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

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

Three blind mice

Three blind mice

See how they run

See how they run

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

源代码如下:

#include 
using namespace std;
void text01()//创建函数来实现输出打印的信息
{
    cout<<"Three blind mice"<

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

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

Enter your age: 29

Your age in months is 348.

源代码如下

#include 
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 "<

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

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

Please enter a Celsius value: 20

20 degrees Celsius is 68 degrees Fahrenheit.

下面是转化公式:

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

源代码如下:

#include 
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<

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 
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<

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

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

        Enter the number of hours : 9

        Enter the number of minutes : 28

        Time : 9:28

源代码如下:

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

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

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

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

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

与君共勉!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1354298.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-14
下一篇 2022-06-14

发表评论

登录后才能评论

评论列表(0条)