C++ primer plus 课后练习题

第四章:复合类型

        一、复习题

        1. a. char actor[30];

            b. short betsie[100];

            c. float chuck[13];

            d. long double dipsea[64];

        2. a. array<char,30> actor;

            b. array<short,100> betsie;

            c. array<float,13> chuck;

            d. array<long double,64> dipsea;

        3. int odd[5] = {1,3,5,7,9};

        4. int even = odd[0] + odd[4];

        5. cout << ideas[1] << endl;

        6. char str[13] = "cheeseburger";

        7. std::string str = "Waldorf Salad";

        8. struct fish {

                char breed[20];

                int weight;

                double length;

             };

        9. struct fish goldfish = { "goldfish" , 10 , 5 };

        10.enum Response { No , Yes , Maybe };(不打等号)

        11.double* p = &ted;

            cout << (*p) << endl;

        12.doule* p = treacle;

             cout << (*p) << *(p+9) << endl;

        13.cout << "Enter a num plz : " << endl;

             int len = 0;

             cin >> len;

             int* p = new int[len];

             vector<int> v(len);

             delete [] p;

        14.他会输出该字符串首字母“H”的地址

        15.struct fish {

                char breed[20];

                int weight;

                double length;

             };

             fish* p = new fish;

             cout << "Enter kind of fish: ";

             cin >> p->breed;

        16.使用cin >> address 将使得程序跳过空白,直到找到非空白字符为止。因此,他将跳过数                                                    字输入后的换行符,从而避免这种问题。

        17.#include <string>

            #include <vector>

            #include <array>

            const int len = 10;

            std::vector<std::string> v(len);

            std::array<std::string,len> arr;

        二、编程练习

        1.

#include <iostream>
using namespace std;
int main() {
    cout << "What's your first name? " << endl;
    char f_str[15];
    cin.getline(f_str,15);

    cout << "What's your last name? " << endl;
    char l_str[15];
    cin.getline(l_str,15);

    cout << "What letter grade do you deserve? " << endl;
    char grade;
    cin >> grade;

    cout << "What's your age? " << endl;
    int age;
    cin >> age;

    cout << "Name: " << l_str << ", " << f_str << endl;
    cout << "Grade: " << (char)(grade+1) << endl;
    cout << "Age: " << age << endl;
}

        2.

#include <iostream>

using namespace std;

int main() {
    string name;
    string dessert;
    cout << "Enter your name: " << endl;
    getline(cin,name);
    cout << "Enter your favorite dessert: " << endl;
    getline(cin,dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
}

        3.

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    cout << "What's your first name? " << endl;
    char f_str[15];
    cin.getline(f_str, 15);

    cout << "What's your last name? " << endl;
    char l_str[15];
    cin.getline(l_str, 15);

    char *newStr = ::strcat(l_str, ", ");
    ::strcat(newStr, f_str);
    cout << newStr << endl;
}

        4.

#include <iostream>
#include <string>

using namespace std;

int main() {
    cout << "What's your first name? " << endl;
    string f_str;
    cin >> f_str;

    cout << "What's your last name? " << endl;
    string l_str;
    cin >> l_str;

    string newStr = l_str+", "+f_str;
    cout << newStr << endl;
}

        5.

#include <iostream>
using namespace std;
struct CandyBar{
    string brand;
    double weight;
    int calorie;
};
int main(){
    CandyBar snack = {"Mocha Munch",2.3,350};
    cout << snack.brand << " " << snack.weight << " " << snack.calorie << endl;
}

        6.

#include <iostream>

using namespace std;
struct CandyBar {
    string brand;
    double weight;
    int calorie;
};

int main() {
    CandyBar snack1 = {"Mocha Munch1", 2.3, 350};
    CandyBar snack2 = {"Mocha Munch2", 2.4, 351};
    CandyBar snack3 = {"Mocha Munch3", 2.5, 352};
    CandyBar snacks[3] = {snack1, snack2, snack3};
    for (int i = 0; i < 3; i++) {
        cout << snacks[i].brand << " " << snacks[i].weight << " " << snacks[i].calorie << endl;
    }
}

        7.

#include <iostream>

using namespace std;
struct Pizza {
    string companyName;
    double diam = 0;
    double weight = 0;
};

int main() {
    Pizza pizza;
    cout << "Enter the name of the company plz:" << endl;
    cin >> pizza.companyName;
    cout << "Enter the diam of the pizza: " << endl;
    cin >> pizza.diam;
    cout << "Enter the weight of the pizza: " << endl;
    cin >> pizza.weight;
    cout << pizza.companyName << " " << pizza.diam << " " << pizza.weight << endl;
}

        8.

#include <iostream>

using namespace std;
struct Pizza {
    string companyName;
    double diam = 0;
    double weight = 0;
};

int main() {
    Pizza *ppizza = new Pizza;
    cout << "Enter the name of the company plz:" << endl;
    cin >> ppizza->companyName;
    cout << "Enter the diam of the pizza: " << endl;
    cin >> ppizza->diam;
    cout << "Enter the weight of the pizza: " << endl;
    cin >> ppizza->weight;
    cout << ppizza->companyName << " " << ppizza->diam << " " << ppizza->weight << endl;
    delete ppizza;
}

        9.

#include <iostream>

using namespace std;
struct CandyBar {
    string brand;
    double weight;
    int calorie;
};

int main() {
    CandyBar snack1 = {"Mocha Munch1", 2.3, 350};
    CandyBar snack2 = {"Mocha Munch2", 2.4, 351};
    CandyBar snack3 = {"Mocha Munch3", 2.5, 352};
    CandyBar(*snacks) = new CandyBar[3];
    snacks[0] = snack1;
    snacks[1] = snack2;
    snacks[2] = snack3;
    for (int i = 0; i < 3; i++) {
        cout << snacks[i].brand << " " << snacks[i].weight << " " << snacks[i].calorie << endl;
    }
}

        10.

#include <iostream>
#include <array>

using namespace std;

int main() {
    array<int, 3> score{};
    int sum = 0;
    for (int i = 0; i < 3; i++) {
        cout << "Enter the score plz: " << endl;
        cin >> score[i];
        sum += score[i];
    }
    cout << "the avg is " << sum / score.size() << endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值