第6章 练习题

6.1、

实参是形参的初始值

6.2、

a、返回类型不匹配;

string f(){

  string s;

  return s;

}

b、没有声明函数类型

c、差个花括号{

d、没有花括号,函数体要用花括号括起来

 

6.3、

//p6_3.cpp -- 6.3、6.4
#include <iostream>
using namespace std;

int fact(int val);

int main()
{
    int temp;
    cout << "Please enter an interger: " << endl;
    cin >> temp;
    cout << temp << "! is " << fact(temp) << endl;

    return 0;
}

int fact(int val)
{
    if(val>1)
        return fact(val-1)*val;
    else
    {
        return 1;
    }
    
}

6.5

//p6_5.cpp 
#include <iostream>
using namespace std;

int absInput(int val);

int main()
{
    int temp;
    cin >> temp;
    cout << temp << "'s abslute is " << absInput(temp) << endl;

    return 0;
        
}

int absInput(int val)
{
    if(val < 0)
        return -val;
    else
    {
        return val;
    }
}

 

6.6、

//p6_6.cpp
#include <iostream>
using namespace std;

int func(int ival);

int main()
{
    int temp;
    cin >> temp;
    func(temp);

    return 0;
}

int func(int ival)
{
    
    for (int i = 0; i <= ival; i++)
    {
        static int i_record = 0;
        cout << "i_record = " << i_record << endl;
        ++i_record;
    }
    return 0;
}

6.7、

//p6_7.cpp
#include <iostream>
using namespace std;

int func(void);

int main()
{
    for (int i = 0 ; i < 10 ; ++i)
    {
        cout << "func's return value is : " << func() << endl;
    } 

    return 0;
}

int func()
{
    static int MyTimes = 0;

    return MyTimes++; 
}

6.8、 6.9

 

6.10

//p6_10.cpp
#include <iostream>
using namespace std;

void exchange_int(int *, int *);

int main()
{
    int ival1, ival2;
    cout << "please enter two integer :"<< endl;
    cin >> ival1 >> ival2;

    cout << "ival1 = " << ival1 << endl;
    cout << "ival2 = " << ival2 << endl;
    exchange_int(&ival1, &ival2);

    cout << "ival1 = " << ival1 << endl;
    cout << "ival2 = " << ival2 << endl;

    return 0;

}

void exchange_int(int * ival1, int *ival2)
{
    int temp;
    temp = *ival1;
    *ival1 = *ival2;
    *ival2 = temp;

    return ;
}

6.11

//p6_11.cpp 
#include <iostream>
using namespace std;

void reset(string &str);

int main()
{
    string str;
    getline(cin,str);

    cout << "str : " << str << endl;
    reset(str);
    cout << "reset(str) : " << str << endl;

    return 0;
}

void reset(string &str)
{
    str = "reseted";
}

6.12

//p6_12.cpp
//p6_10.cpp
#include <iostream>
using namespace std;

void exchange_int(int &, int &);

int main()
{
    int ival1, ival2;
    cout << "please enter two integer :"<< endl;
    cin >> ival1 >> ival2;

    cout << "ival1 = " << ival1 << endl;
    cout << "ival2 = " << ival2 << endl;
    exchange_int(ival1, ival2);

    cout << "ival1 = " << ival1 << endl;
    cout << "ival2 = " << ival2 << endl;

    return 0;

}

void exchange_int(int & ival1, int &ival2)
{
    int temp;
    temp = ival1;
    ival1 = ival2;
    ival2 = temp;

    return ;
}

6.13

void f(T) 传递实参的值,函数不会改变实参

void f(&T) 传递引用,函数可以改变实参

 

6.14

6.15、

字符串的常量引用,字符类型,字符串长度类型的引用

因为在函数中不用改变字符串而统计字母出现的次数occurs是需要变化的

s是引用类型可以减少拷贝的工作,occurs是需要统计信息的量,而c只需要他的值就可以了

6.16、

函数不需要改变参数而没有设声明为常量引用,对常量字符串无使用

bool is_empty(conat string & s) {return s.empty());}

6.17

//p6_17.cpp -- 
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool have_upper(const string& str);
void strToupper(string& str);

