C++primer函数部分课后题

来源:互联网 发布:外国人聊天用什么软件 编辑:程序博客网 时间:2024/05/29 04:05

6.4 阶乘函数

#include <iostream>using namespace std;long fact(long val){    if (val < 1)        return 1;    else        return val* fact(val-1);   // 阶乘函数}int main(){    int i;    cout << "enter one number 0<i<13"<< endl;  // 保证int范围    cin>>i;    cout << "阶乘为\t" << fact(i) << endl;    return 0;}

6.5 输出实参绝对值

#include <iostream>#include <string>using namespace std;double fabs(double val){return val >= 0 ? val : -val; }int main(){double i;cout << "enter one number" << endl;while (cin >> i){    cout << "|" << i << "| = "<< fabs(i) << endl;    string s;    cout << "again more ? yes or no" << endl;    cin >> s;    if (s  != "yes")        break;    cout << "enter one number" << endl;}return 0;}

6.6/6.7说明形参,局部变量,静态局部变量的区别
局部变量:块内部定义的变量;
参数:在函数参数列表中声明的局部变量
局部静态变量:local static variable(object)在第一次执行通过对象的定义之前被初始化。当功能结束时,局部静态变量不会被破坏; 当程序终止时,它们被销毁。

#include <iostream>using namespace std;int fun(int i){    static int a = 0;  // 静态局部变量    a++;    int t = 0;    // 局部变量    return a+t+i;}int main(){    int i = 6;    for(int j = 0; j < 3; j++)    {        cout << fun(i) << endl;   //7, 8, 9     }    return 0;}

6.9 g++ 对多个文件编译

lk@lk-PC:~/Desktop/c++$ cat main.cpp #include <iostream>#include "head.h"#include "fun.cpp"using namespace std;int main(){    int num ;    cin >> num;    cout << num << "的绝对值是:" << endl;    cout<<fun(num) << endl;    return 0;}lk@lk-PC:~/Desktop/c++$ cat head.h int fun(int val);lk@lk-PC:~/Desktop/c++$ cat fun.cpp int fun(int val){    return val >= 0 ? val : -val;}lk@lk-PC:~/Desktop/c++$ 

知识点1:通过使用引用形参,可以改变实参的值。
知识点2:避免使用引用的过程中拷贝,因为如果对象特别巨大,拷贝的过程会非常缓慢,造成程序低效。
知识点3:当函数需要多个返回值时,可以使用引用形参返回多个值。
对于让函数返回多个值的具体操作:1、定义一个新的数据类型,包含多个参数。2、给函数传入额外的引用实参,令其保存额外的参数,隐式的返回。
//C++最好使用引用类型的形参代替指针(可以通过指针来修改它所指对象的值)
// 如果函数无须改变引用形参的值,最好将其声明变为常量引用
6.10使用指针交换两个数的值(const int &i)…

  #include <iostream>using namespace std;void swap(int *i, int *j)  // 使用指针方法{    int t;    t = *i;    *i = *j;    *j = t;}void swap1(int &a, int &b)  // 使用引用方法{    int t;            // a,b是i,j的别名    t = a;    a = b;    b = t;}int main(){    int i , j;    cout << "enter two number:"<< endl;    cin >> i >> j;    cout << "交换前i = " << i << "\tj= " << j << endl;    swap1(i, j);   // swap(&i,&j);    cout << "交换后i = " << i << "\tj= " << j << endl;    return 0;}

6.11 :找出一段字符串中某个字符出现的次数 和第一次出现的位置,用引用的方法

