C++ Primer Plus 第4章 课后编程练习 代码

1#include <iostream>
#include <string>
using namespace std;

struct student
{
    char firstname[40];
    char lastname[40];
    int age;
    char grade;
};

int main()
{
    student std1;
    cout << "What your first name?";
    cin.getline(std1.firstname, 40);
    cout << "What your last name?";
    cin.get(std1.lastname, 40);
    cin.get();

    cout << "What letter grade do you deserve?";
    cin >> std1.grade;
    cout << "What is your age?";
    cin >> std1.age;

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

    return 0;
}
2#include <iostream>
#include <string>
int main()
{
    using namespace std;

    string name;
    string dessert;

    cout << "Enter your name :\n";
    getline(cin, name); //cin 确定结尾的方式类似scanf(以空白作为读取结结束)因此cin只能一次性读取一个单词
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);

    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";

    return 0;
}
3#include <cstring>
#include <iostream>

int main()
{
    using namespace std;
    char firstname[50];
    char lastname[25];
    char sign[] = ", ";
    cout << "Enter your first name:";
    cin >> firstname;
    cout << "Enter your last name:";
    cin >> lastname;
    strcat(firstname, sign);
    strcat(firstname, lastname);

    cout << "Here's the information in a single string: "
         << firstname << endl;

    return 0;
}
4#include <cstring>
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    string firname;
    string lstname;
    string sign = ", ";
    cout << "Enter your first name:";
    cin >> firname;
    cout << "Enter your last name:";
    cin >> lstname;

    firname = firname + sign + lstname;

    cout << "Here's the information in a single string: "
         << firname << endl;

    return 0;
}
5#include <iostream>
// #include <>

struct CandyBar
{
    char brand[30];
    double weight;
    int col;
};

int main()
{
    using namespace std;
    CandyBar snack = {"Mocha Munch", 2.3, 350};

    cout << "Brand: " << snack.brand << endl;
    cout << "Weight: " << snack.weight << endl;
    cout << "Calorie: " << snack.col << endl;

    return 0;
}
6#include <iostream>

struct CandyBar
{
    char brand[30];
    double weight;
    int col;
};

int main()
{
    using namespace std;
    CandyBar snack[3] = {
        {"Mocha Munch", 2.3, 350},
        {"Losh Jdygla", 3.4, 780},
        {"Lohidy Yohigh", 1.9, 600},
    };

    cout << "Brand: " << snack[0].brand << endl;
    cout << "Weight: " << snack[0].weight << endl;
    cout << "Calorie: " << snack[0].col << endl;

    cout << "Brand: " << snack[1].brand << endl;
    cout << "Weight: " << snack[1].weight << endl;
    cout << "Calorie: " << snack[1].col << endl;

    cout << "Brand: " << snack[2].brand << endl;
    cout << "Weight: " << snack[2].weight << endl;
    cout << "Calorie: " << snack[2].col << endl;

    return 0;
}
7#include <cstring>
#include <iostream>
#include <string>
using namespace std;

struct pizainfo
{
    char cmpy[20];
    int diameter;
    double weight;
};

int main()
{
    pizainfo pizzalib;
    cout << "Please input the pizza's company:";
    cin.getline(pizzalib.cmpy, 20);
    cout << "Please input the pizza's diameter:";
    cin >> pizzalib.diameter;
    cout << "Please input the pizza's weight:";
    cin >> pizzalib.weight;

    cout << "Company: " << pizzalib.cmpy << endl
         << "Diameter: " << pizzalib.diameter << endl
         << "Weight: " << pizzalib.weight << endl;

    // getline(cin, pizzalib[0].cmpy);

    // while (count < MAXKIND &&)
    // {
    //     /* code */
    // }

    return 0;
}
8#include <iostream>
#include <string>
using namespace std;

struct pizainfo
{
    char cmpy[20];
    int diameter;
    double weight;
};

int main()
{
    pizainfo *pizzalib = new pizainfo;

    cout << "Please input the pizza's diameter:";
    cin >> pizzalib->diameter;
    cout << "Please input the pizza's weight:";
    cin >> pizzalib->weight;
    cin.get(); //!注意这里,不可缺省
    cout << "Please input the pizza's company:";
    cin.getline(pizzalib->cmpy, 20);

    cout << "Company: " << pizzalib->cmpy << endl
         << "Diameter: " << pizzalib->diameter << endl
         << "Weight: " << pizzalib->weight << endl;

    return 0;
}
9#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int col;
};

int main()
{

    CandyBar *pCB = new CandyBar[3];

    pCB[0] = {"Mocha Munch", 2.3, 350};
    pCB[1] = {"Losh Jdygla", 3.4, 780};
    pCB[2] = {"Lohidy Yohigh", 1.9, 600};

    cout << "Brand: " << pCB[0].brand << endl;
    cout << "Weight: " << pCB[0].weight << endl;
    cout << "Calorie: " << pCB[0].col << endl;

    cout << "Brand: " << pCB[1].brand << endl;
    cout << "Weight: " << pCB[1].weight << endl;
    cout << "Calorie: " << pCB[1].col << endl;

    cout << "Brand: " << pCB[2].brand << endl;
    cout << "Weight: " << pCB[2].weight << endl;
    cout << "Calorie: " << pCB[2].col << endl;

    return 0;
}
10#include <array>
#include <iostream>

int main()
{
    using namespace std;
    int count = 0;
    cout << "Please input the first time grade(unit in sec): ";
    array<double, 3> grade;
    cin >> grade[0];
    count++;
    cout << "Please input the second time grade(unit in sec): ";
    cin >> grade[1];
    count++;
    cout << "Please input the third time grade(unit in sec): ";
    cin >> grade[2];
    count++;

    cout << "Times: " << count << endl;
    cout << "Average: " << (grade[0] + grade[1] + grade[2]) / 3 << endl;
    return 0;
}
10(拓展一下)
#include <iostream>
#include <vector>

int main()
{
    using namespace std;
    int count = 0;
    int gra;
    cout << "How many times grade input?" << endl;
    cin >> gra;
    cout << "Please input the first time grade(unit in sec): " << endl;
    vector<double> grade(gra);

    do
    {
        int temp;
        cin >> temp;
        grade[count] = temp;
        count++;

        if (count < gra)
        {
            cout << "Please input the next grade: " << endl;
        }
    } while (count < gra);

    cout << "Times: " << count << endl;

    double total = 0;
    for (int i = 0; i < count; i++)
    {
        total += grade[i];
    }

    cout << "Average: " << total / count << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

咖啡与乌龙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值