int main()
{
    string temp;
    getline(cin, temp);
    if (have_upper(temp))
        cout << temp << " have upper alpha " << endl;
    else
        cout << temp << " have no upper alpha" << endl;
    strToupper(temp);
    cout << temp << endl;

    return 0;
}

bool have_upper(const string& str)
{
    bool upApear = false;

    for (auto& c : str)
    {
        if (isupper(c))
        {
            upApear = true;
            break;
        }
    }
    return upApear;
}
void strToupper(string& str)
{
    for (auto& c : str)
    {
        c = toupper(c);
    }
}

不同,一个只需要判断其中大写字母的存在性,不需要修改参数

一个需要修改原参数

6.18

//a
bool compare(matrix & , matrix & )    //判断两个矩阵是否匹配

//b
vector<int>::iterator change_val(int,vector<int>::iterator);
//改变容器中的值

 

6.19

a ) 不合法

b ) 合法

c ) 合法

d ) 合法

 

6.20

不需要改变实参的时候用常量引用,若设为普通引用会无法处理同类型的常量、无意中改变实参的值。

6.21

//p6_21.cpp
#include <iostream>
using namespace std;

int larger(int ival, int * pi);

int main()
{
    int ival1, ival2;
    cin >> ival1 >> ival2;

    cout << larger(ival1, &ival2);

    return 0;
    
}

int larger(int ival, int * pi)
{
    if (ival > *pi)
        return ival;
    else
    {
        return *pi;
    }
    
}

 6.31、

返回局部对象的引用无效

 

6.32

合法:返回数组的每一个元素

6.33、

//p6_33.cpp -- 递归输出vector对象的内容
#include <iostream>
#include <vector>
using namespace std;

void getInt(vector<int> ivec, vector<int>::size_type ix);

int main()
{
    vector<int>  ivec;
    int temp;
    while (cin >> temp)
        ivec.push_back(temp);
    getInt(ivec, ivec.size() - 1);

    return 0;
}

void getInt(vector<int> ivec, vector<int>::size_type ix)
{
    if (ix != 0)
        getInt(ivec, ix - 1);
    cout << ivec[ix] << ",";
}

6.34

传入负数将无法停止直到耗尽栈区内存

6.35

既不能用ival-- 也不能用--ival ;表达式中有其他的ival,并且ival--是先用后减,factorial将一直计算ival,陷入死循环

6.36、 6.37、 6.38

string (& getArry(string *))[10];

typedef string strArry[10];
strArry & getArry(string *);

string arr[10];
decltype(arr) & getArry(string *);


//6.38
int odd[] = {1, 3, 5, 7, 9};
int even[] = {1, 2, 4, 6, 8};
decltype(odd) &arrPtr(int i)
{
    return (i%2) ? odd : even;
}

6.39

a  ) 非法,函数重定义了

b  ) 非法,

c  ) 合法,

6.40

a ) 合法

b ) 默认形参不能放在前面

 

6.41 

a ) 不合法  c ) 与初衷不符,传递参数是按顺序的

 

6.42

6.43、

a放头文件

b放头文件

 

6.54 、6.55、 6.56

//p6_55.cpp 
#include <iostream>
#include <vector>
using namespace std;

int func1(int a, int b);
int func2(int a, int b);
int func3(int a, int b);
int func4(int a, int b);

int main()
{
    vector<decltype(func1)*> ptrf = { func1,func2,func3,func4};
    
    cout << "ptrf[0](1,2) = " << ptrf[0](1, 2) << endl;
    cout << "ptrf[1](1,2) = " << ptrf[1](1, 2) << endl;
    cout << "ptrf[2](1,2) = " << ptrf[2](1, 2) << endl;
    cout << "ptrf[3](1,2) = " << ptrf[3](1, 2) << endl;

    return 0;
}



int func1(int a, int b)
{
    return a + b;
}
int func2(int a, int b)
{
    return a - b;
}
int func3(int a, int b)
{
    return a * b;
}
int func4(int a, int b)
{
    return a / b;
}

 

posted @ 2019-10-31 20:10  小小高~  阅读(219)  评论(0)    收藏  举报