系统学习C++(2)

第一部分是C++的一些基本的知识,还没有涉及成段的代码,而且都是一些零散的知识点,第二部分,涉及了函数的知识,代码涉及的知识点会多一点。


练习6.4:编写一个与用户交互的函数,要求用户输入一个数字,计算生成该数字的阶乘。在main函数中调用该函数。

程序实现:

#include <iostream>

using namespace std;

int factorial(int i)
{
    int factor = 1;
    if(i==0 || i==1)
        return 1;
    else
    {
        while(i>1)
            factor *= i--;
        return factor;
    }
}

int main()
{
    int i;
    while(true)
    {
        cout << "Please input a number: ";
        cin >> i;
        try
        {
            if (i<0)
                throw runtime_error("it should be greater than zero!");
            cout << factorial(i) << endl;
        }
        catch (runtime_error e)
        {
            cout << e.what() << endl;
        }
        cout << "Again? Enter y or n : " ;
        char c;
        cin >> c;
        if(!cin||c=='n')  // 如果没有输入任何字符(直接enter)或者输入的是n
            break;
    }
}

运行结果:
这里写图片描述


练习6.6:说明形参、局部变量以及局部静态变量的区别。编写一个程序,同时用到这三种形式。

代码实现:

#include <iostream>

using namespace std;

void fun1(int i)  // i是形参
{
    i++;
    cout << i << " ";
    static int num=0;  // num是局部静态变量  程序结束结束才会被销毁!
    num++;
    cout << num << endl;
}

int main()
{
    int j=10; // j是局部变量
    for (int x=0;x<5;++x)
    {
        fun1(j);
    }
    return 0;
}

运行结果:

这里写图片描述


练习6.7:编写一个函数,当它第一次被调用时返回0,以后每次调用返回值加1。

代码实现:

#include <iostream>

using namespace std;

// 可使用静态局部变量实现
int countcall()
{
    static int count = 0;
    return count++;
}

int main()
{
    // 调用10次
    for (int i=0;i<10;i++)
    {
        cout <<  i+1 <<  " call: "<< countcall() << endl;
    }
}

运行结果:
这里写图片描述


练习6.10:编写一个函数,使用指针形参交换两个整数的值。

代码实现:

#include <iostream>
using namespace std;

// 指针传值的经典题目!
void exchange(int *p,int *q)
{
    int t;
    t = *p;
    *p = *q;
    *q = t;
};

int main()
{
    int a=1,b=2;
    int *p=&a, *q = &b;
    cout << "before: " << a << " " << b <<endl;
    exchange(p,q);
    cout << "after: " << a << " " << b << endl;
    return 0;
}

运行结果:
这里写图片描述


练习6.12:使用引用而非指针交换两个整数的值。

该题可以与上一题进行比较。
代码实现:

#include <iostream>

using namespace std;

void change(int &a, int &b)
{
    int t=0;;
    t = a;
    a = b;
    b = t;
}

int main()
{
    int a = 1,b=2;
    cout << "before: " << a << " "<< b <<endl;
    change(a,b);
    cout << "after: " << a << " " <<b <<endl;
    return 0;
}

运行结果:
这里写图片描述


练习6.42:给make_plural函数(参见201页)的第二个形参赋予默认参数's',利用新版本的函数输出单词sucess和failure的单数和复数形式。

分析:看了一下make_pural函数,感觉应该是给第个参数赋予默认值。
代码实现:

#include <iostream>
#include <string>
using namespace std;

string make_plural(size_t ctr, const string &word,
    const string &ending = "s")
{
    return (ctr > 1) ? word + ending : word;
}

int main(int argc, char *argv[])
{
    cout << "The single form is : " << make_plural(1,"success") << " " << make_plural(1,"failure") <<endl;
    cout << "The plural form is : " << make_plural(2,"success","es") << " "<< make_plural(2,"failure") <<endl;
}

运行结果:

这里写图片描述


练习6.47:使用递归输出vector内容的程序,使其有条件地输出与执行过程相关的信息。

分析:递归时可以考虑结合vector中自带的方法函数作为递归的条件,输出信息根据本节内容,则可考虑#ifndef NDEBUG。

程序实现:

#include <iostream>
#include <vector>

using namespace std;

// 使用递归输出vector对象的元素
// 打开和关闭编译器

void printvec(vector<int> vec)
{
#ifndef NDEBUG
    cout << "function name: " << __FUNCTION__ <<endl;
    cout << "vector size is : " << vec.size() <<endl;
#endif
    if(!vec.empty())
    {
        int tmp = vec.back();  // 取出vec最后的元素
        vec.pop_back();  // 此时vec去除最后一个元素
        printvec(vec);
        cout << tmp << " ";
    }
}

int main(int argv, char *argc[])
{
    int a[] = {1,2,3,4,5};
    vector<int> vec(a,a+5);
    printvec(vec);
    return 0;
}

运行截图:
这里写图片描述


练习6.51:编写函数f的4个版本,令其各输出一条可以区分的消息。

分析:主要在于函数的形参个数和类型的区别

程序实现:

#include <iostream>

using namespace std;

void f(double,int)
{
    cout << "call f1" <<endl;
}
void f(int)
{
    cout << "call f2" << endl;
}

void f(int, int)
{
    cout << "call f3" << endl;
}

void f(double, double)
{
    cout << "call f4" <<endl;
}

int main(int argv, char *argc)
{
    f(2.56,42);
    f(42);
    f(42,0);
    f(2.56,3.14);
}

这里写图片描述

未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值