欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++Primer Plus 习题_第四章

程序员文章站 2023-04-08 09:56:14
#pragma once #ifndef CHAPTER4_H_ #define CHAPTER4_H_ #include #include #include using namespace std; //1&2 struct memberInfo1; class Exercise1 { priva... ......
#pragma once
#ifndef CHAPTER4_H_
#define CHAPTER4_H_
#include <iostream>
#include <string>
#include <array>

using namespace std;

//1&2
struct memberInfo1;
 
class Exercise1
{
private:
     
public:
    void inputInfo1();
    void outpuInfo1();
    void inputInfo2();
     
    //该函数将输入读入到一个大型的临时数组中,然后使用new[]创建一个刚好能够存储该输入字符串
    //的内存块,并返回一个指向内存块的指针。对于读取大量字符串的程序,这种方法可以节省大量内存
    char *getString(); 
};

//3.
class Exercise3
{
private:

public:
    void inputInfo();
    void outputInfo(char *pf, char *pl);
    char *getString();
};

//4.
class Exercise4
{
private:
    string firstName_;
    string lastname_;
public:
    void inputInfo();
    void outputInfo();
};

//5.
struct CandyBar
{
    string brand;
    float weight;
    int calorie;
};
class Exercise5
{
private:
    
public:
    CandyBar candyBar;
    CandyBar setSnack(string brand, float weight, int calorie);
};

//6.

class Exercise6
{
    
private:
     
public:
    CandyBar setSnack(string brand, float weight, int calorie);
     
};

//7.
struct pizza
{
    string brand;
    float diameter;
    float weight;
};
class Exercise7
{
private:
     int size_;
 
public:
    void setSize(int size);
    pizza recordInfo(string brand, float diameter, float weight);
};

//8.
class Exercise8
{
private:
     int size_;
public:
    void setSize(int size);
    pizza* recordInfo(float diameter, string brand, float weight);
};

//9.
class Exercise9
{

private:

public:
    CandyBar *setSnack(string brand, float weight, int calorie);

};

//10.
class Exercise10
{
private:
    array<float,3> arr;
    int time_;
public:
    void recordDistance(int count);
    void showInfo();
};

#endif // !CHAPTER4_H_
#include "stdafx.h"
#include "chapter4.h"


//1&2
struct memberInfo1
{
    string firtsName;
    string lastName;
    char grade;
    int age;
};

memberInfo1 m1;

void Exercise1::inputInfo1()
{
    cout << "请输入您的姓氏:";
    cin >> m1.firtsName;
    cout << "请输入您的名字:";
    cin >> m1.lastName;
    cout << "请输入您的等级:";
    cin >> m1.grade;
    cout << "请输入您的年龄:";
    cin >> m1.age;
}

void Exercise1::outpuInfo1()
{
    cout << "姓:" << m1.firtsName << endl;
    cout << "名:" << m1.lastName << endl;
    cout << "等级:" << m1.grade << endl;
    cout << "年龄:" << m1.age << endl;
}

void Exercise1::inputInfo2()
{
    cout << "请输入您的姓氏:";
    char *pfirstName;
    pfirstName = getString();
    cout << "请输入您的名字:";
    char *plastName;
    plastName = getString();
    cout << "请输入您的等级:";
    char grade;
    cin >> grade;
    cout << "请输入您的年龄:";
    int age;
    cin >> age;
    cin.get();

    cout << "姓:" << pfirstName << "   字符长度为:" << strlen(pfirstName) << endl; //cout<<指针  直接输出指针指向的内存中的值
    cout << "名:" << plastName << "   字符长度为:" << strlen(plastName) << endl;
    cout << "等级:" << grade << endl;
    cout << "年龄:" << age << endl;
    delete[] pfirstName;
    delete[] plastName;
}

char * Exercise1::getString()
{
    char temp[80];
    cin >> temp;
    char *pn = new char[strlen(temp) + 1]; //字符数组表示字符串需在最后留1位用于放换行符"\n"。strlen()返回的长度不包括换行符。
    strcpy_s(pn, strlen(temp) + 1, temp); //将字符temp的值复制一份存到pn指针指向的新内存中。

    return pn;
}//函数运行结束后将自动释放掉temp的内存。记得delate掉pn指针指向的内存。

//3.
void Exercise3::inputInfo()
{
    cout << "请输入您的姓氏:";
    char *pfirstName;
    pfirstName = getString();
    cout << "请输入您的名字:";
    char *plastName;
    plastName = getString();
    outputInfo(pfirstName, plastName);
    delete[] pfirstName;
    delete[] plastName;
}

