C++ primer Plus(第六版)中文版 第四章 复合类型 编程练习答案

第四章 编程练习

1. 编写一个小程序,如下述输出示例的那样请求并显示信息:
   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之间的空档。

1.1 C风格字符串版

#include <iostream>

int main()
{
    using namespace std;
    const int size = 30;
    char first_name[size];
    char last_name[size];
    unsigned int age;
    char grade;
    
    cout << "What is your first name? ";
    cin.getline(first_name,size);      //或用cin.get(first_name,size).get(); 
    
    cout << "What is your last name? ";
    cin.getline(last_name,size);       //或用cin.get(last_name,size).get(); 

    cout << "What letter grade do you deserve? ";
    cin >> grade;          //cin将键盘输入的char类型的grade字符转换为对应的ASCII码
    grade += 1;            //ASCII码+1   

    cout << "what is your age? ";
    cin >> age;
    
    cout << "Name: " << last_name << ", " << first_name << endl;
    cout << "Grade: " << grade << endl;    //cout将ASCII码转换为字符
    cout << "Age: " << age << endl;
    
    return 0;
}
//或者    
#include <iostream>

int main()
{
    using namespace std;
    const int size = 30;
    char first_name[size];
    char last_name[size];
    unsigned int age;
    char grade;
    
    cout << "What is your first name? ";
    cin.getline(first_name,size);
    
    cout << "What is your last name? ";
    cin.getline(last_name,size);

    cout << "What letter grade do you deserve? ";
    cin >> grade;          //cin将键盘输入的char类型的grade字符转换为对应的ASCII码  

    cout << "what is your age? ";
    cin >> age;
    
    cout << "Name: " << last_name << ", " << first_name << endl;
    cout << "Grade: " << char(grade+1) << endl;    
    //char类型的grade与int型的1做加法运算时,自动提升为int型,grade+1的结果为int型
    //char(grade+1)强制类型转换为char类型
    //cout将ASCII码转换为字符
    cout << "Age: " << age << endl;
    
    return 0;
}

1.2 string类版

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    const int size = 30;
    string first_name;
    string last_name;
    unsigned int age;
    char grade;
    
    cout << "What is your first name? ";
    getline(cin, first_name); 
    
    cout << "What is your last name? ";
    getline(cin, last_name);

    cout << "What letter grade do you deserve? ";
    cin >> grade;          //cin将键盘输入的char类型的grade字符转换为对应的ASCII码
    grade += 1;            //ASCII码+1   

    cout << "what is your age? ";
    cin >> age;
    
    cout << "Name: " << last_name << ", " << first_name << endl;
    cout << "Grade: " << grade << endl;    //cout将ASCII码转换为字符
    cout << "Age: " << age << endl;
    
    return 0;
}

1.3 动态字符串数组版

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;
    const int size = 30;

    char first_name[size];
    char last_name[size];
    
    unsigned int age;
    char grade;

    cout << "What is your first name? ";
    cin.getline(first_name,size); 
    
    cout << "What is your last name? ";
    cin.getline(last_name,size); 

    cout << "What letter grade do you deserve? ";
    cin >> grade; 
    grade += 1;

    cout << "what is your age? ";
    cin >> age;
    
    char* firstname = new char [strlen(first_name)+1];
    char* lastname = new char [strlen(last_name)+1];
    
    strcpy(firstname, first_name);
    strcpy(lastname, last_name);
    
    cout << "Name: " << lastname << ", " << firstname << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << age << endl;
    
    delete [] firstname;
    delete [] lastname;
    
    return 0;
}

1.4 动态字符串数组+函数版

#include <iostream>
#include <cstring>
char* getname(char*);

int main()
{
    using namespace std;
    const int size = 30;

    char first_name[size];
    char last_name[size];
    
    unsigned int age;
    char grade;

    cout << "What is your first name? ";
    cin.getline(first_name,size); 
    
    cout << "What is your last name? ";
    cin.getline(last_name,size); 
    
    cout << "What letter grade do you deserve? ";
    cin >> grade; 
    grade += 1;

    cout << "what is your age? ";
    cin >> age;

    cout << "Name: " << getname(last_name) << ", " << getname(first_name) << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << age << endl;
    
    delete [] getname(last_name);
    
    return 0;
}
char* getname(char* name)
{
    char* name_new = new char [strlen(name)+1];
    strcpy(name_new, name);
    
    return name_new;
}

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

