zoukankan      html  css  js  c++  java
  • [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

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void main() {
     5     char first_name[30], last_name[30];
     6     char grade;
     7     int age;
     8     cout <<"what is your first name?";
     9     gets_s(first_name);//cin.getline(first, 20); 
    10     cout << "what is your last name?";
    11     gets_s(last_name);
    12     cout << "what letter grade do you deserve?";
    13     cin >> grade;
    14     cout << "what is your age?";
    15     cin >> age;
    16 
    17     cout << "Name:" << last_name << "," << first_name << endl;
    18     cout << "Grade:" << char (grade + 1) << endl;
    19     cout << "Age:" << age << endl;
    20 
    21     system("pause");
    22 }

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

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 void main() {
     6     string name;
     7     string dessert;
     8 
     9     cout << "What's your name?" << endl;
    10     getline(cin, name); //读取用户输入的一行字符给name
    11     cout << "What's your favorate dessert?" << endl;
    12     getline(cin, dessert);
    13     cout << "I have some delicious " << dessert << " for you." << endl;
    14     system("pause");
    15 }

    3 . 编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起來,并存储和显示组合结果。请使用 char 数组和头义件 cstring 中的函数。下面是该程序运行时的情形:

    Enter your first name : Flip
    Enter your last name : Fleming
    Here's the information in a single string :Fleming, Flip

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void main() {
     5     char first[20],last[20];
     6 
     7     cout << "Enter your first name :";
     8     cin >> first;
     9     cout << "Enter your last name :";
    10     cin >> last;
    11     cout << "Here's the information in a single string :" << last << "," << first << endl;
    12     system("pause");
    13 }

    4.编写一个程序,它要求用户首先输入其名,再输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string类和头文件string中的函数。下面是程序运行时的情形:

    Enter your first name: Flip

    Enter your last name: Fleming

    Here's the information in a single string: Fleming, Flip

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 void main() {
     6     string first,last;
     7 
     8     cout << "Enter your first name :";
     9     cin >> first;
    10     cout << "Enter your last name :";
    11     cin >> last;
    12     cout << "Here's the information in a single string :" << last << "," << first << endl;
    13     system("pause");
    14 }

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

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 struct CandyBar
     6 {
     7     char kind[30];
     8     double weight;
     9     int energy;
    10 };
    11 
    12 void main() {
    13     CandyBar snack = { "Mocha Munch",2.3,350};
    14 
    15     cout << snack.kind<<endl;
    16     cout << snack.weight << endl;
    17     cout << snack.energy<< endl;
    18     system("pause");
    19 }

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

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 struct CandyBar
     6 {
     7     char kind[30];
     8     double weight;
     9     int energy;
    10 };
    11 
    12 void main() {
    13     CandyBar snack[3] = {   {"Mocha Munch",2.3,350},
    14                             {"wu can rou",2.5,550},
    15                             {"qi cai hong",1.5,150 } };
    16 
    17     cout << snack[0].kind<<"," << snack[0].weight << "," << snack[0].energy <<endl;
    18     cout << snack[1].kind << "," << snack[1].weight << "," << snack[1].energy << endl;
    19     cout << snack[2].kind << "," << snack[2].weight << "," << snack[2].energy << endl;
    20     system("pause");
    21 }

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

    ● 披萨饼公司的名称,可以由多个单词组成。

    ● 披萨饼的直径。

    ● 披萨饼的重量。

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

     1 #include<iostream>
     2 using namespace std;
     3 
     4 struct Pizza
     5 {
     6     char company[30];
     7     double diameter;
     8     double weight;
     9 };
    10 
    11 void main() {
    12     int flag = 1;
    13     while (flag)
    14     {
    15         Pizza p;
    16         cout << "请输入公司名:";
    17         cin.getline(p.company,30);
    18         cout << "请输入披萨直径:";
    19         cin >> p.diameter;
    20         cout << "请输入披萨重量:";
    21         cin >> p.weight;
    22         cout << "公司名:" << p.company << ",披萨直径:" << p.diameter << ",披萨重量:" << p.weight << endl;
    23 
    24         cout << "-----------------------------------------------------------------" << endl;
    25         cout << "输入数字0退出,输入其他数字继续:";
    26         cin >> flag;
    27         cin.get();
    28     }
    29     //system("pause");
    30 }

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

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 
     5 struct Pizza
     6 {
     7     char company[30];
     8     double diameter;
     9     double weight;
    10 };
    11 
    12 void main() {
    13     int flag = 1;
    14     while (flag)
    15     {
    16         Pizza *p=new Pizza;
    17         cout << "请输入披萨直径:";
    18         cin >> (*p).diameter;
    19         cin.get();//读取掉换行符
    20         cout << "请输入公司名:";
    21         cin.getline((*p).company,30);
    22         cout << "请输入披萨重量:";
    23         cin >> (*p).weight;
    24         cout << "公司名:" << (*p).company << ",披萨直径:" << (*p).diameter << ",披萨重量:" << (*p).weight << endl;
    25         delete p;
    26 
    27         cout << "-----------------------------------------------------------------" << endl;
    28         cout << "输入数字0退出,输入其他数字继续:";
    29         cin >> flag;
    30         cin.get();
    31     }
    32     //system("pause");
    33 }

    注意:使用new分配内存之后一定要记得delete

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

     1 #include<iostream>
     2 using namespace std;
     3 
     4 struct CandyBar
     5 {
     6     char kind[30];
     7     double weight;
     8     int energy;
     9 };
    10 
    11 void main() {
    12     CandyBar *p = new CandyBar;
    13     *p = { "Mocha Munch",2.3,350 };
    14     *(p + 1) = {"wu can rou", 2.5, 550};
    15     *(p + 2) = { "qi cai hong",1.5,150 };
    16 
    17     cout << p[0].kind << "," << p[0].weight << "," << p[0].energy << endl;
    18     cout << (*(p + 1)).kind << "," << (*(p + 1)).weight << "," << (*(p + 1)).energy << endl;
    19     cout << (p + 2)->kind << "," << (p + 2)->weight << "," << (p + 2)->energy << endl;
    20 
    21     delete p;
    22     system("pause");
    23 }

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

     1 #include<iostream>
     2 #include<array>
     3 using namespace std;
     4 
     5 void main() {
     6     array<double, 3> a;
     7     cout << "请输入三次百米跑的成绩。" << endl;
     8     cout << "请输入第一次的成绩(秒):";
     9     cin >> a[0];
    10     cout << "请输入第二次的成绩(秒):";
    11     cin >> a[1];
    12     cout << "请输入第三次的成绩(秒):";
    13     cin >> a[2];
    14 
    15     cout << "您3次的成绩分别为:" << a[0] << "秒," << a[1] << "秒," << a[2] << "秒。"<<endl;
    16     cout << "平均成绩为:" << (a[0] + a[1] + a[2]) / 3.0 << "秒," << endl;
    17     
    18     system("pause");
    19 }

  • 相关阅读:
    Python合集之Python循环语句(二)
    io流2
    io流
    集合工具类
    泛型
    Map
    VSCode_Extensions
    C++ in VSCode
    C# 私有字段前缀 _ 的设置(VS2019, .editorconfig)
    dotnet 跨平台编译发布
  • 原文地址:https://www.cnblogs.com/little-monkey/p/7488416.html
Copyright © 2011-2022 走看看