zoukankan      html  css  js  c++  java
  • C++Primer Plus习题记录-Chapter8

    8-2

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    
    using namespace std;
    struct CandyBar
    {
        string brand;
        double weight;
        int heat;
    };
    void info_candy(CandyBar &changed_para, char* str, double m, int n);
    void show(CandyBar &candy);//test
    
    int main() {
        CandyBar init_para = {
            "Millennium Munch",
            2.85,
            350
        };
        show(init_para);
        char ch[20];
        cin.getline(ch,20);
        double m;
        cin >> m;
        int n;
        cin >> n;
        info_candy(init_para, ch, m, n);
        show(init_para);
        return 0;
    }
    void info_candy(CandyBar &changed_para, char* str, double m, int n) {
        changed_para.brand = str;
        changed_para.weight = m;
        changed_para.heat = n;
    }
    void show(CandyBar &candy) {
        cout << candy.brand << "
    " << candy.weight << "
    " << candy.heat << endl;
    }

     8-3

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <cctype>
    #include <array>
    #include <iomanip>
    #include <algorithm>
    using namespace std;
    void upper(string& lower);
    int main() {
        string lower;
        getline(cin, lower);
        while (lower!="q")
        {
            upper(lower);
            cout << lower << endl;
            getline(cin, lower);
        }
        return 0;
    }
    void upper(string & lower) {
        char *pt= &lower[0];
        int length = lower.size();
        pt[0]=toupper(pt[0]);
        for (int i = 1; i < length; i++) {
            pt[i]=toupper(pt[i]);
        }
    }

     8-4

    using namespace std;
    struct stringy
    {
        char* str;
        int ct;
    };
    void set(stringy &beany, char []);
    void show(stringy str,int n=1);
    void show(const char* str, int n=1);
    int main() {
        stringy beany;
        char testing[] = "Reality isn't what it used to be.";
        set(beany, testing);
        show(beany);
        testing[0] = 'D';
        testing[1] = 'u';
        show(testing);
        show(testing, 3);
        show("Done!");
    
        return 0;
    }
    void set(stringy &beany, char ch[]) {
            beany.str = ch;
    }
    void show(stringy str,int n) {
        for (int i = 0; i < n; i++)
            cout << str.str << endl;
    }
    void show(const char* str, int n) {
        for (int i = 0; i < n; i++)
            cout << str << endl;
    }

     8-5

    template<class T>
    T maxn(T a[]);
    
    int main() {
        
        int a[5] = { 1,2,3,4,5 };
        double aa[5] = { 2.1,3.1,4.1,1.1,1.0 };
        int max1 = maxn(a);
        double max2 = maxn(aa);
        cout << max1 << ' ' << max2;
        return 0;
    }
    template<class T>
    T maxn(T a[]) {
        T temp=a[0];
        for (int i = 1; i < 5; i++) {
            if (a[i] > temp)
                temp = a[i];
        }
        return temp;
    }

     8-6

    using namespace std;
    template<typename T>
    T maxn(T ar[], int n);
    template <> const char* maxn(const char* ch[], int n);
    int main(void) {
        const char *ch[3] = { "aa","b","ccc" };
        int a[6] = { 1,2,3,4,2,4 };
        double b[4] = { 1.1,2.2,0.3,2.4 };
        int maxa = maxn(a,6);
        double maxb = maxn(b, 4);
        const char* pr;
        pr = maxn(ch, 3);
        cout << maxa << " " << maxb << " " << &pr << " " << static_cast<const void*>(pr);
        return 0;
    }
    template<typename T>
    T maxn(T ar[], int n) {
        T temp;
        temp = ar[0];
        for (int i = 1; i < n; i++) {
            if (ar[i] > temp)
                temp = ar[i];
        }
        return temp;
    }
    template <> const char* maxn(const char* ch[], int n) {
        const char *temp = ch[0];
        for (int i = 1; i < n; i++) {
            if (strlen(temp) < strlen(ch[i]))
                temp = ch[i];
        }
        return temp;
    }

    8-7

    using namespace std;
    
    template<typename T>
    T sumArray(T ar[], int n);
    template<typename T>
    T sumArray(T* ar[], int n);
    struct debts
    {
        char name[50];
        double amount;
    };
    int main(void) {
        int things[6] = { 13,31,103,301,310,130 };
        debts mr[3]= {
            {"Light",13.4},
            {"Lighr",13.1},
            {"Lighy",13.2}
        };
        double* pd[3];
        for (int i = 0; i < 3; i++) {
            pd[i] = &mr[i].amount;
        }
        int sum1;
        sum1=sumArray(things, 6);
        double sum2;
        sum2 = sumArray(pd, 3);
        cout << sum1 << " " << sum2;
        return 0;
    }
    
    template<typename T>
    T sumArray(T ar[], int n) {
        T sum = 0;
        for (int i = 0; i < n; i++)
            sum += ar[i];
        return sum;
    }
    template<typename T>
    T sumArray(T* ar[], int n) {
        T sum = 0;
        for (int i = 0; i < n; i++)
            sum += *ar[i];
        return sum;
    }
  • 相关阅读:
    UIWebView的高度不对问题
    SQL --分组【Group By】
    SQL--Order By
    SQL--空值处理
    SQ--模糊查询
    SQL 聚合函数
    SQL 语句的TOP,Distinct语句
    SQL约束
    1 翻译系列:什么是Code First(EF 6 Code First 系列)
    终极版:由简单工厂模式,升级到抽象工厂模式(用到反射)
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10320227.html
Copyright © 2011-2022 走看看