(三)《C++ Primer Plus》 | 第 4 章:复合类型

(1)编写 C++ 程序,完成如下输入请求和输出。

  • cin 以空格或回车停止读取,所以在遇到带有空格的字符串输入时一般使用 get 或 getline 函数。
  • cin.getline(str, n) 读取一行字符,以回车结束并丢弃换行符;cin.get(str, n) 读取一行字符,以回车结束并保留换行符。因此,如果使用后者连续读取输入,则中途需使用 cin.get() 来清除上一次输入的换行符。
#include <iostream>

using namespace std;

int main()
{
    // 存放名,长度为20字节
    cout << "What is your first name? ";
    char fname[20];
    cin.get(fname, 20);

	// 接收回车符
    cin.get();

    // 存放姓,长度为10字节
    cout << "What is your second name? ";
    char sname[10];
    cin.get(sname, 10);

    // 存放grade,长度为1字节
    cout << "What letter grade do you deserve? ";
    char ch;
    cin >> ch;

    // 存放age,长度为4字节
    cout << "What is your age? ";
    int age;
    cin >> age;

    // 输出个人信息
    cout << "Name: " << sname << ", " << fname << endl;
    cout << "Grade: " << char(ch + 1) << endl;
    cout << "Age: " << age << endl;
    return 0;
}

(2)使用 string 类替换上题的字符数组实现同样的功能。

#include <iostream>

using namespace std;

int main()
{
    // 存放名
    cout << "What is your first name? ";
    string fname;
    getline(cin, fname);

    // 存放姓
    cout << "What is your second name? ";
    string sname;
    getline(cin, sname);

    // 存放grade,长度为1字节
    cout << "What letter grade do you deserve? ";
    char ch;
    cin >> ch;

    // 存放age,长度为4字节
    cout << "What is your age? ";
    int age;
    cin >> age;

    // 输出个人信息
    cout << "Name: " << sname << ", " << fname << endl;
    cout << "Grade: " << char(ch + 1) << endl;
    cout << "Age: " << age << endl;
    return 0;
}

(3)编写 C++ 程序,要求用户首先输入其名,再输入其姓,然后显示。

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    // 存放名,长度为 20 字节
    cout << "What is your first name? ";
    char fname[20];
    cin.get(fname, 20);

    cin.get();

    // 存放姓,长度为 10 字节
    cout << "What is your second name? ";
    char sname[10];
    cin.get(sname, 10);

    // 存放姓名
    char name[40];

    // 一次复制两次拼接
    strcpy(name, sname);
    strcat(name, ", ");
    strcat(name, fname);

    cout << "Here's the information in a single string: " << name << endl;
    return 0;
}

(4)使用 string 类替换上题的字符数组实现同样的功能。

#include <iostream>

using namespace std;

int main()
{
    // 存放名
    cout << "What is your first name? ";
    string fname;
    getline(cin, fname);

    // 存放姓
    cout << "What is your second name? ";
    string sname;
    getline(cin, sname);

    // 拼接姓名
    string name = sname + ", " + fname;

    cout << "Here's the information in a single string: " << name << endl;
    return 0;
}

(5)结构体 CandyBar 包含 3 个成员。第一个成员存储了糖块的品牌,第二个陈冠存储糖块的重量(小数),第三个糖块存储糖块的卡路里含量(整数)。编写 C++ 程序,将其成员分别初始化为 “Mocha Munch”、2.3 和 350,然后显示。

#include <iostream>

using namespace std;

struct CandyBar
{
    string brand;
    float weight;
    int calorie;
};

int main()
{
    // 创建 CandyBar 变量并初始化
    CandyBar snack = { "Mocha Munch", 2.3, 350 };

    // 显示
    cout << "The brand of snack: " << snack.brand << endl;
    cout << "The weight of snack: " << snack.weight << endl;
    cout << "The calorie of snack: " << snack.calorie << endl;
    return 0;
}

(6)根据上题,创建 3 个 CandyBar 变量,其初始值根据用户输入而定,然后显示。

#include <iostream>

using namespace std;

struct CandyBar
{
    string brand;
    float weight;
    int calorie;
};