//程序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";
    // cin.get();
    return 0; 
}

修改后

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    const int ArSize = 20;
    string name;
    string 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. 编写一个程序,它要求用户首先输入其名,然后输入其姓;
   然后程序使用一个逗号和一个空格将姓和名组合起来,并存储和显示组合结果。
   请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形:
   Enter your first name: Flip
   Enter your last name: Fleming
   Here's the information in a single string: Fleming, Flip

3.1 strcpy版

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;
    const int size=30;
    char first_name[size];
    char last_name[size];
    char divide[size] = ", ";
    char full_name[size];
    
    cout << "Enter your first name: ";
    cin.get(first_name, 30).get();
    cout << "Enter your last name: ";
    cin.get(last_name, 30).get();

    strcpy(full_name, strcat(last_name, strcat(divide, first_name)));
    
    cout << "Here's the information in a single string: ";
    cout << full_name;

    return 0;
}

3.2 指针版

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;
    const int size=30;
    char first_name[size];
    char last_name[size];
    char divide[size] = ", ";
    char* full_name;
    
    cout << "Enter your first name: ";
    cin.get(first_name, 30).get();
    cout << "Enter your last name: ";
    cin.get(last_name, 30).get();

    full_name = strcat(last_name, strcat(divide, first_name));
    //或者用    
    //full_name = strcat(divide, first_name);
    //full_name = strcat(last_name, full_name);
    
    cout << "Here's the information in a single string: ";
    cout << full_name;

    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

4.1 基础版

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string first_name;
    string last_name;
    string divide = ", ";
    string  full_name;
    
    cout << "Enter your first name: ";
    getline(cin, first_name);
    cout << "Enter your last name: ";
    getline(cin, last_name);

    full_name = last_name + divide + first_name;
    
    cout << "Here's the information in a single string: ";
    cout << full_name;

    return 0;
}

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

5.1 基础版

#include <iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    using namespace std;
    
    CandyBar snack = {"Mocha Munch", 2.3, 350};
    
    cout << "The brand is " << snack.brand << endl;
    cout << "The weight is " << snack.weight << endl;
    cout << "The calorie is " << snack.calorie << endl;

    return 0;
}

5.2 同时完成定义结构和创建结构变量并初始化,省略=号

#include <iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    using namespace std;
    struct CandyBar
    {
        char brand[20];
        double weight;
        int calorie;
    }snack{"Mocha Munch", 2.3, 350};
    
    cout << "The brand is " << snack.brand << endl;
    cout << "The weight is " << snack.weight << endl;
    cout << "The calorie is " << snack.calorie << endl;

    return 0;
}

5.3 使用动态结构

#include <iostream>
#include <string>

struct CandyBar
{
    std::string brand;          //下面涉及到赋值,所以不能用C风格字符串
    double weight;
    int calorie;
};

int main()
{
    using namespace std;
    
    CandyBar* snack = new CandyBar;
    
    snack->brand = "Mocha Munch";       //或者用(*snack).brand = "Mocha Munch"; 
    snack->weight = 2.3;                //或者用(*snack).weight = 2.3;
    snack->calorie = 350;               //或者用(*snack).calorie = 2.3;
    
    cout << "The brand is " << snack->brand << endl;
    cout << "The weight is " << snack->weight << endl;
    cout << "The calorie is " << snack->calorie << endl;

    delete snack;
    
    return 0;
}

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

6.1 基础版

#include <iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    using namespace std;
    
    CandyBar candy[3]=
    {
        {"Mocha Munch", 2.3, 350},
        {"Doublemint", 5.0, 400},
        {"White Rabbit", 3.5, 200},
    };
        
    
    cout << "The first brand is " << candy[0].brand << endl;
    cout << "The first weight is " << candy[0].weight << endl;
    cout << "The first calorie is " << candy[0].calorie << endl << endl;


    cout << "The second brand is " << candy[1].brand << endl;
    cout << "The second weight is " << candy[1].weight << endl;
    cout << "The second calorie is " << candy[1].calorie << endl << endl;
    
    cout << "The third brand is " << candy[2].brand << endl;
    cout << "The third weight is " << candy[2].weight << endl;
    cout << "The third calorie is " << candy[2].calorie << endl << endl;
    
    return 0;
}