void Exercise3::outputInfo(char *pf, char *pl)
{
    cout << "姓名:" << pf << "," << pl << endl;
}

char * Exercise3::getString()
{
    char temp[80];
    cin >> temp;
    char *pn = new char[strlen(temp) + 1];
    strcpy_s(pn, strlen(temp) + 1, temp);
    return pn;
}

//4.
void Exercise4::inputInfo()
{
    cout << "请输入您的姓氏:";
    cin >> firstName_;
    cout << "请输入您的名字:";
    cin >> lastname_;

}

void Exercise4::outputInfo()
{
    cout << "姓名:" << firstName_ << ", " << lastname_ << endl;
}

//5.
CandyBar Exercise5::setSnack(string brand, float weight, int calorie)
{
    CandyBar snak = { brand, weight, calorie };
    return snak;
}

//6.
CandyBar Exercise6::setSnack(string brand, float weight, int calorie)
{
    CandyBar snak = { brand, weight, calorie };
    return snak;
}

//7.
void Exercise7::setSize(int size)
{
    size_ = size;
}

pizza Exercise7::recordInfo(string brand, float diameter, float weight)
{
    pizza piz = {};
    piz.brand = brand;
    piz.diameter = diameter;
    piz.weight = weight;
    return piz;
}


//8.
void Exercise8::setSize(int size)
{
    size_ = size;
}

pizza* Exercise8::recordInfo(float diameter, string brand, float weight)
{
    pizza *ppizza = new pizza{ brand,diameter,weight };
    return ppizza;
}

//9.
CandyBar* Exercise9::setSnack(string brand, float weight, int calorie)
{
    CandyBar *pc = new CandyBar{ brand, weight, calorie };
    return pc;
}

//10.
void Exercise10::recordDistance(int time)
{
    time_ = time;
    for (int i = 0; i < time_; i++)
    {
        cout << "请输入第" << i + 1 << "次40码跑的成绩:";
        cin >> arr[i];
    }
}

void Exercise10::showInfo()
{
    float total = 0;
    cout << "共跑了" << time_ << "次" << endl;
    for (int i = 0; i < time_; i++)
    {
        total += arr[i];
    }
    cout << "平均成绩为:" << total / time_ << endl;
} 
// C++Primer Plus 习题_第四章.cpp: 定义控制台应用程序的入口点。
//-------------------------------复习题-----------------------------------------
//1.如何声明下述数据?
//a.actor是由30个char组成的数组。
//b.betsie是由100个short组成的数组。
//c.chuck是由13个float组成的数组。
//d.dipsea是由64个long double组成的数组。
//答案:a.char actor[30];
//        b.short betsie[100];
//        c.float chuck[13];
//        d.long double dispasea[64];

//2.使用模板类array而不是数组来完成问题1.
//答案:include<array>;
//        using namespace std;
//        array<char 30> actor;
//        array<short 100>betsie;
//        array<float 13>chuck;
//        array<long double 64>dispasea;

//3.声明包含5个元素的int数组,并将它初始化为前5个正奇数。
//答案:int oddly[5] = {1, 3, 5, 7, 9};

//4.编写一条语句,将问题3中数组的第一个元素和最后一个元素的和赋值给变量even。
//答案:int sum = oddly[0] + oddly[4];

//5.编写一条语句,显示float数组ideas中第二个元素的值。
//答案:cout << ideas[1] << endl;

//6.声明一个char数组,并将其初始化为字符串"cheeseburger"。
//答案:char lunch[13] = "cheeseburger";  或者  char lunch[] = "cheeseburger";

//7.声明一个string对象,并将其初始化为字符串"Waldorf Salad"。
//答案:include<string>;
//        using namespace;
//        string name = "Waldorf Salad";

//8.设计一个描述鱼的结构声明。结构中应当包括品种、重量(整数,单位盎司)和长度(英寸,包括小数)。
//答案:struct Fish
//        {
//            char kind;
//            int    weight;
//            float length;
//        };     

//9.声明一个问题8中定义的结构的变量,并对它进行初始化。
//答案:Fish fish = {"weever", 2, 12.5};

//10.用enum定义一个名为Response的类型,它包含Yes、No和Maybe等枚举量,其中Yes的值为1,No为0,Maybe为2。
//答案:enum Response
//        {
//            No,
//            Yes,
//            Maybe
//        };