int main()
{
    // 创建三个 CandyBar 变量
    CandyBar snacks[3];

    // 输入
    cout << "Please input the information of first snack: " << endl;
    cout << "brand: ";
    cin >> snacks[0].brand;
    cout << "weight: ";
    cin >> snacks[0].weight;
    cout << "calorie: ";
    cin >> snacks[0].calorie;
    cout << "Please input the information of second snack: " << endl;
    cout << "brand: ";
    cin >> snacks[1].brand;
    cout << "weight: ";
    cin >> snacks[1].weight;
    cout << "calorie: ";
    cin >> snacks[1].calorie;
    cout << "Please input the information of third snack: " << endl;
    cout << "brand: ";
    cin >> snacks[2].brand;
    cout << "weight: ";
    cin >> snacks[2].weight;
    cout << "calorie: ";
    cin >> snacks[2].calorie;

    // 显示
    cout << "The first candy: " << snacks[0].brand << " " 
        << snacks[0].weight << " " << snacks[0].calorie << endl;
    cout << "The second candy: " << snacks[1].brand << " " 
        << snacks[1].weight << " " << snacks[1].calorie << endl;
    cout << "The third candy: " << snacks[2].brand << " " 
        << snacks[2].weight << " " << snacks[2].calorie << endl;
    return 0;
}

(7)设计结构体,包含披萨公司名称、披萨饼的直径和披萨饼的重量三种成员。然后要求用户输入变量信息,并输出。

#include <iostream>

using namespace std;

struct Pizza
{
    string company;
    float diameter;
    float weight;
};

int main()
{
    // 初始化变量并输入
    Pizza p;
    cout << "Please input the information of pizza: " << endl;
    cout << "company: ";
    cin >> p.company;
    cout << "diameter: ";
    cin >> p.diameter;
    cout << "weight: ";
    cin >> p.weight;

    // 显示
    cout << "The information of the pizza: " << p.company << " " 
        << p.diameter << " " << p.weight << endl;
    return 0;
}

(8)根据上题,使用 new 为结构体分配内存,且在输入披萨饼公司名称之前输入披萨饼的直径。

#include <iostream>

using namespace std;

struct Pizza
{
    string company;
    float diameter;
    float weight;
};

int main()
{
    Pizza* p = new Pizza;
    cout << "Please input the information of pizza: " << endl;
    cout << "diameter: ";
    cin >> p->diameter;
    cout << "company: ";
    cin >> p->company;
    cout << "weight: ";
    cin >> p->weight;

    // 显示
    cout << "The information of the pizza: " << p->company << " " 
        << p->diameter << " " << p->weight << endl;

    // 释放内存
    delete p;
    return 0;
}

(9)根据第 6 题,使用 new 关键字动态分配内存。

#include <iostream>

using namespace std;

struct CandyBar
{
    string brand;
    float weight;
    int calorie;
};

int main()
{
    // 创建三个 CandyBar 变量
    CandyBar* snacks = new CandyBar[3];

    // 输入
    cout << "Please input the information of first snack: " << endl;
    cout << "brand: ";
    cin >> snacks[0].brand;
    cout << "weight: ";
    cin >> snacks[0].weight;
    cout << "calorie: ";
    cin >> snacks[0].calorie;
    cout << "Please input the information of second snack: " << endl;
    cout << "brand: ";
    cin >> snacks[1].brand;
    cout << "weight: ";
    cin >> snacks[1].weight;
    cout << "calorie: ";
    cin >> snacks[1].calorie;
    cout << "Please input the information of third snack: " << endl;
    cout << "brand: ";
    cin >> snacks[2].brand;
    cout << "weight: ";
    cin >> snacks[2].weight;
    cout << "calorie: ";
    cin >> snacks[2].calorie;

    // 显示
    cout << "The first candy: " << snacks[0].brand << " " 
        << snacks[0].weight << " " << snacks[0].calorie << endl;
    cout << "The second candy: " << snacks[1].brand << " " 
        << snacks[1].weight << " " << snacks[1].calorie << endl;
    cout << "The third candy: " << snacks[2].brand << " " 
        << snacks[2].weight << " " << snacks[2].calorie << endl;

    // 释放内存
    delete []snacks;
    return 0;
}

(10)编写 C++ 程序,要求用户输入三次 40 码跑的成绩,并显示次数和平均成绩。

  • array 的使用格式为:array<typename, n_elem> arr
#include <iostream>
#include <array>

using namespace std;

int main()
{
    // 使用 array 存储成绩
    array<float, 3> arr;
    cout << "Please input three scores of 40-yard runs: ";
    cin >> arr[0] >> arr[1] >> arr[2];

    // 显示
    cout << "The average score for " << arr.size() 
        << " 40-yard runs is " << (arr[0] + arr[1] + arr[2]) / 3.0 << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值