首页 > 其他分享 >第8章函数探幽

第8章函数探幽

时间:2022-08-24 15:01:51浏览次数:50  
标签:const 函数 show int char 探幽 include string

第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];
}

标签:const,函数,show,int,char,探幽,include,string
From: https://www.cnblogs.com/lofly/p/16619338.html

相关文章

  • sorted函数
    描述sorted() 函数对所有可迭代的对象进行排序操作。sort与sorted区别:sort是应用在list上的方法,sorted可以对所有可迭代的对象进行排序操作。list的sort方......
  • pytest内置fixture函数request.cls的使用
    官方文档解释源码(FixtureRequest类中)@propertydefcls(self):"""Class(canbeNone)wherethetestfunctionwascollected."""if......
  • PHP array_chunk()函数
    array_chunk()函数是PHP中的内置函数,用于根据传递给函数的参数将数组拆分为给定大小的部分或块。最后一个块可能包含的元素少于块的所需大小。语法:arrayarray_chunk($ar......
  • django中聚合函数查询和分组聚合查询
    聚合函数:Max,Min,Count首字母都要大写,且后面的参数加‘’号,不然会报错,还有就是,如果是Count(')的话,需要加个别名,比如(m=Count('')),不然会报错,所以为了记住,我们平时MaxMin的......
  • 6.函数重载(重点)
    1.函数重载是:允许函数名相同,这种现象叫函数重载2.函数重载的作用:是为了方便使用函数名3.函数重载的条件:同一个作用域,参数的个数不同,参数的顺序不同,参数的类型不同//参数......
  • ref函数
    <template><div><h1>vue3</h1><span>{{name}}-{{age}}</span><button@click="refname">函数修改name</button><button@click="name='刚'">......
  • reactive函数
    <template><div><h1>vue3</h1><span>{{info.name}}-{{info.age}}</span><button@click="infobtn">修改info</button></div></template><scrip......
  • 基础函数
    Oracle函数:nvl(test,exp1):当test为空时,取exp1的值,否则就取test的值selectnvl('abc','bb')fromdual;--123selectnvl(null,'bb')fromdual;......
  • Java-List集合字段求和函数
    一、FunctionCustom通用求和函数使用示例二、求和函数修订记录版本是否发布2020-01-25v1.0是一、FunctionCustom通用求和函数使用示例特点:简化代码......
  • C学习笔记:自己写的函数实现strtok函数的功能
    intsign(char*str,char*sep)//遍历寻找符合的符号{while(*sep)//遍历sep字符数组的符号{if(*sep==*str)return1;//符合条件......