C++ Primer Plus 第六版编程练习 第四章答案

欢迎讨论纠错~

 

#include <iostream>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

void question_1();
void question_2();
void question_3();
void question_4();
void question_5();
void question_6();
void question_7();
void question_8();
void question_9();
void question_10();

int main()
{
    //question_1();
    question_10();
    return 0;
}

void question_1()
{
    string first_name , last_name;
    char grade;
    int age;
    cout << "What is your first name? ";
    getline(cin,first_name);
    cout << "What is your last name? ";
    getline(cin,last_name);
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;
    cout << "Name: " << last_name << ", " << first_name << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age << endl;
    return; 
}

void question_2()
{
    string name , dessert;
    
    cout << "Enter your name:\n";
    getline(cin,name);
    cout << "Enter your favorite dessert:\n";
    getline(cin,dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
    return;
}

void question_3()
{
    char first_name[50], last_name[50];
    cout << "Enter your first name: ";
    cin >> first_name;
    //char *pa = new char[strlen(first_name)+1];
    //strcpy(pa, first_name);
    cout << "Enter your last name; ";
    cin >> last_name;
    strcat(last_name, ", ");
    strcat(last_name, first_name);
    cout << "Here's the information in a single string: " << last_name << endl;
    return;
}

void question_4()
{
    string first_name, last_name;
    cout << "Enter your first name: ";
    cin >> first_name;
    //char *pa = new char[strlen(first_name)+1];
    //strcpy(pa, first_name);
    cout << "Enter your last name; ";
    cin >> last_name;
    string name = last_name + ", " + first_name;
    cout << "Here's the information in a single string: " << name << endl;
    return;
}

void question_5()
{
    struct CandyBar
    {
        string logo;
        double weight;
        int cal;
    }snack{"Mocha Munch" , 2.3 , 350};
    cout << "snack logo: " << snack.logo << endl;
    cout << "snack weight: " << snack.weight << endl;
    cout << "snack cal: " << snack.cal << endl;
    return;
}

void question_6()
{
    struct CandyBar
    {
        string logo;
        double weight;
        int cal;
    };
    CandyBar snack[3] = 
        {
            {"Mocha" , 2.3 , 350},
            {"Munch" , 1.5 , 29},
            {"X" , 2.5 , 120}
        };
    cout << "snack[0] logo: " << snack[0].logo << endl;
    return;
}

void question_7()
{
    struct pizza
    {
        string company;
        double diameter;
        double weight;
    };
    pizza * William = new pizza[50];
    bool end = 0;
    int count = 0;
    int last = 0;
    while (!end){
        cout << "Enter the company of the pizza: ";
        cin >> (William + count)->company;
        cout << "Enter the diameter of the pizza: ";
        cin >> (William + count)->diameter;
        cout << "Enter the weight of the pizza: ";
        cin >> (William + count)->weight;
        cout << "The pizza is from " << (William + count)->company << ", diameter " << (William + count)->diameter;
        cout << "cm, weight " << (William + count)->weight << "g." << endl;
        cout << "It's the last pizza? (0/1)";
        cin >> end;
        count++;
    }
    delete [] William;
    return;
}

void question_8()
{
    struct pizza
    {
        string company;
        double diameter;
        double weight;
    };
    pizza * William = new pizza[50];
    bool end = 0;
    int count = 0;
    int last = 0;
    while (!end){
        cout << "Enter the diameter of the pizza: ";
        cin >> (William + count)->diameter;
        cout << "Enter the company of the pizza: ";
        cin >> (William + count)->company;
        cout << "Enter the weight of the pizza: ";
        cin >> (William + count)->weight;
        cout << "The pizza is from " << (William + count)->company << ", diameter " << (William + count)->diameter;
        cout << "cm, weight " << (William + count)->weight << "g." << endl;
        cout << "It's the last pizza? (0/1) ";
        cin >> end;
        count++;
    }
    delete [] William;
    return;
}

void question_9()
{
    struct CandyBar
    {
        string logo;
        double weight;
        int cal;
    };
    CandyBar * snack = new CandyBar[3];
    snack[0] = {"Mocha" , 2.3 , 350};
    snack[1] = {"Munch" , 1.5 , 29};
    snack[2] = {"X" , 2.5 , 120};
    cout << "snack[0] logo: " << snack[0].logo << endl;
    delete [] snack;
    return;
}

void question_10()
{
    vector<double> runscore(3);
    cout << "Enter the score of 40m run1: ";
    cin >> runscore[0];
    cout << "Enter the score of 40m run2: ";
    cin >> runscore[1];
    cout << "Enter the score of 40m run3: ";
    cin >> runscore[2];
    cout << "Your average score of 40m run: " << (runscore[0] + runscore[1] + runscore[2])/3 << endl;
    return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值