函数探幽的编程题复习题(c++ prime plus )

第一题 :

1. 编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。

#include <iostream>
#include <stdlib.h>
using namespace std;

void  prints(char *str,int n=1);

int main()
{

    char str[20]="qwertyuiopp";
    prints(str);
    prints(str,3);
    prints(str,6);
    system("pause");
    

}

void prints(char *str,int n)
{
    if(n<=0)
    {
        cout<<"结束"<<endl;
    }
    else
    {
        cout<<str<<endl;
        prints(str,n-1);
    }


}

 

 

 第二题

 

CandyBar结构包含3个成员,第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为“Millennium Munch”、2.85和350。另外,该程序还包含一个以CandyBar的引用为参数,并显示结构内容的函数。请尽可能使用const。

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std; 

typedef struct Candy_Bar
{
    string name;
    double weight;
    int hot;
}SAL;

void set(Candy_Bar &x ,const char * name="whp",const double weight=20.0,const int hot=10);
void show(Candy_Bar & x);


int main()
{
    Candy_Bar x0,x1,x2;
    set(x0);
    show(x0);
    set(x1,"qwe",30.0,3);
    show(x1);
    set(x2,"iop",12.0,12);
    show(x2);
    return 0;
    system("pause");
}

void set(Candy_Bar &x ,const char * name,const double weight,const int hot)
{
    x.name=name;
    x.weight=weight;
    x.hot=hot;
}


void show(Candy_Bar & x)
{
    cout<<"开始输出"<<endl;
    cout<<x.name<<"-"<<x.weight<<"-"<<x.hot<<endl;
    cout<<"结束"<<endl;
}

 

 

 

 第三题

其中对string对象的内容进行大写转换,使用了一个for循环来进行,判断停止可以string对象的长度,也可以直接判断string对象的内容是否为空来做,我在这里就是直接用str[i]来作为判断条件,当str[i]为'\n'或为空时,则表示循环结束。

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

string change(string & str);

int main()
{
    string str;
    cout<<"请输入你的待转字符串"<<endl;
    getline(cin,str);
    while(str!="p")
    {
        str=change(str);
        cout<<"转过后的"<<str<<endl;
        cout<<"再次输入"<<endl;
        getline(cin,str);
    }
    return 0;
}

string change(string & str)
{
    for(int i=0;str[i];i++)
    {
        str[i]=toupper(str[i]);
    }
    return str;
}
第四题

请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个都使用默认参数。请尽可能使用const参数。set()使用new分配足够的空间来存储指定的字符串。这里使用的技术与设计和实现类时使用的相似。(可能还必须修改头文件的名称,删除using编译指令,这取决于所用的编译器)

 

#include <iostream>
using namespace std;
#include <cstring>  // for strlen(), strcpy()


struct stringy {
    char * str;     // points to a string
    int ct;         // length of string (not counting '\0')
    };

// prototypes for set(), show(), and show() go here
void set(stringy & t,const char * str);
void show(stringy & t,const int n=1);
void show(const char *str,const int n=1);


int main()
{
    stringy beany;
    char testing[] = "Reality isn't what it used to be.";

    set(beany, testing); // first argument is a reference,
                // allocates space to hold copy of testing,
                // sets str member of beany to point to the
                // new block, copies testing to new block,
                // and sets ct member of beany
    show(beany);    // prints member string once
    show(beany, 2); // prints member string twice
    testing[0] = 'D';
    testing[1] = 'u';
    show(testing);    // prints testing string once
    show(testing, 3); // prints testing string thrice
    show("Done!");
    return 0;
}

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

void show(stringy & t,const int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<t.str<<endl;
    }
    cout<<endl;
}

void show(const char *str,const int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<str<<endl;
    }
    cout<<endl;
}

 

 

 

 

 第五题

编写模板函数max5(),它将一个包含5个T类型元素的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。

 

#include <iostream>
using namespace std;


template <typename T>
T max5(T arr[5]);




int main()
{

int max1,max2;
int arr1[5]={1,2,3,4,5};
int arr2[5]={7,8,9,4,5};
max1=max5(arr1);
max2=max5(arr2);
cout<<"第一个最大的数值为"<<max1<<endl;
cout<<"第二个最大的数值为"<<max2<<endl;


}

template <typename T>
T max5(T arr[5])
{
    int max;
    max=arr[0]>arr[1]?arr[0]:arr[1];
    max=max>arr[2]?max:arr[2];
    max=max>arr[3]?max:arr[3];
    max=max>arr[4]?max:arr[4];
    return max;
}

 第六题

编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为参数,并返回数组中的最大的元素。在程序对它进行测试,该程序使用一个包含6个int元素的数组和一个包含4个double元素的数组来调用该函数。程序还包含一个具体化,它将char指针数组和数组中的指针数量作为参数,并返回最长的字符串的地址。如果有多个这样的字符串,则返回其中第一个字符串的地址。使用由5个字符串指针组成的数组来测试该具体化。

#include <iostream>
#include <cstring>

using namespace std;

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

template <> char * maxn(char * arr[],int n);

int main()
{
        int max1;
        double max2;
        int arr[5]={1,2,3,4,5};
        double arr1[5]={2.0,5.4,8.1,7.2,9.2};
        cout<<"输出\n"<<endl;
        max1=maxn(arr,5);
        max2=maxn(arr1,5);
        char * arr3[5] ={ "one", "two", "three", "four", "five" };
        char * max3=maxn(arr3,5);
        cout<<max1<<endl;
        cout<<max2<<endl;
        cout<<max3<<endl;

}



template <typename T>
T maxn(T arr[],int n)
{
    T max=0;
    for(int i=0;i<n;i++)
    {
        max=max>arr[i]?max:arr[i];
    }
    return max;
}

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

第七题

 修改程序清单8.14,使其使用两个名为SumArray()的模板函数来返回数组元素的总和,而不是显示数组的内容。程序应显示thing的总和以及所有debt的总和。

 

#include <iostream>
 
template <typename T>
T SumArray(T arr[], int n);
 
template <typename T>
T 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;
    int sum1 = SumArray(things, 6);
    cout << "The sum of things is " << sum1 << endl;
    double sum2 = SumArray(pd, 3);
    cout << "The sum of the debts is " << sum2 << endl;
    system("pause");
    return 0;

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

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

 

posted @ 2022-07-11 15:41  术术子  阅读(74)  评论(0编辑  收藏  举报