//11.假设ted是一个double变量,请声明一个指向ted的指针,并使用该指针来显示ted的值。
//答案:double *pd = &ted;
//        cout << *pd << "\n";

//12.假设treacle是一个包含10个元素的float数组,请声明一个指向treacle的第一个元素的指针,并使用该指针来显示数组的第一个元素的最后一个元素。
//答案:float *pf = treacle; 或者 float *pf = &treacle[0]; //数组就是指针 或者指向数组第一个元素的地址。
//        cout << pf[0] << " " << pf[9] << endl; 或者 cout << *pf << " " << *(pf + 9);        

//13.编写一段代码,要求用户输入一个正整数,然后创建一个动态的int数组,其中包含元素数目等于用户输入的值。首先使用new来完成这项任务,再使用vector对象来完成这项任务。
//答案:
//方法一:
//unsigned int size;
//cout << "请输入一个元素数目:";
//cin << size;
//int * dyn = new int [size];
//vector<int> dv(size);

//14.下面的代码是否有效?如果有效,它将打印出什么结果?
//cout << (int *) "Home of the jolly bytes";
//答案:是的,它是有效的。表达式"home of the jolly bytes"是一个字符串常量,因此它将判定为字符串开始的地址。cout对象将char地址解释为打印字符串,但类型转换(int *)将地址转换为
//int指针,然后作为地址被打印。总之,该语句打印字符串的地址,只要int类型足够宽,能够存储该地址。

//15.编写一段代码,给问题8中描述的结构动态分配内存,再读取该结构成员的值。
//答案:Fish fish = new Fish{"weever", 2, 12.5};
//        string name = fish.kind;
//或者用指针 
//        fish *p = new Fish{"weever", 2, 12.5};
//        cout << "种类为:" << p-> kind;

//16.程序清单4.6指出混合输入数字和一行字符串时存储的问题。如果将下列的代码:
//cin.getline(address,80);
//替换为:
//cin>>address;
//将对程序的运行带来什么影响?
//答:使用cin>>address 将使得程序跳过空白,直到找到给空白字符为止。然后它将读取字符,直到再次遇到空白为止。因此,它将跳过数字输入的换行符,从未避免这种问题。另一方面,它值读取
//一个单词,而不是整行。

//17.声明一个vector对象和一个对象,它们都包含10个string对象。指出所需的头文件,但不要使用using。使用const来指定要包含的string对象数。
//答:  #include <string>
//        #include <vector>
//        #include <array>
//        const int Str_num {10};   //或者 const int = 10;
//        ....
//        std:vector<std:string> vstr(Str_num);
//        std:array<std:string,Str_num> astr;

//-------------------------------编程练习-----------------------------------------

//1.编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
//    What is your first name? Betty Sue
//    What is your last name? Yewe
//    What letter grade do you deserve? B
//    What is your age? 22
//    Name:Yewe,Betty Sue
//    Grade:C
//    Age:22
//注意:改程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。

//2.修改程序清单4.4,使用C++ string类而不是char数组。

//3.编写一个程序,它要求用户首先输入其名,然后输入其姓:然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组
//和头文件cstring中的函数。下面是该程序运行时的情形:
//    Enter your first name: Flip
//    Enter your last name: Fleming
//    Here's the informarion in a single string: Fleming, Flip

//4.编写一个程序,它要求用户首先输入其名,再输入其姓:然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象
//和头文件string中的函数。下面是该程序运行时的情形:
//    Enter your first name: Flip
//    Enter your last name: Fleming
//    Here's the information in a single string: Fleming, Flip

//5.结构CandyBar包含3个成员,第一个成员存储了糖块的品牌:第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。
//编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员分别初始化为"Mocha Munch"、2.3和350.初始化应在声明snack时进行。最后,
//程序显示snack变量的内容。

//6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序。创建一个包含三个元素的CandyBar数组。并将它们初始化为所选的值。然后显示每个结构
//的内容。

//7.William Wingate 从事披萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
//     披萨饼公司的名称,可以有多个单词组成。
//     披萨饼的直径。
//     披萨饼的重量。
//请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

//8.完成编程练习7,但使用new来为结构分配内存,而不是声明结构变量。另外,让程序在请求输入披萨饼公司名称之前输入披萨饼的直径。

//9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

//10.编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可以让用户输入40米跑的成绩),并显示次数和平均值。请使用一个array对象来存储数据
//(如果编译器不支持array类,请使用数组)。


