C++ Primer Plus 第六版 第八章 练习题参考答案

//1 ************************************
using namespace std;
void disp(const char  *s,int n = 1 );
static int l = 0;
int main(){ 
    cout << "please input:\n";
    char s[20];
    cin.get(s, 20);
    disp(s);
    cout << "the another version which uses two parameters: \n";
    disp(s, 5);
    system("pause");
    return 0;
}
void disp(const char  *s, int n ) {
    l++;
    cout << " enter is : "<<endl;
    if (n) {
        for (int i = 0; i < l; ++i) {
            cout << s << endl;
        }
    }
    cout << "\n";
}
//2 ************************************
const int max = 20;
using namespace std;
struct candybar {
    char broad[max];
    double weight;
    int caro;
};
void set(candybar &, char * b = "Millennium Munch", double w = 2.85, int c = 350);
void show(const candybar &);
int main(){ 
    cout << "please input the info: \n " << endl;
    candybar  p;
    char bro[max];
    cout << "enter the broad name:\n";
    cin.get(bro, max);
    cout << "enter the weight \n";
    double we;
    cin >> we;
    cin.get();
    cin.get();
    int ca;
    cout << "enter the caro: \n";
    cin >> ca;
    set(p,bro,we,ca);
    show(p);
    system("pause");
    return 0;
}
void show(const candybar & ca) {
    cout << "******the info****** \n";
    cout << "the broad name: "<<ca.broad<<endl;
    cout << "the weight: " << ca.weight << endl;
    cout << "the caro: " << ca.caro << endl;
}
void set(candybar &ca, char * b , double w, int c )
{
    int i = 0;
    for (i = 0; b[i] != '\n'&&i < strlen(b); ++i)
        ca.broad[i] = b[i];
    ca.broad[i] = '\0';
    ca.weight = w;
    ca.caro = c;
}
//3************************************
using namespace std;
void upper(string &s);
int main()
{   
    string s;
    cout << "Enter a string (q tp quit): ";
    while (cin >> s&&s != "q")
    {
        upper(s);
        cout << "Next string(q to quit): ";
    }
    system("pause");
    return 0;
}
void upper(string &s)
{

    for (int i = 0; i < s.size(); ++i) {
        s[i] = toupper(s[i]);
        cout << s[i] ;
    }
    cout << endl;
}
//4************************************
#include<string>
#include<cstring>
using namespace std;
struct stringy {
    char *str;
    int ct;
};
void set(stringy &s,char *);
void show(const stringy &s,int n=1);
void show(const char *s,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!");
    system("pause");
    return 0;
}
void set(stringy &s,char *ch)
{
    s.ct = strlen(ch);
    s.str = new char[s.ct+1];
    strcpy_s(s.str,strlen(ch)+1 ,ch);
}
void show(const stringy &s,int n)
{
    while (--n > 0)
        cout << s.str<<endl;
}
void show(const char *s,int n)
{
    while (--n > 0)
        cout << s << endl;
}
//5************************************
template<typename T>
T max(T a[5]);
int main()
{   
    using namespace std;
    int a[5] = { 0,1,2,3,4 };
    double b[5] = { 0.0,1.1,2.2,3.3,4.4 };
    cout<< max(a) <<endl;
    cout<< max(b) <<endl;
    system("pause");
    return 0;
}
template<typename T>
T max(T a[5])
{
    T temp = 0;
    for (int i = 0; i < 5; ++i)
    {
        temp = a[i] > a[i + 1] ? a[i] : a[i+1];
    }
    return temp;
}
//6************************************
template<typename T>
T maxn(T a[],int n);
template <>char *maxn(char **s, int n);

int main()
{   
    using namespace std;
    int a[6] = { 1,2,3,4,5,6 };
    double b[4] = { 1,2,3,4 };
    cout << maxn(a, 3) << endl;
    cout << maxn(b, 3) << endl;
    char *str[3] = { "abc","abcd","abcde" };
    cout << maxn(str, 3) << endl;
    system("pause");
    return 0;
}
template<typename T>
T maxn(T a[],int n)
{
    T temp = 0;
    for (int i = 0; i < n; ++i)
    {
        temp = a[i] > a[i + 1] ? a[i] : a[i + 1];
    }
    return temp;
}
template <>char *maxn(char **s, int n)
{

    for (int i = 1; i < n; i++)
        s[0] = strlen(s[i]) > strlen(s[0]) ? s[i] : s[0];
    return s[0];
}
//7************************************
template <typename T>           
void SumArray(T arr[], int n);
template <typename T>           
void 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];
    for (int i = 0; i < 3; i++)
        pd[i] = &mr_E[i].amount;

    cout << "Listing Mr. E's counts of things:\n";
    SumArray(things, 6);  
    cout << "Listing Mr. E's debts:\n";
    SumArray(pd, 3);  

    system("pause");
    return 0;
}
template <typename T>
void SumArray(T arr[], int n)
{
    using namespace std;
    T temp = 0;
    cout << "The sum of template A\n";
    for (int i = 0; i < n; i++)
        temp += arr[i];
    cout << temp << endl;
}
template <typename T>
void SumArray(T * arr[], int n)
{
    using namespace std;
    T temp = 0;
    cout << "The sum of template B\n";
    for (int i = 0; i < n; i++)
        temp += *arr[i];
    cout << temp << endl;
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容