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

1.输出实例显示信息

#include "stdafx.h"
#include<iostream>

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: "<<last_name<<", "<<first_name<<endl;
    cout<<"Grade: " <<char(letter + 1)<<endl;
    cout<<"Age: " <<age<<endl;
    return 0;
}

2. 修改4.4

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

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 "<<dessert;
    cout<<" for you, "<<name<<endl;

    return 0;
}

3.先姓再名

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

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: "
        <<last_name<<", "<<first_name<<endl;

    return 0;
}

4.先姓再名

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

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
        << ", "<<first_name<<endl;

    return 0;
}

5.CandyBar

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

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: "<<candybar.name
        <<",Weight: "<<candybar.weight<< ","<<" Calories: "
        <<candybar.calories<<"."<<endl;
    return 0;
}

6.CandyBar

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

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: "<<candybar[i].name
            << ", Weight: "<<candybar[i].weight<<", " 
            <<" Calories: "<<candybar[i].calories << "." << endl;
    }
    return 0;
}

7.披萨

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

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 "<<pizz_info.name
        <<", the size of the pizza is "<<pizz_info.size
	<<", the weight of the pizza is "<<pizz_info.weight<<endl;

    return 0;
}

    

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

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

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 "<<pizza_info->name
        <<", the size of the pizza is "<<pizza_info->size
	<<", the weight of the pizza is "<<pizza_info->weight<<endl;

    delete pizza_info;

    return 0;
}

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

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

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: "<<pcandybar[i].name
            <<", Weight: "<<pcandybar[i].weight<<", "
            <<" Calories:"<<pcandybar[i].calories<<"."<<endl;
    }

    delete[] pcandybar;
    return 0;
}

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

#include "stdafx.h"
#include<iostream>
#include<array>

using namespace std;

int main()
{
    array<double, 3> 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: "<<time[0]<<", "<< time[1]<<", "<<time[2]<<endl;
    cout<<"Average result: "<<avg_time;

    return 0;
}

 

猜你喜欢

转载自blog.csdn.net/zcysilent/article/details/81701276
今日推荐