6.2 for 循环版

#include <iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    using namespace std;
    
    CandyBar candy[3]=
    {
        {"Mocha Munch", 2.3, 350},
        {"Doublemint", 5.0, 400},
        {"White Rabbit", 3.5, 200},
    };
        
    for(int i=0; i<3; i++)
    {
        cout << "The " << i+1 << " brand is " << candy[i].brand << endl;
        cout << "The " << i+1 << " weight is " << candy[i].weight << endl;
        cout << "The " << i+1 << " calorie is " << candy[i].calorie << endl << endl;
    }
    
    return 0;
}

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

7.1 基础版

#include <iostream>
struct PizzaAnalyze
{
    char brand[20];
    double diameter;
    double weight;
};

int main()
{
    using namespace std;
    
    PizzaAnalyze pizza;
    
    cout << "请输入披萨饼公司名称:";
    cin.get(pizza.brand,20).get();

    cout << "请输入披萨饼的直径:";
    cin >> pizza.diameter;
    
    cout << "请输入披萨饼的重量:";
    cin >> pizza.weight;
    
    cout << "披萨饼公司名称:" << pizza.brand << endl;
    cout << "披萨饼的直径:" << pizza.diameter << endl;
    cout << "披萨饼的重量:" << pizza.weight << endl;

    return 0;
}


7.2 动态结构数组+for+函数实现多次输入版

#include <iostream>
struct PizzaAnalyze
{
    char brand[20];
    double diameter;
    double weight;
};

void input(PizzaAnalyze*, int);
void output(PizzaAnalyze*, int);

int main()
{
    using namespace std;
    
    int num;
    
    cout << "请输入披萨的总数:";
    cin >> num;
    cin.get();
  
    PizzaAnalyze* pizza = new PizzaAnalyze [num];
    
    input(pizza, num);
    output(pizza, num); 

    delete [] pizza;
    
    return 0;
}

void input(PizzaAnalyze* pizza, int num)
{
    using namespace std;
    for (int i=0; i<num; i++)
    {
        cout << "请输入披萨饼公司名称:";
        cin.get(pizza[i].brand,20).get();

        cout << "请输入披萨饼的直径:";
        cin >> pizza[i].diameter;
        cin.get();
    
        cout << "请输入披萨饼的重量:";
        cin >> pizza[i].weight;
        cin.get();
        
        cout << "\n";
    }
}

void output(PizzaAnalyze* pizza, int num)
{
    using namespace std;
    for (int i=0; i<num; i++)
    {
        cout << "第"<< i+1 << "个披萨饼公司名称:" << pizza[i].brand << endl;
        cout << "第"<< i+1 << "个披萨饼的直径:" << pizza[i].diameter << endl;
        cout << "第"<< i+1 << "个披萨饼的重量:" << pizza[i].weight << endl << endl;
    }
}

8. 完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。

    另外,让程序在请求输入比萨饼公司名称之前输入披萨饼的直径。
 

#include <iostream>
struct PizzaAnalyze
{
    char brand[20];
    double diameter;
    double weight;
};

int main()
{
    using namespace std;
    PizzaAnalyze* pizza = new PizzaAnalyze;
    
    cout << "请输入披萨饼的直径:";
    cin >> (*pizza).diameter;
    cin.get();
    
    cout << "请输入披萨饼公司名称:";
    cin.get((*pizza).brand,20).get();


    
    cout << "请输入披萨饼的重量:";
    cin >> (*pizza).weight;
    
    cout << "披萨饼公司名称:" << (*pizza).brand << endl;
    cout << "披萨饼的直径:" << (*pizza).diameter << endl;
    cout << "披萨饼的重量:" << (*pizza).weight << endl;

    delete pizza;
    
    return 0;
}
    

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

