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

1.

#include <iostream>
int main()
{
    using namespace std;

    char firstname[100];
    char lastname[100];
    char grade;
    int age;

    cout << "what is your first name?";
    cin.getline(firstname, 100);//只能是getline,不能是get或cin
    cout << "what is your last name?";
    cin.getline(lastname, 100);
    cout << "what letter grade do you deserve?";
    cin >> grade;
    grade = grade+1;
    cout << "what is your age?";
    cin >> age;

    cout << "Name: " << lastname << ", " << firstname << endl;
    cout << "Grade: " << grade << endl ;
    cout << "Age: " << age << endl;

    return 0;
}

2.

#include <iostream>
int main()
{
    using namespace std;
    string name;
    string dessert;

    cout << "Enter your name:\n";
    getline(cin, name);//str类 getline(cin, str) ; char类 cin.getline(char, 20)
    cout << "Enter your favorite desert:\n";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert << " for you, " << name << ".\n";

    return 0;
}

3.

#include <iostream>
#include <cstring>
int main()
{
    using namespace std;
    char firstname[30];
    char lastname[30];

    cout << "Enter your first name:";
    cin.getline(firstname, 30);
    cout << "Enter your last name:";
    cin.getline(lastname, 30);
    strcat(lastname,","); //<cstring>, 字符拼接strcat(char1,char2)
    strcat(lastname, firstname);
    cout << "Here's the information in a single string:" << lastname << endl;

    return 0;
}

4.

#include <iostream>
#include <string>
int main()
{
    using namespace std;
    string firstname;
    string lastname;
    string wholename;

    cout << "Enter your first name:";
    getline(cin, firstname);
    cout << "Enter your last name:";
    getline(cin, lastname);
    wholename = firstname + ", " + lastname; //str类 str=str1 + str2
    cout << "Here's the information in a single string:" << wholename << endl;

    return 0;
}

5.

#include <iostream>
struct CandyBar
{
    char brand[100];
    float weight;
    int caloie;
};

int main()
{
    using namespace std;
    CandyBar snack=
            {
                "Mocha Munch",
                2.3,
                350
            };
    cout << "Brand: " << snack.brand << endl;
    cout << "Weight: " << snack.weight << endl;
    cout << "Caloie: " << snack.caloie << endl;

    return 0;
}

6.

#include <iostream>
struct CandyBar
{
    char brand[100]; //3个成员
    float weight;
    int caloie;
};

int main()
{
    using namespace std;
    CandyBar snack[3]=
            {
                    {"A A", 1.1, 100},//3个元素
                    {"B B", 2.2, 200},
                    {"C C", 3.3, 300}
            };
    cout << "Brand1: " << snack[0].brand << endl;
    cout << "Weight1: " << snack[0].weight << endl;
    cout << "Caloie1: " << snack[0].caloie << endl;
    cout << "Brand2: " << snack[1].brand << endl;
    cout << "Weight2: " << snack[1].weight << endl;
    cout << "Caloie2: " << snack[1].caloie << endl;
    cout << "Brand2: " << snack[2].brand << endl;
    cout << "Weight2: " << snack[2].weight << endl;
    cout << "Caloie2: " << snack[2].caloie << endl;

    return 0;
}

7.

#include <iostream>
struct Pizza
{
    char brand[100]; //3个成员
    float diameter;
    float weight;
};

int main()
{
    using namespace std;
    Pizza example;

    cout << "Please enter your pizza's information:\n";
    cout << "Brand: ";
    cin.getline(example.brand, 100);
    cout << "Diameter: ";
    cin >> example.diameter;
    cout << "Weight: ";
    cin >> example.weight;

    cout << "The following is your pizza's information:\n";
    cout << "Brand: " << example.brand << endl;
    cout << "Diameter: " << example.diameter << "cm \n";
    cout << "Weight: " << example.weight << "kg \n";

    return 0;
}

8.

#include <iostream>
struct Pizza
{
    char brand[100]; //3个成员
    float diameter;
    float weight;
};

int main()
{
    using namespace std;
    Pizza * p;//定义指向结构体类型Student的数据的指针变量p
    p = new Pizza;//用new运算符开辟一个存放Student型数据的空间
    // 或Pizza * p = new Pizza;

    cout << "Please enter your pizza's information:\n";
    cout << "Brand: ";
    cin.getline(p->brand, 100);
    cout << "Diameter: ";
    cin >> p->diameter;
    cout << "Weight: ";
    cin >> p->weight;

    cout << "The following is your pizza's information:\n";
    cout << "Brand: " << p->brand << endl;
    cout << "Diameter: " << p->diameter << "cm \n";
    cout << "Weight: " << p->weight << "kg \n";

    delete p;
    return 0;
}

9.

#include <iostream>
struct CandyBar
{
    char brand[100]; //3个成员
    float weight;
    int caloie;
};

int main()
{
    using namespace std;
    CandyBar * snack;
    snack = new CandyBar[3];
    snack[0] = {"A A", 1.1, 100};//3个元素
    snack[1] = {"B B", 2.2, 200};
    snack[2] = {"C C", 3.3, 300};

    cout << "Brand1: " << snack[0].brand << endl;
    cout << "Weight1: " << snack[0].weight << endl;
    cout << "Caloie1: " << snack[0].caloie << endl;
    cout << "Brand2: " << snack[1].brand << endl;
    cout << "Weight2: " << snack[1].weight << endl;
    cout << "Caloie2: " << snack[1].caloie << endl;
    cout << "Brand3: " << snack[2].brand << endl;
    cout << "Weight3: " << snack[2].weight << endl;
    cout << "Caloie3: " << snack[2].caloie << endl;

    delete [] snack;
    return 0;
}

10.

#include <iostream>
#include <array>
using namespace std;

int main()
{
    array<double , 3> p;
    cout << "Enter 1st score: ";
    cin >> p[0];
    cout << "Enter 2nd score: ";
    cin >> p[1];
    cout << "Enter 3rd score: ";
    cin >> p[2];

    cout << "1st score: " << p[0] << ", " << "2nd score: " << p[1] << ", " << "3rd score: " << p[2] << endl;
    cout << "Average score: " << (p[0]+p[1]+p[2])/3;

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值