C++ Primer Plus(6th)第四章 编程练习题答案

1.输出实例显示信息

#include "stdafx.h"
#include

using namespace std;

int main()
{
    char first_name[32];
    char last_name[32];
    char letter;
    int age;

    cout<<"What is your first name? ";
    cin.getline(first_name, 128);

    cout<<"What is your last name? ";
    cin.getline(last_name, 128);

    cout<<"What letter grade do you deserve? ";
    cin>>letter;

    cout<<"What is your age? ";
    cin>>age;

    cout<<"Name: "<

2. 修改4.4

#include "stdafx.h"
#include
#include

using namespace std;

int main()
{
    const int ArSize = 20;
    string name;
    string dessert;

    cout<<"Enter your name:\n";
    getline(cin,name);
    cout<<"Enter your favourite dessert:\n";
    getline(cin, dessert);
    cout<<"I have some delicious "<

3.先姓再名

#include "stdafx.h"
#include
#include

using namespace std;

int main()
{
    char first_name[256];
    char last_name[256];

    cout<<"Enter your fitst name:";
    cin.getline(first_name, 256);
    cout<<"Enter your last name:";
    cin.getline(last_name, 256);
    cout<<"Here's your infomation is a single string: "
        <

4.先姓再名

#include "stdafx.h"
#include
#include

using namespace std;

int main()
{
    string first_name;
    string last_name;

    cout<<"Enter your first name: ";
    getline(cin,first_name);
    cout<<"Enter your last name: ";
    getline(cin,last_name);
    cout<<"Here's your infomation is a single string: "<< last_name
        << ", "<

5.CandyBar

#include "stdafx.h"
#include
#include

using namespace std;

typedef struct CandyBar{
	string name;
	double weight;
	int calories;
};

int main()
{
    CandyBar candybar={"Mocha Munch", 2.3, 350};
    cout<<"the infomation of CandyBar, Name: "<

6.CandyBar

#include "stdafx.h"
#include
#include

using namespace std;

typedef struct CandyBar{
    string name;
    double weight;
    int calories;
};

int main()
{
	CandyBar candybar[3]={
        {"HI",2.3,350},
        {"OH",3.5,400},
        {"HA",3.0,370}
    };

    for(int i=0;i<3;i++)
    {
        cout<<"the infomation of CandyBar, Name: "<

7.披萨

#include "stdafx.h"
#include
#include

using namespace std;

typedef struct PIZZA_INFO{
    string name;
    double size;
    double weight;
};

int main()
{
    PIZZA_INFO pizz_info;

    cout<<"Enter the pizza company name: ";
    getline(cin, pizz_info.name);

    cout<<"Enter the pizza size: ";
    cin>>pizz_info.size;

    cout<<"Enter the pizza weight: ";
    cin>>pizz_info.weight;

    cout<<"The company name of pizza is "<

8.完成练习7,使用new来为结构分配内存

#include "stdafx.h"
#include
#include

using namespace std;

typedef struct PIZZA_INFO {
    string name;
    double size;
    double weight;
};

int main()
{
    PIZZA_INFO *pizza_info = new PIZZA_INFO;

    cout<<"Enter the pizza size: ";
    cin>>pizza_info->size;

    cout<<"Enter the pizza company name: ";
    cin>>pizza_info->name;

    cout<<"Enter the pizza weight: ";
    cin>>pizza_info->weight;

    cout<<"The company name of pizza is "<name
        <<", the size of the pizza is "<size
	<<", the weight of the pizza is "<weight<

9.完成练习6,使用new来动态分配数组

#include "stdafx.h"
#include
#include

using namespace std;

typedef struct CandyBar{
    string name;
    double weight;
    int calories;
};

int main()
{
    CandyBar *pcandybar=new CandyBar[3]{
        {"HI",2.3,350},
        {"OH",3.5,400},
        {"HA",3.0,370}
    };

    for(int i=0;i<3;i++)
    {
        cout<<"the infomation of CandyBar, Name: "<

10.输入三次40码跑的成绩

#include "stdafx.h"
#include
#include

using namespace std;

int main()
{
    array time;
    double avg_time = 0.0;

    printf("Enter three results time of runing 40 meters: ");
    cin>>time[0];
    cin>>time[1];
    cin>>time[2];

    avg_time=(time[0]+time[1]+time[2])/3;
    cout<<"Result: "<

 

 

你可能感兴趣的:(C++ Primer Plus(6th)第四章 编程练习题答案)