Skip to content

Latest commit

 

History

History
282 lines (236 loc) · 7.72 KB

编程题.md

File metadata and controls

282 lines (236 loc) · 7.72 KB

第四章 复合类型 编程练习题

Code已使用g++编译并调试通过

  1. 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;
int main(void)
{
    string first_name,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 is your age? ";
    cin >> age;

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

    cout << "Name : "<< last_name << ","<< first_name <<endl;
    cout << "Grade : "<<++grade<<endl;
    cout << "Age : "<<age<<endl;
    return 0;
}
  1. 修改程序清单 4.4 ,使用C++ string类而不是char数组。
#include <iostream>
using namespace std;

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

完整Code:

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

int main(void)
{
    char first_name[20];
    string last_name;
    cout << "Enter your first name: ";
    cin.getline(first_name,20);

    cout << "Enter your last name: ";
    getline(cin,last_name);

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

完整Code

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

int main(void)
{
    string first_name,last_name;
    cout << "Enter your first name: ";
    getline(cin,first_name);

    cout << "Enter your last name: ";
    getline(cin,last_name);

    cout << "Here's the information in a single string: " << last_name << ", "<<first_name << endl;
    return 0;
}
  1. 结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandBar变量,并将其成员分别初始化为“Mocha Munch”、2.3和350。初始化在声明snack时进行。最后,程序显示snack变量的内容。
#include <iostream>
#include <cstring>
using namespace std;
struct CandyBar{
    string brand;
    float candy_weight;
    int calorie;
}; /*注意分号*/

int main(void)
{
    CandyBar snack = {"Mocha Munch",2.3,350};
    cout << "糖的品牌:"<<snack.brand << endl;
    cout << "糖的重量:"<< snack.candy_weight<<endl;
    cout << "糖的卡路里:"<< snack.calorie<<endl;
    return 0;
}
  1. 结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将他们初始化为所选择的值,然后显示每个结构的内容。
#include <iostream>
#include <cstring>

using namespace std;

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

int main(void)
{
    CandyBar snack[3] = {
        {"Mocha Munch_00",2.3,350},
        {"Mocha Munch_01",2.4,355},
        {"Mocha Munch_02",2.5,360}
    };
    cout << "     brand     candy_wieght      calorie"<<endl;
    for (int i = 0;i<3;i++){
        cout << snack[i].brand<<"       "<<snack[i].candy_weight
            <<"          "<<snack[i].calorie<<endl;
    }
    return 0;
}
  1. William Wingate从事披萨分析服务。对于每个披萨饼,他都需要记录下列信息:
    披萨饼公司的名称,可以有多个单词组成。
    披萨饼的直径
    披萨饼的重量
    请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或它的方法)和cout。
#include <iostream>
#include <cstring>
using namespace std;
struct pizza_info{
    string company;
    float diameter;
    float weight;
}pizza;

int main(void)
{
    cout << "please input company of pizza : ";
    getline(cin,pizza.company);

    cout << "please input weight of pizza : ";
    cin >> pizza.weight;

    cout << "please input diameter of pizza : ";
    cin >> pizza.diameter;

    cout << "Company is : "<<pizza.company << "Weight is : "
        << pizza.weight<<" , Diameter is : "<<pizza.diameter<<endl;
    return 0;
}
  1. 完成编程练习7,但使用new来为结构动态分配内存,而不是声明一个结构变量。另外,让程序在请求输入披萨名称之前输入披萨直径。
#include <iostream>
#include <cstring>
using namespace std;
struct pizza_info{
    string company;
    float diameter;
    float weight;
};

int main(void)
{
    pizza_info *p = new pizza_info;
    cout << "please input diameter of pizza : ";
    cin >> p->diameter;

    cin.get();/*处理换行符,为下一行的读取做出准备*/
    cout << "please input company of pizza : ";
    getline(cin,p->company);

    cout << "please input weight of pizza : ";
    cin >> p->weight;


    cout << "Company is : "<<p->company << " Weight is : "
        << p->weight<<" , Diameter is : "<<p->diameter<<endl;
    return 0;
}
  1. 完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar的数组。
#include <iostream>
#include <cstring>

using namespace std;

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

int main(void)
{
    CandyBar *snack = new CandyBar[3];
    for (int i = 0;i<3;i++)
        snack[i] = {"Mocha Munch",2.3,350};

    cout << "     brand     candy_wieght      calorie"<<endl;
    for (int i = 0;i<3;i++){
        cout << snack[i].brand<<"_0"<<i<<"      "<<snack[i].candy_weight
            <<"          "<<snack[i].calorie<<endl;
    }
    delete[] snack;
    return 0;
}
  1. 编写一个程序,让用户输入三次40码跑的成绩(如果你愿意,也可以让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。
#include <iostream>
#include <array>
using namespace std;
const int times =3;

int main(void)
{
    array<double,times>grade;

    cout << "please input your first grade : ";
    cin >> grade[0];

    cout << "please input your second grade : ";
    cin >> grade[1];

    cout << "please input your third grade : ";
    cin>> grade[2];

    double average = (grade[0] + grade[1] + grade[2])/times;
    cout <<times << " times average is "<< average <<endl;

    return 0;
}