C++ Primer Plus 编程练习4

C++ Primer Plus 编程练习4

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"
using namespace std;

const int nsize = 20;
struct student
{
    char name[nsize];
    char last_name[nsize];
    char grade;
    int age;
};
void display(student);

int main()
{
    cout << "what is your name? " ;
    student dyg;
    cin.getline(dyg.name, nsize);
    cout << "what is your last_name? " ;
    cin.getline(dyg.last_name, nsize);
    cout << "what is your grade? ";
    cin >> dyg.grade;
    cout << "what is your age? " ;
    cin >> dyg.age;
    display(dyg);
    return 0;
}

void display(student lz)
{
    cout << "name: " << lz.name <<"," << lz.last_name << endl;
    cout << "grade: " << char(lz.grade + 1) << endl;
    cout << "age: " << lz.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 "string"
using namespace std;

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

const int nsize = 20;

int main()
{
    char fname[nsize];
    char lname[nsize];
    char name[2*nsize+1];

    cout << "enter your first name: ";
    cin.getline(fname, nsize);
    cout << "enter your last name: ";
    cin.getline(lname, nsize);
    strncpy(name, fname, nsize);
    strcat(name, ",");
    strncat(name,lname,nsize);
    name[2*nsize] = '\0';
    cout << "here's the information in a single string: " << name << endl;
    return 0;
}

在这里插入图片描述

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 "string"
using namespace std;

int main()
{
    string fname, lname, add, fullname;
    cout << "enter your first name: ";
    getline(cin, fname);
    cout << "enter your last name: ";
    getline(cin, lname);
    add = ",";
    fullname = fname + add + lname;
    cout << "here's the information in a single string: " << fullname << endl;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
using namespace std;

const int asize = 20;
struct candybar 
{
    char brand[asize];
    double weight;
    int calory;
};

int main()
{
    candybar snack = {"Mocha Munch", 2.3, 350};
    cout << "here's the information of snack:" << endl;
    cout << "brand: " << snack.brand << endl;
    cout << "weight: " << snack.weight << endl;
    cout << "calory: " << snack.calory ;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
using namespace std;

const int asize = 20;
struct candybar 
{
    char brand[asize];
    double weight;
    int calory;
};

int main()
{
    candybar snack[3] =
    {
        {"A", 1.2, 3},
        {"B", 4.5, 6},
        {"C", 7.8, 9}
    };
    for (int i = 0; i < 3;i++)
    {
        cout <<" brand:"<< snack[i].brand << endl
             <<" weight:"<< snack[i].weight << endl
             <<" calory:"<< snack[i].calory << endl<<endl;
    }
    return 0;
}

在这里插入图片描述

7,William Wingate从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:

  • 披萨饼公司的名称,可以有多个单词组成。
  • 披萨饼的直径。
  • 披萨饼的重量。

请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。

#include "iostream"
#include "string"
using namespace std;

const int asize = 20;
struct pizza
{
    char company[asize];
    double diameter;
    double weight;
};

int main()
{
    pizza pie;
    cout << "what's the name of the pizza company?";
    cin.getline(pie.company, asize);
    cout << "what's the diameter of the pizza?";
    cin >> pie.diameter;
    cout << "what's the weight of the pizza?";
    cin >> pie.weight;
    cout << "company: " << pie.company << endl;
    cout << "diameter: " << pie.diameter << endl;
    cout << "weight: " << pie.weight << endl;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
#include "string"

using namespace std;
const int asize = 20;
struct pizza
{
    char company[asize];
    double diameter;
    double weight;
};

int main()
{
    pizza *pie = new pizza;
    cout << "what's the diameter of pizza? ";
    cin >> pie->diameter;
    cin.get();
    cout << "what's the name of the pizza company? ";
    cin.get(pie->company, asize);
    cout << "what's the weight of the pizza ? ";
    cin >> pie->weight;
    cout << "company: " << pie->company << endl;
    cout << "diameter(inches): " << pie->diameter << endl;
    cout << "weight(ounce): " << pie->weight << endl;
    delete pie;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
#include "string"
using namespace std;

struct candybar
{
    string brand;
    double weight;
    int calory;
};

int main()
{
    candybar *snack = new candybar[3];
    snack[0].brand = "A";
    snack[0].weight = 1.1;
    snack[0].calory = 600;
    snack[1].brand = "C";
    snack[1].weight = 6.9;
    snack[1].calory = 900;
    snack[2].brand = "X";
    snack[2].weight = 12.6;
    snack[2].calory = 1900;

    for (int i = 0; i < 3;i++)
    {
        cout << "brand: " << snack[i].brand << endl
             << "weight: " << snack[i].weight << endl
             << "calory: " << snack[i].calory << endl
             << endl;
    }
    delete[] snack;
    return 0;
}

在这里插入图片描述

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

#include "iostream"
#include "array"
using namespace std;

int main()
{
    array<double, 4> grade = {0};
    cout << "输入三次40米跑成绩: " << endl;
    cin >> grade[0] >> grade[1] >> grade[2] ;
    cout << "成绩1: " << grade[0] << endl;
    cout << "成绩2: " << grade[1] << endl;
    cout << "成绩3: " << grade[2] << endl;

    grade[3] = (grade[0] + grade[1] + grade[2]) / 3;
    cout << "平均成绩:"<<grade[3] << endl;
    return 0;
}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值