C++ Primer Plus 第四章-复合类型

1. 编写一个C++程序,如下述输出示例所示的那样请求并显示信息:
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A 、B 或C, 所以不必担心D 和F 之间的空档。

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

const int Size = 40;

int main() {
    char firstname[Size];
    char lastname[Size];
    char grade;
    int age;
    cout << "What is your first name? ";
    cin.getline(firstname, Size);
    cout << "What is your last name? ";
    cin.getline(lastname, Size);
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;
    grade = grade + 1;
    cout << "Name: " << lastname << "," << firstname << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << age << endl;
}

2. 修改程序清单4.4, 使用C++ string 类而不是char 数组。

//程序清单4.4 instr2.cpp -- reading more than one word with getline
#include <iostream>
int main()
{
    using namespace std;
    const int ArSize = 20;
    char name[ArSize];
    char dessert[ArSize];
    cout << "Enter your name:\n";
    cin.getline(name, ArSize); // reads through newline
    cout << "Enter your favorite dessert:\n";
    cin.getline(dessert, ArSize);
    cout << "I have some delicious" << dessert;
    cout << " for you, " << name << ". \n";
    return 0;
}
#include <iostream>
#include<cstring>
#include <string>
using namespace std;

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

  
3. 编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和
名组合起来,并存储和显示组合结果。请使用char 数组和头文件cstring 中的函数。下面是该程序运行时的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here's the information in a single string: Fleming, Flip

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

int main() {
    char firstname[30];
    char lastname[30];
    char Name[30];
    cout << "Enter your first name: ";
    cin >> firstname;
    cout << "Enter your last name: ";
    cin >> lastname;
    strncpy(Name, lastname, 20);//strcnpy是将指定长度的字符串复制到指定的数组中
    strcat(Name, ", ");//将两个char类型连接
    strncat(Name, firstname, 20);//在字符串末尾追加n个字符
    cout << "Here's the information in a single string: " << Name << endl;
}


4. 编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名
组合起来,并存储和显示组合结果。请使用string 对象和头文件string 中的函数。下面是该程序运行时
的情形:
Enter your first name: Flip
Enter your last name: Fleming
Here's the information in a single string: Fleming, Flip

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

int main() {
    string firstname;
    string lastname;
    string Name;
    cout << "Enter your first name: ";
    getline(cin, firstname);
    cout << "Enter your last name: ";
    getline(cin, lastname);
    Name = lastname + "," + firstname;
    cout << "Here's the information in a single string: " << Name << endl;
}

  

5. 结构CandyBar 包含3 个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack 的CandyBar 变量,并将其成员分别初始化为"Mocha Munch" 、2.3 和350 。初始化应在声明snack时进行。最后,程序显示snack 变量的内容。

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

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

int main() {
    CandyBar snack = {
        "Mocha Munch" ,
        2.3,
        350
    };
    cout << "Candy name is " << snack.name << ", weight is " << snack.weight << " ,calorie content is " << snack.calorie << endl;
}


6. 结构CandyBar 包含3 个成员,如编程练习5 所示。请编写一个程序,创建一个包含3 个元素的
CandyBar 数组,并将它们初始化为所选择的值,然后显示每个结构的内容。

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

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

int  main() {
    CandyBar snack[3] = {
        {"Mocha Munch" ,2.3,350},
        {"Alps",3.2,400},
        {"Da Bai Tu",3.7,450}
    };
    cout << "Display the contents of the structure:" << endl;
    for (int i = 0; i < 3; i++) {
        cout << "Candy name: " << snack[i].name << " weight: " << snack[i].weight << " calorie: " << snack[i].calorie << endl;
    }
}

 

 
7. William Wingate 从事比萨饼分析服务。对千每个披萨饼,他都需要记录下列信息:
• 披萨饼公司的名称,可以有多个单词组成。
• 披萨饼的直径。
• 披萨饼的重盘。
请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin (或它的方法)和cout 。

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


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

int main() {
    Pizza pizza;
    cout << "Input Pizza's information:" << endl;
    cout << "name:";
    getline(cin, pizza.name);
    cout << "diameter:";
    cin >> pizza.diameter;
    cout << "weight:";
    cin >> pizza.weight;
    cout << endl;
    cout << "Display Pizza's information:" << endl;
    cout << "name: " << pizza.name << ", diameter: " << pizza.diameter << ", weight: " << pizza.weight<<endl;

}

8. 完成编程练习7, 但使用new 来为结构分配内存,而不是声明一个结构变量。另外,让程序在请求输入披萨饼公司名称之前输入披萨饼的直径。


9. 完成编程练习6, 但使用new 来动态分配数组,而不是声明一个包含3 个元素的CandyBar 数组。

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

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

int main() {
    CandyBar * snack = new CandyBar[3];
    snack[0] = { "Mocha Munch",2.3,350 };
    snack[1] = { "Fruit Salad", 1.6,140 };
    snack[2] = { "Fried Chicken", 2.4,880 };
    cout << "Display the contents of the structure:" << endl;
    for (int i = 0; i < 3; i++) {
        cout << "Candy name: " << snack[i].name << " weight: " << snack[i].weight << " calorie: " << snack[i].calorie << endl;
    }
}


10. 编写一个程序,让用户输入三次40 码跑的成绩(如果您愿意,也可让用户输入40 米跑的成绩),并显示次数和平均成绩。请使用一个array 对象来存储数据(如果编译器不支持array 类,请使用数组)。

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

int main() {
    array<double, 4> array;
    cout << "Enter the result three times:";
    cin >> array[0] >> array[1] >> array[2];
    double ave;
    ave = (array[0] + array[1] + array[2]) / 3;
    cout << "The average grade is:" << ave << endl;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值