【学习笔记】C++ Primer Plus 课后编程习题——第四章

记录一下自己看书做题的代码,也记录一下学习遇到的问题。代码都是自己写的,有错误请指出。

1.

// 4_13_1.cpp -- exercise 4.13.1
#include <iostream>
int main()
{
    using namespace std;
    cout << "What's your first name? ";
    char fstname[20];
    cin.getline(fstname, 20);
    cout << "What's your last name? ";
    string lstname;
    cin >> lstname;
    cout << "What letter grade do you deserve? ";
    char grade;
    cin >> grade;
    cout << "What's your age? ";
    int age;
    cin >> age;
    cout << "Name: " << lstname << "," << fstname << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age << endl;

    return 0;
}

2.

//4_13_2.cpp --4.13.2 exercise
#include<iostream>
int main()
{
    using namespace std;
    string name;
    string desert;

    cout << "Enter your name:\n";
    getline(cin, name);
    cout << "Enter your favorite dessert:\n";
    getline(cin, desert);
    cout << "I have some delicious " << desert;
    cout << " for you, " << name << endl;

    return 0;
}

以上两道题一个考点是ASCII码与对应的字符串的转换,还有就是cin.getline及getline函数的应用。


3.

// 4_13_3.cpp -- 4.13.3 exercise
#include<iostream>

int main()
{
    using namespace std;
    cout << "Enter your first name: ";
    char fstname[20];
    cin >> fstname;
    cout << "Enter your last name: ";
    char lstname[20];
    cin >> lstname;
    cout << "Here's the information in a single string: " << lstname << ", " << fstname;

    return 0;
}

这道题考了字符串的拼接。


4.

// 4_13_4.cpp -- exercise 4.13.4
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    cout << "Enter your first name: ";
    string fstname;
    getline(cin, fstname);
    cout << "Enter your last name: ";
    string  lstname;
    getline(cin, lstname);
    cout << "Here's the information in a single string: " << lstname + ", " + fstname << endl;

    return 0;
}

考察了string类型的拼接,相比于char来说方便很多。


5.

// 4_13_5.cpp -- exercise 4.13.5
#include <iostream>

int main()
{
    using namespace std;

    struct CandyBar
    {
        string name;
        float weight;
        int Calorie;
    };

    CandyBar snacks =
    {
        "Mocha Munch",
        2.3,
        350
    };

    cout << "The name of this snack is " << snacks.name << "." << endl;
    cout << "The weight of it is " << snacks.weight << endl;
    cout << "The Calorie of it is " << snacks.Calorie << endl;

    return 0;
}

考察了如何创建struct。


6.

// 4_13_6.cpp -- exercise 4.13.6
#include <iostream>

int main()
{
    using namespace std;

    struct CandyBar
    {
        string name;
        float weights;
        int calorie;
    };

    CandyBar snacks[3] =
    {
        {"Barone", 2.0, 350},
        {"Bartwo", 2.1, 360},
        {"Barthree", 2.2, 370}
    };

    cout << "Names of snacks are:" << snacks[0].name << " " << snacks[1].name << " "
         << snacks[2].name;
}

考点:创建结构数组。


7.

// 4_13_7.cpp -- exercise 4.13.7
#include <iostream>

int main()
{
    using namespace std;

    struct Pizza_info
    {
        string name;
        int diameter;
        float weights;
    };

    Pizza_info cus_pizza;
    cout << "Please tell me the brand of your pizza: ";
    getline(cin, cus_pizza.name);
    cout << "Please chose the diameter of your pizza: ";
    cin >> cus_pizza.diameter;
    cout << "Please chose the weight of your pizza: ";
    cin >> cus_pizza.weights;
    cout << "Okay,your pizza is " << cus_pizza.name
         << " and the diameter of it is " << cus_pizza.diameter
         << ".The pizza's weight is " << cus_pizza.weights << "." << endl;

    return 0;
}

考点,创建一个结构变量,然后再通过访问结构的成员来赋值。


8.

// 4_13_8.cpp -- exercise 4.13.8
#include <iostream>
#include <string>

struct pizza_info
{
    std::string name;
    int diameter;
    float weights;
};


int main()
{
    using namespace std;

    pizza_info *ps = new pizza_info;

    cout << "How big of the pizza do you want?" << endl;
    cin >> (*ps).diameter;
    cin.get(); // very important! C++ Primer Plus page 81!!!
    cout << "Okay, which brand are you prefer?"<< endl;
    getline(cin, ps->name);
    cout << "And tell me the weight of the pizza." << endl;
    cin >> ps->weights;
    cout << "Your pizza is " << (*ps).name << " and the diameter of it is "
         << ps->diameter << " and it weights " << ps->weights << endl;

    return 0;
}

这道题之所以标红,是因为我在先输入披萨的尺寸后,没等我输入名称就直接跳到输入重量那里了。最后认真读书之后发现再C++PP第六版81页有写,cin在读取用户输入的数字时,将回车生成的换行符留在输入队列中。因此加入一句cin.get(),跳过这个换行符就好了。


9.

//4_13_9.cpp -- exercise 4.13.9
#include<iostream>

struct CandyBar
{
    std::string name;
    float weights;
    int calorie;
};

int main()
{
    using namespace std;

    CandyBar *ps = new CandyBar[3]
    {
        {"One", 3.5, 20},
        {"Two", 3.6, 21},
        {"Three", 3.7, 22},
    };

    cout << "Names of snacks are:" << ps[0].name << " " << ps[1].name << " "
         << ps[2].name << endl;
    cout << "Weights of snacks are:" << ps[0].weights << " " << ps[1].weights << " "
         << ps[2].weights << endl;
    cout << "Calorie of snacks are:" << ps[0].calorie << " " << ps[1].calorie << " "
         << ps[2].calorie << endl;
}

考察用new来动态分配结构数组。(自己做的很不熟练)


10.

// 4_13_10.cpp -- exercise 4.13.10

#include <iostream>
#include <array>

int main()
{
    using namespace std;

    array<double, 3>  run_grade;
    cout << "Please enter your 1st time of 100m running:" << endl;
    double fst_time;
    cin >> fst_time;
    run_grade[0] = fst_time;
    cout << "Please enter your 2ed time of 100m running:" << endl;
    double snd_time;
    cin >> snd_time;
    run_grade[1] = snd_time;
    cout << "Please enter your 3rd time of 100m running:" << endl;
    double trd_time;
    cin >> trd_time;
    run_grade[2] = trd_time;

    double avg_time;
    avg_time = (fst_time + snd_time + trd_time) / 3;
    cout << "After 3 times running,your average time is:" << avg_time << endl;

    return 0;
}

考察array的创建,赋值以及访问。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章