c++Primer Plus 第四章课后习题

1

#include <iostream>

using namespace std;

int main()
{
    string firName;
    string secNmae;
    char grade;
    int age;
    cout<<"What is your first name ?";
    getline(cin,firName);
    cout<<"What is your last name ? ";
    getline(cin,secNmae);
    cout<<"What letter grade do you deserve ? ";
    cin>>grade;
    if(grade > 'C'){
        grade='C';
    }
    cout<<"What is your age ? ";
    cin>>age;
    cout<<"Name:"<<secNmae<<", "<<firName<<endl;
    cout<<"Grade:"<<grade<<endl;
    cout<<"Age:"<<age;
    cout << "Hello World!" << endl;
    return 0;
}

2

#include <iostream>

using namespace std;

int main()
{
    string name;
    string 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";
    cout << "Hello World!" << endl;
    return 0;
}

3

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

int main()
{
    char firName[20];
    char lastName[20];
    cout<<"Enter your first name: ";
    cin.getline(firName,20);
    cout<<"Enter your last name: ";
    cin.getline(lastName,20);
    strcat(lastName,", ");
    strcat(lastName,firName);
    cout<<"Here's a information in a single string : "<<lastName<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

4

#include <iostream>

using namespace std;

struct CandyBar{
    string brand; //品牌
    double weight; //重量
    int calorie; //卡路里
};
int main()
{
    struct CandyBar snack={"Mocha Munch",2.3,350}; //创建即赋值
//    snack.brand="Mocha Munch";
//    snack.weight=2.3;
//    snack.calorie=350;
    cout<<"brand : "<<snack.brand<<endl;
    cout<<":weight : "<<snack.weight<<endl;
    cout<<"calorie : "<<snack.calorie<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

5

#include <iostream>

using namespace std;


struct CandyBar{
    string brand; //品牌
    double weight; //重量
    int calorie; //卡路里

    void print(){
        cout<<"brand : "<<brand<<endl;
        cout<<":weight : "<<weight<<endl;
        cout<<"calorie : "<<calorie<<endl;
        cout << "Hello World!" << endl;
    }
};
int main()
{
    struct CandyBar snacks[3]={{"Mocha Munch",2.3,350},{"little white rabbit",2.6,263},{"alps",2.9,420}};
    snacks[0].print();
    cout<<"\n";
    snacks[1].print();
    cout<<"\n";
    snacks[2].print();
    cout << "Hello World!" << endl;
    return 0;
}

6

#include <iostream>

using namespace std;
struct Pizza{
    string nameOfcompany;
    double diameter;
    double weight;

    void print(){
        cout<<"披萨饼公司名:"<<nameOfcompany<<endl;
        cout<<"披萨饼直径:"<<diameter<<endl;
        cout<<"披萨饼重量:"<<weight<<endl;
    }

};
int main()
{
    struct Pizza pizza;
    cout<<"请输入披萨饼公司的名称:";
    getline(cin,pizza.nameOfcompany);
    cout<<"披萨饼直径:";
    cin>>pizza.diameter;
    cout<<"披萨饼重量:";
    cin>>pizza.weight;
    pizza.print();
    cout << "Hello World!" << endl;
    return 0;
}

7


#include <iostream>

using namespace std;

struct Pizza{
    string nameOfcompany;
    double diameter;
    double weight;

    void print(){
        cout<<"披萨饼公司名:"<<nameOfcompany<<endl;
        cout<<"披萨饼直径:"<<diameter<<endl;
        cout<<"披萨饼重量:"<<weight<<endl;
    }

};

int main()
{
    struct Pizza *pizza=new struct Pizza;

    cout<<"披萨饼直径:";
    cin>>pizza->diameter;
    cin.get();

    cout<<"请输入披萨饼公司的名称:";
    getline(cin,pizza->nameOfcompany);

    cout<<"披萨饼重量:";
    cin>>pizza->weight;
    pizza->print();
    cout << "Hello World!" << endl;

    delete pizza;
    return 0;
}

 8

#include <iostream>

using namespace std;


struct CandyBar{
    string brand="brand"; //品牌
    double weight=0.0; //重量
    int calorie=0; //卡路里

    void print(){
        cout<<"brand : "<<brand<<endl;
        cout<<":weight : "<<weight<<endl;
        cout<<"calorie : "<<calorie<<endl;
        cout << "Hello World!" << endl;
    }
};
int main()
{
    struct CandyBar *snacks=new struct CandyBar[3];

    snacks->print();
    cout<<"\n";
    (snacks+1)->print();
    cout<<"\n";
    (snacks+2)->print();
    cout << "Hello World!" << endl;
    return 0;
}

9

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

int main()
{
    array<double,3> my_array;
    cout<<"请输入第一次40m跑的时间:";
    cin>>my_array[0];
    cout<<"请输入第二次40m跑的时间:";
    cin>>my_array[1];
    cout<<"请输入第三次40m跑的时间:";
    cin>>my_array[2];
    cout<<"次数:"<<3<<endl;
    cout<<"平均成绩:"<<(my_array[0]+my_array[1]+my_array[2])/3<<endl;
    cout << "Hello World!" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惜日短

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值