《C++Primer》第五版习题答案--第六章【学习笔记】

《C++Primer》第五版习题答案--第六章【学习笔记】

ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考。
作者:cosefy
Date: 2020/1/16

第六章:语句

练习6.2:

  • 返回类型错误
  • 无返回类型
  • 形参名字应该不同
  • 函数体需要用花括号包含起来

练习6.4:
实现:编写函数,使得用户输入一个整数,main函数调用函数得到阶乘。

#include<iostream>
using namespace std;

int fact(int n) 
{
    int result = 1;
    while (n > 1)
        result *= n--;
    return result;
}
int main() 
{
    int a;
    cout << "请输入一个整数,求阶乘: " << endl;
    cin >> a;
    cout << "结果:"<<fact(a) << endl;
    return 0;
}

练习6.5:

#include<iostream>
using namespace std;
int fuc(int num)
{
    return num > 0 ? num : -num;
}
int main()
{
    cout << "请输入一个整数: " << endl;
    int a;
    cin >> a;
    cout << "绝对值: " << fuc(a) << endl;
    return 0;
}

练习6.6:

  • 形参和函数体内部的变量统称为局部变量,每当函数执行完就会释放;而局部静态变量存储在静态存储区,生命周期贯穿函数调用及之后的时间。

练习6.7:

#include<iostream>
using namespace std;

int test1() 
{
    static int a = 0;
    return a++;
}
int main()
{
    for (int i = 1; i < 5; i++)
        cout << test1() << endl;
    return 0;
}

练习6.10:

#include<iostream>
using namespace std;

void traverse(int* p, int* q)
{
    int tmp;
    tmp = *p;
    *p = *q;
    *q = tmp;
}

int main()
{
    int a = 20, b = 10;
    cout << "Before: (a,b)=" << a <<" "<< b << endl;
    traverse(&a, &b);
    cout << "After: (a,b)=" << a <<" "<< b << endl;
    return 0;
}

练习6.13:

前者是值传递,后者是地址传递,前者无法改变实参,后者可以改变实参。

练习6.15:

  • s是字符串,我们希望它不被修改,保持稳定,所以定义为常量,而occurs在程序中值需要修改
  • c是一个临时变量,没有必要进行地址传递,否则没有意义
  • 那么,occurs的值将一直保持0,而s有可能会被修改,并且s不再能接受常量字符串的实例化。

练习6.17:

#include<iostream>
using namespace std;

bool find_upperalpha(const string& s)
{
    for (auto c : s)
        if (isupper(c))
            return true;
    return false;
}
void trf_tolower(string& s)
{
    for (auto &c : s)
        if (isupper(c))
            c = tolower(c);
}
int main()
{
    cout << "请输出一个字符串: " << endl;
    string s;
    cin >> s;
    cout << "是否有大写字母:" << find_upperalpha(s) << endl;
    trf_tolower(s);
    cout << "把大写字母转化为小写: " << s << endl;
    return 0;
}

练习6.18:

  • bool compare(matrix &m1,matrix &m2);
  • vector<int>:: iterator change_val(int ,vector<int>::iterator);

练习6.22:

 #include<iostream>
using namespace std;

void fuc(int* &p1, int* &p2)
{
    int* q = NULL;
    q = p1;
    p1 = p2;
    p2 = q;
}
int main()
{
    int a1 = 100, a2 = 200;
    int* p1 = &a1;
    int* p2 = &a2;
    cout << "Before: " << *p1 << " " << *p2 << endl;
    fuc(p1, p2);
    cout<< "After: " << *p1 << " " << *p2 << endl;
    return 0;
}

练习6.24:

不可以用值来传递数组。

void print(const int (&a)[10]){/**/}

练习6.25:

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

int main(int argc, char** argv)
{
    string str;
    for (int i = 1; i != argc; i++)
    {
        str += argv[i];
        str += ' ';
    }
    cout << str << endl;
    return 0;
}

练习6.27:

#include<iostream>
using namespace std;

int get_sum(initializer_list<int> i1)
{
    int sum = 0;
    for (auto elem : i1)
        sum += elem;
    return sum;
}
int main()
{
    initializer_list<int> i2{ 1,2,3,4,5,6 };
    cout << get_sum(i2) << endl;
    return 0;
}

练习6.32:
合法,把0-9拷贝到大小为10的数组中。

练习6.34:
如果输入为负数,则将一直递归下去。

练习6.35:
val--传入的值是自减之前的值。

练习6.39:

错误,错误,正确。

猜你喜欢

转载自www.cnblogs.com/cosefy/p/12203197.html