C++ Primer Plus 第6版 中文版 第4章编程练习

该博客围绕《C++ Primer Plus 第6版 中文版》第4章练习展开,虽未详细展示练习内容,但给出了部分序号,如2、8、10 ,体现了对C++相关知识的练习巩固。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、
#include <iostream>

using namespace std;

int main()
{
    char firstName[30];
    char lastName[30];
    char grade;
    int age;

    cout << "What is your first name? ";
    cin.getline(firstName, 30);
    cout << "What is your last name? ";
    cin.getline(lastName, 30);
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;


    cout << "Name: " << lastName << ", " << firstName << endl;
    cout << "Grade: " << static_cast<char> (grade+1) << endl;
    cout << "Age: " << age << endl;

    return 0;
}


2、

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name, 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";

    return 0;
}

3、

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char firstName[30], lastName[30], result[70];

    cout << "Enter your first name: ";
    cin.getline(firstName, 30);
    cout << "Enter your last name: ";
    cin.getline(lastName, 30);

    strcpy(result, lastName);
    strcat(result, ", ");
    strcat(result, firstName);

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

    return 0;
}

4、

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string firstName, lastName, result;

    cout << "Enter your first name: ";
    getline(cin, firstName);
    cout << "Enter your last name: ";
    getline(cin, lastName);

    result = lastName + ", " + firstName;

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

    return 0;
}

5、

#include <iostream>

using namespace std;

struct CandyBar
{
    char name[20];
    double weight;
    int calorie;
};

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

    cout << "CandyBar name: " << snack.name << " weight: " << snack.weight << " calorie: " << snack.calorie << endl;

    return 0;
}

6、

#include <iostream>

using namespace std;

struct CandyBar
{
    char name[20];
    double weight;
    int calorie;
};

int main()
{
    CandyBar snack[3] = {{"Mocha Munch", 2.3, 350}, {"two", 2.4, 44}, {"three", 3.4, 55}};

    for(int i=0; i<3; i++)
    {
        cout << "CandyBar name: " << snack[i].name << " weight: " << snack[i].weight << " calorie: " << snack[i].calorie << endl;
    }

    return 0;
}

7、

#include <iostream>
#include <string>
 
using namespace std;
 
struct Pizza
{
    string coName;
    double diameter;
    double weight;
};
 
int main()
{
    Pizza pizza;
 
    cout << "Enter pizza infomation: " << endl;
    cout << "  coName: ";
    cin >> pizza.coName;
    cout << "diameter: ";
    cin >> pizza.diameter;
    cout << "  weight: ";
    cin >> pizza.weight;
 
    cout << "pizza is: " << pizza.coName << " " << pizza.diameter << " " << pizza.weight << endl;
 
    return 0;
}
 
 

8、

#include <iostream>
#include <string>

using namespace std;

struct Pizza
{
    string coName;
    double diameter;
    double weight;
};

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

    cout << "Enter pizza infomation: " << endl;
    cout << "diameter: ";
    cin >> pizza->diameter;
    cout << "  coName: ";
    cin >> pizza->coName;
    cout << "  weight: ";
    cin >> pizza->weight;

    cout << "pizza is: " << pizza->coName << " " << pizza->diameter << " " << pizza->weight << endl;

    return 0;
}


9、

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

struct CandyBar
{
    string name;
    double weight;
    int calorie;
};

int main()
{
    CandyBar *p = new CandyBar[3];

    for(int i=0; i<3; i++)
    {
        p[i].name = "name";
        p[i].weight = i;
        p[i].calorie = i;

        cout << "CandyBar name: " << p[i].name << " weight: " << p[i].weight << " calorie: " << p[i].calorie << endl;
    }

    return 0;
}
 

10、

#include <iostream>

using namespace std;

int main()
{
    double arr[3];
    double sum;

    for(int i=0; i<3; i++)
    {
        cout << "The " << i+1 << " time is: ";
        cin >> arr[i];
        sum += arr[i];
    }

    cout << "average: " << sum/3 << endl;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值