• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LOFLY
终其一生,编织快乐
博客园    首页    新随笔    联系   管理    订阅  订阅

第8章函数探幽

第8章函数探幽

第8章函数探幽

编程题

第1题 编写一个接受参数(字符串地址)并输出该字符串的函数。然而,如果提供了第2个参数(int类型),且该参数不为0,则该函数输出字符串的次数将为调用该函数的次数(注意,字符串的输出次数不等于第2个参数的值),而等于函数被调用的次数)。
#include <iostream>
using namespace std;


void loop_print(const char *str , int n = 0);

int main(int argc, char const *argv[])
{
    
    loop_print("Hello World!");
    loop_print("Hello World!");
    loop_print("Hello World!",1);
    return 0;
}

void loop_print(const char * str, int n ){
    static int count = 0;  // 使用静态变量存储函数的调用次数
     count ++ ;
    if(!n){
        cout << str << endl;
    }

    else{
        for(int i = 0;i < count; i++){
            cout << "Print No." << i+1 << "." << endl;
            cout << str << endl;
        }
    }
}
第2题 CandyBar结构体包含3个成员。 第一个成员存储品牌名称,第二个成员是重量(小数), 第3个成员是热量(整数)。 请编写这样的函数, 以CandyBar的引用, char指针,double值和int值作为参数, 并用最后3个值设置相应的结构体成员。 最后3个参数的默认值分别为Millennium Munch , 2.85和350. 另外, 该程序还包含一个以CandyBar的引用为参数并显示结构体内容的数量。请尽可能使用const。
#include <iostream>
using namespace std;
#include <string>

struct CandyBar{
    string brand;
    double weight;
    int colories;
};

void createCandy(CandyBar  &, string, double , int);
void show(const CandyBar &);

int main(int argc, char const *argv[])
{
    CandyBar cb;
    createCandy(cb, "Millennium Munch",2.85,3);
    show(cb);
    return 0;
}

void createCandy(CandyBar  & candy,  string brand, double weight, int colories){
    candy.brand = brand;
    candy.weight = weight;
    candy.colories = colories;
}

void show(const CandyBar & cb){

    cout << "Candy info:" << endl;
    cout << "Candy Brand:" << cb.brand << endl;
    cout << "Candy Weight: " << cb.weight << endl;
    cout << "Candy Colories: " << cb.colories << endl;
}
第3题 编写一个函数,它以一个指向string 对象的引用为参数, 并将该string对象的内容转换为大写, 为此使用表6.4中的toupper()。然后编写一个程序,它通过使用一个循环实现用不同的输入来测试这个函数。 该程序的定义如下:

Enter a string (q to quit): go away
GO AWAY
NEXT string (q to quit) :good grief!
GOOD grief!
Next String (q to quit):q
Bye;
#include <iostream>
using namespace std;
#include <string>
#include <cctype>

void stringToUpper(string &);

int main(int argc, char const *argv[])
{
    // string str = "Hello World";
    // cout << "Before translate :" << str << endl;
    // stringToUpper(str);
    // cout << "After translate :" << str << endl;

    string st;
    cout << "Enter a string (q to quit): " ;
    getline(cin , st);
    while(st != "q"){
        stringToUpper(st);
        cout << st << endl;
        cout << "Next string  (q to quit) :" ;
        getline(cin, st);
    }
    cout << "Bye." << endl;
    return 0;
}


void stringToUpper(string & str){
    int size = str.size();
    // for(string::iterator begin = str.begin(); begin != str.end(); begin ++ ){

    // }

    for(int i = 0; i < size; i++){
        if(islower(str[i])){
            str[i] = toupper(str[i]);
        }
    }
} 
第4题 下面是一个程序框架:
#include <iostream>
using namespace std;
#include <cstring>  // 为了使用strcpy()  strlen()

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


void show(const string & ,  int n =0);
void show(const stringy &, int n = 0);
void set(stringy &, char *);
int main(int argc, char const *argv[])
{
    
    stringy beany;
    char testing[] = "Reality isn't what it used to be." ;
    set(beany , testing);

    show(beany);
    show(beany,2);
    testing[0] = 'P';
    testing[1] = 'A';

    show(testing);
    show(testing,3);
    show("Done!");
    
    /*回收堆内存*/
    delete beany.str;



    return 0;
}



void show(const string & st ,  int n ){
    if(n == 0 ) n++;
    for(int i = 0; i < n ;i++){
        cout << st << endl;
    }
}
void show(const stringy & sty, int n ){

    if (n == 0){
        n ++ ;
    }

    for(int i = 0; i < n;i++){
        cout << sty.str << endl;
    }
}
/*输出 stringy 类型对象的信息*/
void set(stringy & sty, char *st){

    sty.ct = strlen(st);
    sty.str = new char[sty.ct];
    /*通过new 创建动态存储,此处不考虑回收*/
    strcpy(sty.str,st);
}
第5题 编写函数模板max5(), 它以一个包含5个T类型元素的数组作为参数,并返回 数组中的最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换成一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。
#include <iostream>
using namespace   std;

template <class T>
T max5(T [] , int n=5);

int main(int argc, char const *argv[])
{
    int  x[] = {1,2,3,4,5};
    int res = max5(x);
    cout << "max value of x array : " << res << endl;

    double y[] = {4.8,3.4,9.8,10,1.2};
    cout << "max value of y array:" << max5(y) << endl;
    return 0;
}

template <class T>
T max5(T  x[] , int n ){
    int max = x[0];

    for(int i = 0; i < n ; i++){
        max = max < x[i] ? x[i]: max;
    }

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

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

template <> char* maxn(char * str[], int n );
int main(int argc, char const *argv[])
{
    
    int arr[5] = {1,2,3,4,5};
    char *str [5] = {"wqewfw","dfg","fweshif","revdser","Hellofwe"};

    cout << "Max value int arr " << maxn(arr, 5) << endl;
    cout << "Max length string int str " << maxn(str, 5) << endl;
    
    return 0;
}

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

template <> char* maxn<char*>(char * str[], int n ){
    cout << "Hello " << endl;
    int pos = 0;
    for(int i =0 ;i < n; i++){
        if(strlen(str[pos])  <  strlen(str[i])){
            pos = i;
        }
    }
    return str[pos];
}
posted @ 2022-08-24 14:53  编织快乐  阅读(59)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3