C++ Primer Plus(第六版)中文版课后答案

第四章编程练习(6-10)

4.13.6

#include<iostream>
using namespace std;
struct CandyBar {
    char flavor[20];
    float price;
    double weight;
};
int main() {
    CandyBar candy{
        "Sweet",
        5.21,
        500
    };
    cout << candy.flavor << endl
        << candy.price << "元" << endl
        << candy.weight << " g." << endl;
    return 0;
}

4.13.7

#include<iostream>
#include<string>
using namespace std;
struct william {
    string name;
    float calibre;
    double weight;
};
int main() {
    william wingate{};
    cout << "Enter the name of the pizza : ";
    getline(cin,wingate.name);
    cout << "Enter the calibre of the pizza : ";
    cin >> wingate.calibre;
    cout << "Enter the weight of the pizza : ";
    cin >> wingate.weight;
    cout << "The pizza's name is " << wingate.name << endl;
    cout << "The pizza's calibre is " << wingate.calibre << endl;
    cout << "The pizza's weight is " << wingate.weight << endl;
    return 0;
}

4.13.8

#include<iostream>
#include<string>
using namespace std;
struct william {
    string name;
    float calibre;
    double weight;
};
int main() {
    william* pizza = new william;
    cout << "Enter the calibre of the pizza : ";
    (cin >> pizza->calibre).get();   //.get(),以获取并返回输入流中的下一个字符
    cout << "Enter the name of the pizza : ";
    getline(cin, pizza->name);
    cout << "Enter the weight of the pizza : ";
    cin >> pizza->weight;
    cout << "The pizza's name is " << pizza->name << endl;
    cout << "The pizza's calibre is " << pizza->calibre << endl;
    cout << "The pizza's weight is " << pizza->weight << endl;
    delete pizza;
    return 0;
}

4.13.9


//4.13.9
#include<iostream>
#include<string>
using namespace std;
struct CandyBar {
    string name;
    float price;
    double weight;
};
int main() {
    CandyBar* candy = new CandyBar;
    cout << "Enter the name of candybar : ";
    getline(cin, candy->name);
    cout << "Enter the price of candybar : ";
    cin>>candy->price;
    cout << "Enter the weight of candybar : ";
    cin >> candy->weight;
    cout << "The name of candybar is : " << candy->name << endl;
    cout << "The price of candybar is : " << candy->price << endl;
    cout << "The weight of candybar is : " << candy->weight << endl;
    delete candy;
    return 0;
}
 

4.13.10

#include<iostream>
#include<string>
#include<array>
using namespace std;
int main() {
    array<double,3>scores;
    cout << "Enter your first scores of 40m: ";
    cin >> scores[0];
    cout << "Enter your second scores of 40m: ";
    cin >> scores[1];
    cout << "Enter your third scores of 40m: ";
    cin >> scores[2];
    cout << "You's the first score is :" << scores[0] << endl;
    cout << "You's the second score is :" << scores[1] << endl;
    cout << "You's the third score is :" << scores[2] << endl;
    cout << "You's the average score is :" << (scores[0] + scores[1] + scores[2]) / 3 << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值