C++primer第六章习题答案

这是一个包含C++ Primer第六章多个习题的解答,包括递归计算阶乘、输入整数获取阶乘、绝对值函数、交换整数、打印字符串、检查字符串是否包含大写字母、将字符串转为小写、比较大小、迭代器的递归函数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<vector>
#include<string>
#include<stdexcept>
#include"6.h"

using namespace::std;
//6.3
//int fact(int val) {
//    if (val == 0 || val == 1) return 1;
//    else {
//        /*int a = 1;
//        for (int i = 1; i <= val; i++) {
//            a *= i;
//        }
//        return a;*/
//        return val * fact(val-1);
//    }
//}

//6.4
//int fact() {
//    int val;
//    cout << "please enter one integer and get the factorial"<<endl;
//    cin >> val;
//    if (val == 0 || val == 1) return 1;//别忘了0的阶乘的问题
//    else {
//        int result = 1;
//        while (val > 1) {
//            result *= val--;
//        }
//        return result;
//    }
//}
//6.5
/*template<typename t>t abso(t val) {
    return val>0?val:-val;
}*///用模板类更好处理复杂情况
//6.10
 /*void swap(int *p1, int *p2) {
    int swap;
    swap = *p1;
    *p1 = *p2;
    *p2 = swap;
 }*/
//6.11
 /*void reset(int &i) {
    i = 0;
  }*/
//6.12
//void swap(int &p1, int &p2) {
//    int swap;
//    swap = p1;
//    p1 = p2;
//    p2 = swap;
//}

//6.17
//bool haveupper(const string &s) {
//    bool a = true;
//    for (auto c : s) {
//        if (isupper(c)) return a;
//    }
//    return !a;
//}
//void tolower1(string &s) {
//    for (auto &c : s) {
//        c=tolower(c);
//    }
//}

//6.21
//int bigger(int a, const int* const b) {
//    return(a > (*b) ? a : (*b));
//}

//6.22
//void swapp(const int* a, const int* b) {
//    auto p = a;
//    a = b;
//    b = p;
//}

//6.23
//void print(const char *cp) {
//    if (cp)
//        while (*cp)  cout << *cp++<<endl;
//}
//void print(const int *beg, const int *end) {
//    while (beg != end) cout << *beg++ << endl;
//}
//void print(const int ia[], size_t size) {
//    for (auto i = 0; i != size; i++) {
//        cout << ia[i] << endl;
//    }
//}
//void print(int(&arr)[2]) {
//    for (auto c : arr) cout << c<<endl;
//}

//6.27
//int add1(initializer_list<int>lst) {
//    int i = 0;
//    for (auto bg = lst.begin(); bg != lst.end(); bg++) i += *bg;
//    return i;
//}
//int add2(initializer_list<int>lst) {
//    int i = 0;
//    for (auto ele : lst) i += ele;
//    return i;
//}

//6.33
//void recursive(vector<int>::iterator beg,vector<int>::iterator end){
//    if (beg != end) {
//        cout << *beg;
//        recursive(++beg, end);
//    }
//}

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

//6.44
//inline bool isshorter(const string &lft, const string &rht)
//{
//    return lft.size() < rht.size();
//}

//6.47
//void recursive(vector<int>::iterator beg, vector<int>::iterator end) {
//    #ifndef NDEBUG
//    cout << "size" << end - beg << endl;
//    #endif
//    if (beg != end) {
//    cout << *beg;
//    recursive(++beg, end);
//        }
//    }
//void print(vector<int>::iterator first, vector<int>::iterator last)
//{
//#ifndef NDEBUG
//    cout << "vector size: " << last - first << endl;
//#endif
//    if (first == last)
//    {
//        cout << "over!" << endl;
//        return;
//    }
//    cout << *first << " ";
//    print(++first, last);
//
//}
//6.55
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : -1; }
int main() {
    
 /*cout<<fact(5);*/
//cout << abso(-1.11111111111);
//6.10
    /*for (int lht, rht; cout << "Please Enter:\n", cin >> lht >> rht;) {
        swap(&lht, &rht);
        cout << lht << " " << rht << endl;
    }*/
//6.11
    /*int i = 10;
    reset(i);
    cout << i;*/
//6.12    
    /*for (int lht, rht; cout << "Please Enter:\n", cin >> lht >> rht;) {
        swap(lht, rht);
        cout << lht << " " << rht << endl;
    }*/
//6.17
    /*string s;
    cin >> s;
    cout<<haveupper(s);
    tolower1(s);
    cout << s;*/

//6.21
    /*int a = 1,b=2;
    cout<<bigger(a, &b);*/

//6.22
    /*const int i = 42, j = 99;
    auto lhs = &i;
    auto rhs = &j;
    swap(lhs, rhs);
    std::cout << *lhs << " " << *rhs << std::endl;*/
    /*char ch[3] = { 'a','b' };
    print(ch);
    int j[2] = { 0,1 };
    print(begin(j),end(j));
    print(j, end(j) - begin(j));
    print(j);*/

//6.27
    /*cout << add2({ 1, 2, 3, 4, 5 });*/
//6.33
    /*vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    recursive(vec.begin(), vec.end());*/
//6.42
    /*cout<<make_plural(2, "es", "success");*/
//6.47
    /*vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    print(vec.begin(), vec.end());*/
//6.55
    int func(int, int);
    vector <decltype(func)*>v;
    v.push_back(add);
    v.push_back(subtract);
    v.push_back(multiply);
    v.push_back(divide);
    for (auto i : v) {
        cout<<typeid(i).name();
        cout<<i(6, 8)<<endl;
    }
    
    system("pause");
    return 0;
}

//6.25
//void main(int argc, char** argv) {
//    string str;
//    for (int i = 1; i != argc; i++) {
//        str = str + argv[i]+'a';
//    }
//    cout << str;
//}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值