#include <iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    using namespace std;
    int size = 3;

    CandyBar* candy = new CandyBar [size];
    candy[0] = {"Mocha Munch", 2.3, 350};
    candy[1] = {"Doublemint", 5.0, 400};
    candy[2] = {"White Rabbit", 3.5, 200};   
    
    cout << "The first brand is " << candy[0].brand << endl;
    cout << "The first weight is " << candy[0].weight << endl;
    cout << "The first calorie is " << candy[0].calorie << endl << endl;


    cout << "The second brand is " << candy[1].brand << endl;
    cout << "The second weight is " << candy[1].weight << endl;
    cout << "The second calorie is " << candy[1].calorie << endl << endl;
    
    cout << "The third brand is " << candy[2].brand << endl;
    cout << "The third weight is " << candy[2].weight << endl;
    cout << "The third calorie is " << candy[2].calorie << endl << endl;
    
    delete [] candy;
    
    return 0;
}

9.2 for循环版

#include <iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};
void output(CandyBar*);

int main()
{
    using namespace std;
    int size = 3;

    CandyBar* candy = new CandyBar [size];
    candy[0] = {"Mocha Munch", 2.3, 350};
    candy[1] = {"Doublemint", 5.0, 400};
    candy[2] = {"White Rabbit", 3.5, 200};   
        
    output(candy);
    
    return 0;
}

void output(CandyBar* candy)
{
    using namespace std;
    
    for(int i=0; i<3; i++)
    {
        cout << "The " << i+1 << " brand is " << candy[i].brand << endl;
        cout << "The " << i+1 << " weight is " << candy[i].weight << endl;
        cout << "The " << i+1 << " calorie is " << candy[i].calorie << endl << endl;
    }
}

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

10.1 基础版

#include <iostream>
#include <array>

int main()
{
    using namespace std;
    const int num = 3;
    array<double, num> time;
    double average;

    cout << "请输入第1次40米跑的成绩:";
    cin >> time[0];
    cout << "请输入第2次40米跑的成绩:";
    cin >> time[1];
    cout << "请输入第3次40米跑的成绩:";
    cin >> time[2];

    average = (time[0] + time[1] + time[2]) / num;
    cout << "第一次的成绩为:" << time[0] << endl;
    cout << "第二次的成绩为:" << time[1] << endl;
    cout << "第三次的成绩为:" << time[2] << endl;
    cout << num << "次的平均成绩为" << average << endl;
    
    return 0;
}

10.1 for循环版

#include <iostream>
#include <array>

int main()
{
    using namespace std;
    const int num = 3;
    array<double, num> time;
    double average = 0;

    for(int i=0; i < num; i++)
    {
        cout << "请输入第" << i+1 <<"次40米跑的成绩:";
        cin >> time[i];
    }
    
    for(int i=0; i < num; i++)
        average = average + time[i];
    average = average / num;
    
    for(int i=0; i < num; i++)
        cout << "第" << i+1 <<"次的成绩为:" << time[i] << endl;
    cout << num << "次的平均成绩为" << average << endl;
    
    return 0;
}

10.2 for循环+函数版

#include <iostream>
#include <array>

using namespace std;

const int num = 3;
array<double, num> time;

array<double, num> input(array<double, num> time, const int num);
double calculate_average(array<double, num> time, const int num);
void output(array<double, num>time, const int num, double average);

int main()
{
    double average;

    time = input(time, num);
    
    average = calculate_average(time, num);
    
    output(time, num, average);
   
    return 0;
}
array<double, num> input(array<double, num> time, const int num)
{
    using namespace std;
    for(int i=0; i < num; i++)
    {
        cout << "请输入第" << i+1 <<"次40米跑的成绩:";
        cin >> time[i];
    }
    return time;
}

double calculate_average(array<double, num> time, const int num)
{
    double average = 0;
    
    for(int i=0; i < num; i++)
        average = average + time[i];
    average = average / num;
    
    return average;
}
    
void output(array<double, num> time, const int num, double average)
{
    for(int i=0; i < num; i++)
        cout << "第" << i+1 <<"次的成绩为:" << time[i] << endl;
    cout << num << "次的平均成绩为" << average << endl;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值