IMXYLZ

C++ Primer Plus 第8章习题答案

C++ Primer Plus 编程习题答案


8.1

//ex8.1 ~ c++ primer plus
#include <iostream>

static int times;

void show(const char * s,const int c=0);

int main(){
    show("Hello world!");
    show("One time");
    show("Two times",100);
    show("One Times");
    show("Four times",1);

    return 0;
}

void show(const char *s,const int c){
    if(c == 0){
        std::cout << s << std::endl;
    }else{
        for(int i=0;i<times;i++){
            std::cout << s << std::endl;
        }
    }
    times++;
}

8.3

//ex8.2 ~ c++ primer plus
#include <iostream>
#include <cstring>

struct CandyBar{
    char * name;
    double weight;
    int kcal;
};

void set(CandyBar &cb,const char *name = "Millennium Munch",const double weight=2.85,const int kcal=350);
void show(const CandyBar &cb);

int main(){
    CandyBar cb,cb2;

    set(cb,"Milk", 123.2, 1700);
    set(cb2);

    show(cb);
    show(cb2);

    delete cb.name;
}

void set(CandyBar &cb,const char *name,const double weight,const int kcal){
    char * cs = new char[strlen(name)+1];
    strcpy(cs, name);
    cb.name = cs;
    cb.weight = weight;
    cb.kcal = kcal;
}
void show(const CandyBar &cb){
    using namespace std;
    std::cout << "name: "<< cb.name << ", weight: "<<cb.weight<<", kcal: "<<cb.kcal<<std::endl;
}

8.3

//ex8.3 ~ c++ primer plus
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>

std::string toUpper(std::string s);

int main(){

    using namespace std;
    char s[100];
    while(true){
        std::cout << "Enter a string (q to quit): ";
        cin.getline(s,100);
        if(strcmp(s,"q") == 0){
            std::cout << "Bye" << std::endl;
            break;
        }
        std::cout << toUpper(s) << std::endl;
    }
}

std::string toUpper(std::string s){
    for(int i=0;i<s.length();i++){
        s[i] = std::toupper(s[i]);
    }
    return s;
}

8.4

在线运行 http://ideone.com/ZGSAFE

#include <iostream>
#include <cstring>

struct stringy {
    char * str;
    int ct;
};

void set(stringy &st, char *s);
void show(const stringy &st, int count=1);
void show(const char s[], int count=1);

using namespace std;
int main(){
    stringy beany;

    char testing[] = "Reality isn't what it used to be.";

    show(testing);

    set(beany, testing);
    cout<<"beany.str "<<beany.str<<endl;

    show(beany);

    show(beany,2);

    testing[0] = 'D';
    testing[1] = 'u';

    show(testing);
    show(testing, 3);
    show("Done!");

    return 0;
}

void set(stringy &st, char *s){
    st.ct = strlen(s);
    st.str = new char[st.ct+1];
    strcpy(st.str,s);
}

void show(const stringy &st, int count){
    while(count-- > 0){
        cout << st.ct << " -> " << st.str << endl;
    }
}

void show(const char s[], int count){
    while(count-- > 0){
        cout << s << endl;
    }
}

8.5

//ex8.5 ~ c++ primer plus
#include <iostream>

template <typename T> T max5(T ts[]){
    T t = ts[0];
    for(int i=1;i<5;i++){
        if( t < ts[i]){
            t = ts[i];
        }
    }
    return t;
}

int main(){
    int is[5] = {20, 15, 17, 33, 8};

    using namespace std;
    cout << "max(is) = "<<max5<int>(is)<<endl;

    double ds[5] = {20.5, 22.37, 8.33, 80.12, -12};
    cout << "max(ds) = "<<max5<double>(ds)<<endl;
    return 0;
}

8.6

//ex8.6 ~ primer c++ plus
#include <iostream>
#include <cstring>

template <typename T> T maxn(T ts[],int len){
    T max = ts[0];
    for(int i=1;i<len;i++){
        if(max<ts[i]){
            max = ts[i];
        }
    }
    return max;
}

template <>
const char * maxn<const char *>(const char *cs[],int len){
    int index = 0;
    for(int i=1;i<len;i++){
        if(strlen(cs[index]) < strlen(cs[i])){
            index = i;
        }
    }
    return cs[index];
}

int main(){

    int is[] = {45,12,100,20,7,8};
    double ds[] = {10.3, 20.88, 8.11, 3.55};

    using namespace std;
    cout << "max(is) = " << maxn(is,6) << endl;
    cout << "max(ds) = " << maxn(ds,4) << endl;

    const char *cs[5] = {"have a consistent style",
                        "be easy to read and understand",
                        "be portable to other architectures",
                        "be maintainable by different programmers",
                        "be free of common types of errorsok"};

    cout << "max(cs) = " << maxn<const char *>(cs,4) << endl;
}

8.7

//ex8.7 ~ c++ primer plus
#include <iostream>

template <typename T> T ShowArray(T arr[], int n);

struct debts{
    char name[50];
    double amount;
};

template <typename T> T ShowArray(T *arr[], int n);

using namespace std;
int main(){
    int things[6] = {13, 31, 103, 301, 310, 130};
    struct debts mr_E[3] = {
        {"Ima Wolfe", 2400.0},
        {"Ura Foxe", 1300.0},
        {"Iby Stout", 1800.0}
    };
    double * pd[3];

    for(int i=0;i<3;i++){
        pd[i] = &mr_E[i].amount;
    }

    cout << "ShowArray(things) = " << ShowArray(things, 6) << endl;;
    cout << "ShowArray(pd) = " << ShowArray(pd, 3) << endl;

    return 0;
}

template <typename T> T ShowArray(T arr[], int n){
    T v = 0;
    for(int i=0;i<n;i++){
        v += arr[i];
    }
    return v;
}

template <typename T> T ShowArray(T *arr[], int n){
    T v = 0;
    for(int i=0;i<n;i++){
        v += *arr[i];
    }
    return v;
}

Comments

回顶部