c++ primer plus 第八章习题

c++ primer plus 第八章习题

#include<iostream>
using namespace std;
void show(const char * a, int b=0);
void show(const char * a, int b)
{
    static int uses = 0;
    ++uses;
    
    cout << "第" << uses << "次调用.\n";
    cout <<a<<endl;
}
int main()
{
    int i;
    const char * p = "hello world\n";
    cout <<"输入函数调用次数:";
    cin >> i;
    while(i>0)
    {
        show(p, i);
        i--;
    }
    cout << "默认n为0:";
    show(p);
    return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
struct candybar
{
    char name[50];
    double weight;
    int heat;
};
void fill(candybar &candy, char *a, double b, int c);
void show(const candybar);
int main()
{
    candybar candy1;
    char name[30];
    double weight;
    int heat;
    cout<<"Please input the name, weight and heat: \n";
    cin.getline(name,30);
    cin>>weight>>heat;
    fill(candy1,name,heat,weight);
    show(candy1);
}
void fill(candybar &candy, char *a, double b, int c)
{
    strcpy(candy.name, a);
    candy.weight=b;
    candy.heat=c;
}
void show(const candybar a)
{
    cout<<a.name<<endl
    <<a.weight<<endl
    <<a.heat<<endl;
}
#include <iostream>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
void upper(string str);
int main()
{
    string str;
    cout<<"Enter a string(q to quit):\n";
    getline(cin, str);
    while(str!="q")
    {
        upper(str);
        cout<<"Next string(q to quit):\n";
        getline(cin, str);
    }
    
    
}
void upper(string str)
{
    for(int i=0;i<str.size();i++)
        str[i]=toupper(str[i]);
    cout<<str<<endl;
}
#include <iostream>
using namespace std;
#include <cstring>
#include <cctype>
#include <string>
struct stringy
{
    char str[30];
    int ct;
};
void set(stringy &s,char * str);
void show(const string str, int times=1)
{
    while(times--)
        cout<<str;
    cout<<endl;
}
void show(const stringy s, int times=1)
{
    while(times--)
        cout<<s.str;
    cout<<endl;
}

int main()
{
    stringy beany;
    char testing[]="Reality isn't what it used to be.";
    set(beany, testing);
    show(beany);
    show(beany,2);
    testing[0]='D';
    testing[1]='u';
    show(testing);
    show(testing, 3);
    show("Done!\n");
}
void set(stringy &s,char * str)
{
    strcpy(s.str,str);
}
在这里插入代码片
```#include <iostream>
template <typename T>
T max(T arr[])
{
    T max=arr[0];
    for(int i=1;i<5;i++)
    {
        if(arr[i]>max)
            max=arr[i];
    }
    return max;
}
int main()
{
    using namespace std;
    int x1[5]={1,2,3,4,5};
    double x2[5]={6,7,8,9,10};
    cout<<max(x1)<<endl<<max(x2)<<endl;
}
#include <iostream>
#include <string>
template <typename T>
T maxn(T arr[], int n)
{
    T max=arr[0];
    for(int i=1;i<n;i++)
    {
        if(arr[i]>max)
            max=arr[i];
    }
    return max;
}

template <> char * maxn(char *arr[], int n)
{
    char *max=arr[0];
    for(int i=0;i<n;i++)
    {
        if(strlen(max)<strlen(arr[i]))
            max=arr[i];
    }
    return max;
}

int main()
{
    using namespace std;
    int x1[6]={34,223,65,88,490,43};
    double x2[4]={88.23,23.433,344.33,44.5};
    char * x3[5]={"c++ primer!","ASAP","Today is a good day","Be patient","Hello world"};
    
    cout<<"The biggest int number is "<<maxn(x1,6)<<endl;
    cout<<"The biggest double number is "<<maxn(x2,4)<<endl;
    cout<<"The longest string is "<<maxn(x3,5)<<endl;
}
// tempover.cpp --- template overloading
#include <iostream>

template <typename T>            // template A
int SumArray(T arr[], int n);

template <typename T>            // template B
double SumArray(T * arr[], int n);

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

int main()
{
    using namespace std;
    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];
    
    // set pointers to the amount members of the structures in mr_E
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;
    
    cout << "Listing Mr. E's counts of things: "<<SumArray(things, 6)<<endl;
    // things is an array of int
      // uses template A
    cout << "Listing Mr. E's debts: "<<SumArray(pd, 3);
    // pd is an array of pointers to double
          // uses template B (more specialized)
    // cin.get();
    return 0;
}

template <typename T>
int SumArray(T arr[], int n)
{
    using namespace std;
    int total=0;
    for (int i = 0; i < n; i++)
        total+=arr[i];
    return total;
}

template <typename T>
double SumArray(T * arr[], int n)
{
    using namespace std;
    double total=0;
    for (int i = 0; i < n; i++)
        total+=*(arr[i]);
    return total;
}

猜你喜欢

转载自blog.csdn.net/weixin_42365868/article/details/86679895