C++ Primer Plus(第6版) 中文版 第七章编程练习

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

#include <iostream>

using namespace std;

void prt(std::string str,int num = 0)
{
    static int count = 0;
    count++;
    if(num!=0)
    {

        for(int i =0 ;i<count;i++)
        {
            std::cout<<str<<std::endl;
        }

    }
    else
    {
        std::cout<<str<<std::endl;
    }
}

int main()
{

    prt("Hello World!",0);
    prt("Hello World!",5);
    prt("Hello World!",5);
    prt("Hello World!",5);
    prt("Hello World!",0);
    return 0;
}

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

#include <iostream>

using namespace std;

struct CandyBar {
    const char* brand;
    double weight;
    int heat;
};

void setData(CandyBar& bar, const char* brand = "Millennium Munch",double weight = 2.85,int heat = 350)
{
    bar.brand = brand;
    bar.heat = heat;
    bar.weight = weight;

}
void outData(const CandyBar& bar)
{
    cout<<bar.brand<<endl<<bar.weight<<endl<<bar.heat<<endl;
}
int main()
{
    CandyBar bar = { "aaa",10,2222};
    setData(bar,"bbb",20,333);
    outData(bar);
    setData(bar);
    outData(bar);

    return 0;
}


3.编写一个函数,它接受一个指向string对象的引用作为参数,并将该 string对象的内容转换为大写,为此可使用表 6.4描述的函数 toupper()。然后编写一个程序,它通过使用一个循环让您能够用不同的输入来测试这个函数,该程序的运行情况如下:

Enter a string (q to quit): go away

GO ANAY

Next string (q to quit): good grief

GOOD GRIEF

Next string(q to quit): q

Bye

#include <iostream>
#include <string.h>


using namespace std;


void toupper(string & str)
{
    int num  = str.size();
    for(int i =0;i<num;i++)
    {

        if(str.at(i)>='a'&&str.at(i)<='z')
            str.at(i) = str.at(i)-((int)'a'-(int)'A');

    }
}

int main()
{
    char str[100];
    while(true)
    {
        cout<<"Enter a string (q to quit): ";
        cin.getline(str,100);
        if(!strcmp(str,"q"))
        {
            cout<<"Bye"<<endl;
            break;
        }
        string str2 = str ;
        toupper(str2);
        cout<<str2<<endl;
    }


    return 0;
}

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

#include <iostream>
#include <string>
#include <cstring> // For strlen
using namespace std;

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

void show(const stringy& str ,int num =1)
{

    for(int i=0;i<num;i++)
    {
        cout<<str.str<<endl;
    }

}
void show(const char* str,int num = 1)
{
    for(int i=0;i<num;i++)
    {
        cout<<str<<endl;
    }
}
void set(stringy & beany, const char * testing) {
    beany.ct = strlen(testing);
    beany.str = new char[beany.ct + 1]; // Allocate memory for string and null terminator
    strcpy(beany.str, testing); // Copy testing to beany.str
}

int main()
{


    stringy beany;
    char testing[] = "reality isn't whit 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!");
    return 0;
}

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

#include <iostream>

using namespace std;

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


int main()
{
    int arr[5] = {1,2,3,4,5};
    double arrd[5] = {1.00,2.00,3.00,6.00,7.00};

    int max = max5(arr);
    cout<<max<<endl;
    double maxd = max5(arrd);
    cout<<maxd<<endl;
    return 0;
}

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

#include <iostream>

using namespace std;

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

template<typename T>
void ShowArray(const T arr[], int n) {
    T sum = 0;
    cout << "Sum: ";
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    cout << sum << endl;
}

template <typename T>
void ShowArray(T *arr[],int n)
{
    T sum = 0;
    cout << "Sum: ";
    for (int i=0;i<n; i++)
    {
        sum += *arr[i];

    }
    cout<<sum<<endl;;
}



int main()
{
    int things[6] = { 13,13,103,301,310,130};
    struct debts mr_E[3] =
    {
        {"Ima Wolfe",2400.0},
        {"Ura Foxe", 2400.0},
        {"Iby Stout",2400.0},

    };
    double * pd[3] ;

    for(int i = 0;i<3;i++)
        pd[i] = &mr_E[i].amount;


    ShowArray(things,6);

    ShowArray(pd,3);



    return 0;
}

相关推荐

  1. C++ Primer Plus(6) 中文版 编程练习

    2024-04-12 11:42:01       17 阅读
  2. c++ primer中文版作业

    2024-04-12 11:42:01       20 阅读
  3. 中文编程入门(Lua5.4.6中文版 Lua 函数

    2024-04-12 11:42:01       20 阅读
  4. 中文编程入门(Lua5.4.6中文版 Lua 运算符

    2024-04-12 11:42:01       18 阅读
  5. C Primer Plus()11.13 编程练习 6

    2024-04-12 11:42:01       23 阅读
  6. C Primer Plus()15.9 编程练习 6

    2024-04-12 11:42:01       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-12 11:42:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-12 11:42:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-12 11:42:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-12 11:42:01       20 阅读

热门阅读

  1. 0基础刷图论最短路 1(从ATcoder 0分到1800分)

    2024-04-12 11:42:01       22 阅读
  2. 关于conda安装pytorch gpu总是会自动变成cpu版本

    2024-04-12 11:42:01       18 阅读
  3. 时间戳与时间锁区别与联系

    2024-04-12 11:42:01       25 阅读
  4. 【数据结构】2.包装类&简单认识泛型

    2024-04-12 11:42:01       19 阅读
  5. 【备忘】npm yarn pnpm 命令对比

    2024-04-12 11:42:01       23 阅读
  6. Spring Boot 经典面试题(三)

    2024-04-12 11:42:01       21 阅读
  7. 4.11Qt

    4.11Qt

    2024-04-12 11:42:01      21 阅读
  8. 【浮点数加法】

    2024-04-12 11:42:01       20 阅读
  9. Circuits--Sequential--More circuits

    2024-04-12 11:42:01       21 阅读
  10. unity之URP多相机和URP多通道渲染

    2024-04-12 11:42:01       16 阅读
  11. 蓝桥杯 总结经典基础题型

    2024-04-12 11:42:01       13 阅读
  12. 基于springboot的大学生就业招聘系统源码数据库

    2024-04-12 11:42:01       15 阅读