#include "stdafx.h"
#include "chapter4.h"
 
int main()
{
    //1&2 
    cout << "第一题" << endl;
    Exercise1 ex1;
    ex1.inputInfo1();
    ex1.outpuInfo1();
    cout << "第二题" << endl;
    ex1.inputInfo2();
     
    //3.
    cout << "第三题" << endl;
    Exercise3 ex3;
    ex3.inputInfo();

    //4.
    cout << "第四题" << endl;
    Exercise4 ex4;
    ex4.inputInfo();
    ex4.outputInfo();

    //5.
    cout << "第五题" << endl;
    Exercise5 ex5;
    ex5.candyBar = ex5.setSnack("Mocha Munch",2.3,350);
    cout << "品牌:" << ex5.candyBar.brand << "  " << "重量:" << ex5.candyBar.weight << "  " << "卡路里:" << ex5.candyBar.calorie <<endl;

    //6.
    cout << "第六题" << endl;
    Exercise6 ex6;
    CandyBar * c[3];
    c[0] = &(ex6.setSnack("Mocha Munch", 2.3, 350));
    c[1] = &(ex6.setSnack("uimodle", 2.0, 200));
    c[2] = &(ex6.setSnack("lebin", 3.5, 180));
    for (int i = 0; i < 3; i++)
    {
        cout << "品牌" << i << ":" << c[i]->brand << "  " << "重量:" << c[i]->weight << "  " << "卡路里:" << c[i]->calorie << endl;
    }

    //7.
    cout << "第七题" << endl;
    Exercise7 ex7;
    int const size = 3;
    ex7.setSize(size);
    pizza pizzzaa[size];
    for (int i = 0; i < size; i++)
    {
        string brand;
        float diameter;
        float weight;
        cout << "披萨" << i + 1 << "名称:";
        cin >> brand;
        cout << "披萨" << i + 1 << "直径:";
        cin >> diameter;
        cout << "披萨" << i + 1 << "重量:";
        cin >> weight;
        pizzzaa[i] = ex7.recordInfo(brand, diameter, weight);
    }
    cout << "显示所有输入信息:" << endl;
    for (int i = 0; i < size; i++)
    {
        cout << i + 1 << ":" << pizzzaa[i].brand << "---" << pizzzaa[i].diameter << "---" << pizzzaa[i].weight <<endl;
    }

    //8.
    cout << "第八题" << endl;
    Exercise8 ex8;
    int const size2 = 3;
    ex8.setSize(size2);
    pizza *ppizzz[size2] = {};
    for (int i = 0; i < size2; i++)
    {
        float diameter;
        string brand;
        float weight;
        cout << "披萨" << i + 1 << "直径:";
        cin >> diameter;
        cout << "披萨" << i + 1 << "名称:";
        cin >> brand;
        cout << "披萨" << i + 1 << "重量:";
        cin >> weight;
        ppizzz[i] = ex8.recordInfo( diameter, brand, weight);
    }
    cout << "显示所有输入信息:" << endl;
    
    for (int i = 0; i < size2; i++)
    {
        cout << i + 1 << ": " << ppizzz[i]->brand << "---" << ppizzz[i]->diameter << "---" << ppizzz[i]->weight << endl;
        delete ppizzz[i]; //基本类型的对象没有析构函数,所以回收基本类型组成的数组空间用 delete 和 delete[] 都是应该可以的;
        //但是对于类对象数组,只能用 delete[]。对于 new 的单个对象,只能用 delete 不能用 delete[] 回收空间。
        //new 和 delete、new[] 和 delete[] 对应使用。
    }

    //9.
    cout << "第九题" << endl;
    Exercise9 ex9;
    CandyBar * c2[3];
    c2[0] = ex9.setSnack("Mocha Munch", 2.3, 350);
    c2[1] = ex9.setSnack("uimodle", 2.0, 200);
    c2[2] = ex9.setSnack("lebin", 3.5, 180);
    for (int i = 0; i < 3; i++)
    {
        cout << "品牌" << i+1 << ":" << c2[i]->brand << "  " << "重量:" << c2[i]->weight << "  " << "卡路里:" << c2[i]->calorie << endl;
        delete c2[i];
    }

    //10.
    cout << "第十题" << endl;
    Exercise10 ex10;
    ex10.recordDistance(3);
    ex10.showInfo();
 
    return 0;
}