c++primer 第四章编程练习答案

4.13.1

#include<iostream>

struct students {
    char firstname[20];
    char lastname[20];
    char grade;
    int age;
};
int main() {
    using namespace std;
    students student1;
    cout << "What is your fistname? ";
    cin.get(student1.firstname, 20).get();
    cout << "What is your lastname? ";
    cin.getline(student1.lastname, 20);
    cout << "What latter grade do you deserve? ";
    cin >> student1.grade;
    cout << "What is your age? ";
    cin >> student1.age;
    cin.get();
    cout << endl;
    cout << "name: " << student1.firstname << " , " << student1.lastname << endl;
    cout << "grade: " << char (student1.grade + 1) << endl;
    cout << "age: " << student1.age;
    cin.get();
}

4.13.2

#include<iostream>

struct students {
    char firstname[20];
    char lastname[20];
    char grade;
    int age;
};
int main() {
    using namespace std;
    students student1;
    cout << "What is your fistname? ";
    cin.get(student1.firstname, 20).get();
    cout << "What is your lastname? ";
    cin.getline(student1.lastname, 20);
    cout << "What latter grade do you deserve? ";
    cin >> student1.grade;
    cout << "What is your age? ";
    cin >> student1.age;
    cin.get();
    cout << endl;
    cout << "name: " << student1.firstname << " , " << student1.lastname << endl;
    cout << "grade: " << char (student1.grade + 1) << endl;
    cout << "age: " << student1.age;
    cin.get();
}

4.13.3

#include <iostream>
#include <cstring>

const int Strlen = 20;

int main()
{
using namespace std;

char first_name[Strlen];
char last_name[Strlen];
char full_name[2 * Strlen];

cout << "Enter your first name: ";
cin.get(first_name, Strlen).get();
cout << "Enter your last name: ";
cin.get(last_name, Strlen).get();

strcpy_s(full_name, last_name);//strcpy经常发生缓存区溢出,被视为是不安全的
strcat_s(full_name, ", ");
strcat_s(full_name, first_name);

cout << "Here's the information in a single string: " << full_name;
cin.get();
}

4.13.4

#include<iostream>
#include<string>

int main() {
    using namespace std;
    string firstname;
    string lastname, fullname;
    cout << "Enter your first name: ";
    getline(cin, firstname);
    cout << "Enter your last name: ";
    cin >> lastname;
    cin.get();
    fullname = firstname + ',' + lastname;
    cout << "Here's the information in a single string: " << fullname;
    cin.get();
}

4.13.5

#include<iostream>
#include<string>

struct CandyBar
{
    std::string brand;
    double weight;
    int calories;
};
S
int main() {
    using namespace std;
    CandyBar snack = {
        "Mocha Munch",
        2.3,
        350
    };
    cout << snack.brand << endl << snack.weight << endl << snack.calories;
    cin.get();
}

4.13.6

#include<iostream>
#include<string>

struct CandyBar
{
    std::string brand;
    double weight;
    int calories;
};

int main() {
    using namespace std;
    int i;
    CandyBar candy[3]{
        {"haha",12.3,24},
        {"jude",11.2,25},
        {"zc",25.2,132}
    };
    for (i = 0; i < 3; i++) {
        cout << "candybar " << i << endl
            << candy[i].brand << endl
            << candy[i].weight << endl
            << candy[i].calories << endl;
    }
    cin.get();
}

4.13.7

#include<iostream>
#include<string>


struct PizzaBar
{
    std::string name;
    float diameter;
    float weight;
};
int main() {
    using namespace std;
    PizzaBar pizza;
    cout << "Please Enter your pizza name: ";
    cin >> pizza.name;
    cout << "Please Enter your pizza diameter: ";
    cin >> pizza.diameter;
    cout << "Please Enter your pizza weight: ";
    cin >> pizza.weight;
    cin.get();
    cout << "your pizza well be ordered ,please confirm!" << endl;
    cout << pizza.name << endl << pizza.diameter << endl << pizza.weight;
    cin.get();
}

4.13.8

#include <iostream>
#include<string>

int main() {
    using namespace std;
    char *name = new char[20];
    double *diameter = new double;
    double *weight = new double;
    cout << "pizza name: ";
    cin >> name;
    cout << "pizza weight: ";
    cin >> *weight;
    cout << "pizza diameter: ";
    cin >> *diameter;
    cin.get();
    cout << "name: " << name << endl;
    cout << "weight:" << *weight << endl;
    cout << "diameter: " << *diameter << endl;
    cin.get();
}

4.13.9

#include<iostream>
#include<string>

using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main() {
    CandyBar *candy = new CandyBar[3];
    int i;
    candy->brand = "dad";
    candy->weight = 12.16;
    candy->calories = 123;
    (candy + 1)->brand = "wqe";
    (candy + 1)->weight = 45.6;
    (candy + 1)->calories = 456;
    (candy + 2)->brand = "zxc";
    (candy + 2)->weight = 78.9;
    (candy + 2)->calories = 789;
    for (i = 0; i < 3; i++) {
        cout << "display " << i+1 << " CandyBar
";
        cout << "brand: " << candy[i].brand << endl;
        cout << "weight: " << candy[i].weight << endl;
        cout << "calories: " << candy[i].calories << endl;
        cout << endl;
    }
    delete [] candy;
    cin.get();
}

4.13.10

#include<iostream>
#include<array>

int main() {
    using namespace std;
    array<double, 3> score;
    double sum = 0;
    int i;
    for (i = 0; i < 3; i++) {
        cout << "input " << i+1 << " score: ";
        cin >> score[i];
        sum += score[i];
        cout << "display " << i + 1 << " average score is " << sum / (i + 1) << endl;
        cin.get();
    }
    cin.get();
}
原文地址:https://www.cnblogs.com/zhang293/p/7523657.html