C++ primer plus 第六版编程题解答(第四章)

4.1

#include <iostream>
#include <typeinfo>
using namespace std;
int main(){
    char f_name[10], l_name[10];
    char grade;
    int age;
    cout << "What's your first name? ";
    cin.getline(f_name,10);
    cout << "What's your last name? ";
    cin >> l_name;
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What's your age? ";
    cin >> age;
    cout << "Name: " << l_name << ", " << f_name << endl;
    cout << "Grade: " << char(grade+1) << endl;
    cout << "Age: " << age;
    return 0;
}

4.2

#include <iostream>
#include <string>
int main(){
    using namespace std;
    string name, dessert;
    cout << "Enter your name:\n";
    getline(cin,name); // cin.getline()针对字符数组,getline()针对string类型而不是char*,需要引入string库
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
    return 0;
}

4.3

#include <iostream>
using namespace std;
int main(){
    char f_name[20], l_name[20];
    cout << "Enter your first name: ";
    cin.getline(f_name, 20);
    cout << "Enter your last name: ";
    cin.getline(l_name, 20);
    cout << "Here's the information in a single string: " << l_name << ", " << f_name;
    return 0;
}

4.4

#include <iostream>
using namespace std;
int main(){
    string f_name, l_name;
    cout << "Enter your first name: ";
    getline(cin, f_name);
    cout << "Enter your last name: ";
    getline(cin, l_name);
    cout << "Here's the information in a single string: " << l_name << ", " << f_name;
    return 0;
}

4.5

#include <iostream>
using namespace std;
int main(){
    struct CandyBar{
        char brand[20];
        float weight;
        int calorie;
    };
    CandyBar snack = {
        "Mocha Munch",
        2.3,
        350
    };
    cout << snack.brand << endl;
    cout << snack.weight << endl;
    cout << snack.calorie << endl;
    return 0;
}

4.6

#include <iostream>
using namespace std;
int main(){
    struct CandyBar{
        char brand[20];
        float weight;
        int calorie;
    };
    CandyBar a[3] = {
        {"Mocha Munch",
        2.3,
        350},
        {
        "Mocha Munch2",
        2.4,
        450},
        {
        "Mocha Munch3",
        2.5,
        550}
    };

    cout << a[0].brand << "\t" << a[1].brand << "\t" << a[2].brand << endl;
    cout << a[0].weight << "\t" << a[1].weight << "\t" << a[2].weight << endl;
    cout << a[0].calorie << "\t" << a[1].calorie << "\t" << a[2].calorie << endl;
    return 0;
}

4.7

#include <iostream>
using namespace std;
int main(){
    struct Pizza{
        char name[20];
        float diam;
        float weight;
    };
    Pizza cur;
    cout << "Enter the company name: ";
    cin >> cur.name;
    cout << "Enter the diameter: ";
    cin >> cur.diam;
    cout << "Enter the weight: ";
    cin >> cur.weight;
    cout << "The company is: " << cur.name << endl;
    cout << "The diameter is: " << cur.diam << endl;
    cout << "The weight is: " << cur.weight << endl;
    return 0;
}

4.8

#include <iostream>
using namespace std;
int main(){
    struct Pizza{
        char name[20];
        float diam;
        float weight;
    };
    Pizza *p = new Pizza;
    cout << "Enter the diameter: ";
    cin >> p->diam;
    cout << "Enter the company name: ";
    cin >> p->name;
    cout << "Enter the weight: ";
    cin >> p->weight;
    cout << "The company is: " << p->name << endl;
    cout << "The diameter is: " << p->diam << endl;
    cout << "The weight is: " << p->weight << endl;
    delete p;
    return 0;

}

4.9

#include <iostream>
using namespace std;
int main(){
    struct CandyBar{
        char brand[20];
        float weight;
        int calorie;
    };
    CandyBar *p = new CandyBar[3]{
        {"Mocha Munch",
        2.3,
        350},
        {
        "Mocha Munch2",
        2.4,
        450},
        {
        "Mocha Munch3",
        2.5,
        550}
    };

    cout << p[0].brand << "\t" << p[1].brand << "\t" << p[2].brand << endl;
    cout << p[0].weight << "\t" << p[1].weight << "\t" << p[2].weight << endl;
    cout << p[0].calorie << "\t" << p[1].calorie << "\t" << p[2].calorie << endl;
    return 0;
}

4.10

#include <iostream>
#include <array>
using namespace std;
int main(){
    array <float, 3> arr;
    cout << "Please enter your first score: ";
    cin >> arr[0];
    cout << "Please enter your second score: ";
    cin >> arr[1];
    cout << "Please enter your third score: ";
    cin >> arr[2];
    cout << "Your test times are 3." << endl;
    cout << "Your average score is: " << (arr[0] + arr[1] + arr[2])/3 <<endl;
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值