#include <iostream>#include <vector>#include <string>using namespace std;int find(const string &ch, char c, int &val){    val = -1;    int count = 0;  // 统计出现次数    for (int i = 0; i != ch.size(); i++)    {        if (ch[i] == c)        {            if (val == -1)                val = i+1;   // 第一次出现的位置            count++;        }       }    return count;}int main(){    string ch;    cout << "输入一串字符串" << endl;    cin >> ch;    char c;    cout << "输入你要查找字符" << endl;    cin >> c;    int val = 0;  // 第一次出现的位置    if ( find(ch, c, val) )    {        cout << c << "字符共出现的次数为" << find(ch, c, val) << endl;        cout << c << "字符第一次出现的位置是在第"<< val << "位" << endl;    }    else        cout << c << "字符未出现"<<endl;    return 0;}//const 注意
 #include <iostream>using namespace std;void fun(const int j) // j 只能读 ,不能写{//  j++;     // 错误}int main(){    int i = 1;       fun(i);    cout << i;    const int a = 2;    a = i;  // 错误   与传参相比这就是忽略顶层const,而这里是没忽略所以错误    return 0;}/*   所谓用实参初始化形参时,会忽略掉顶层const 换句话说 顶层const被忽略掉了,当形参有顶层const时,传给它常量对象或者非常量对象都对;注意是实参给形参传 */

6.17 编写一个函数,判断string中是否有大写字母,在编写一个函数把大写改为小写,两个函数的形参类型一样吗?

  #include <iostream>#include <string>using namespace std;bool judge(const string &str) // 一般循环判断是否有大写{    for (int i=0; i != str.size(); i++)    {        if (str[i] >= 'A' && str[i] <= 'Z')            return true;    }}void change(string &str)  // 大写改为小写{    for (auto &c :str)  // 范围for语句    {        if (isupper(c))  // 判断大写            c = tolower(c); // 改为小写    }}int main(){    cout << "enter string"<< endl;    string str;    cin >> str;    if ( judge(str) )    {        cout << "有大写字母"<< endl;        change(str);        cout << "改变后的字符串是:" << str << endl;    }    else        cout << "无大写字母"<< endl;    return 0;}

begin(a)函数 和a.begin()用法的注意

#include <iostream>#include <iterator>#include <vector>using namespace std;//void fun(const int * beg, const int *end)  // 指针所指对象不可以改void fun( vector<int>::iterator beg, vector<int> :: iterator end){    while (beg != end)    {        cout << *beg++ << " ";    }}int main(){    int a[5] = {1,2,3,4,5};    vector<int>b(a, a+5);//  fun(begin(a), end(a));    fun(b.begin(), b.end());/*  auto beg = b.begin();    auto en = b.end();    while (beg != en)    {        cout << *beg++;    }*/    return 0;}/*   vector<int>b(a, a+5);   b.begin() 返回类型是 vector<int>::iterator   如果是 int a[5];   就用begin(a) 返回类型是 int *  */// 如果改变要改变字符串就用string &str   //不改变的话最好用 const string &str对数组的引用:fun(int (&arr)[10]) 维度是类型的一部分注意:&arr[10] 错误  将arr声明成了引用的数组应该是(&arr)[10] 正确 arr是具有10个整数的整型数组的引用

6.21 比较int 的值 和指针所指的值

#include <iostream>using namespace std;int campare(const int i, const int *j){    return i > *j ? i : *j ;}int main(){    int i = 1, j = 10;    cout <<campare(i, &j) << endl;    return 0;}

6.22 这个就很简单吧不写了
6.23 数组调用方法

#include <iostream>using namespace std;void fun(const int *a, const int size)   // const int a[]{    for (int i = 0; i != size; i++)    {        cout << *a+i << " ";    }    cout << endl;}void fun(const int (&a)[3])   // 引用{    for (auto i : a)    {        cout << i << " ";    }    cout << endl;}void fun(const int *a)  // 指针{    while (*a)    {        cout << *a++ << " ";    }    cout << endl;}void fun(const int *beg, const int * end)  // 地址{    while (beg!=end)    {        cout << *beg++ << " ";    }    cout << "\n";}int main(){    int i = 0, a[3] = {1, 2,3};    fun(a, 3);    fun(a);    fun(a);    fun(begin(a), end(a));    return 0;}

6.24/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;}