C++ primer plus 4.13 编程练习

来源:互联网 发布:天猫dw美工教程 编辑:程序博客网 时间:2024/06/05 10:21

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:2
注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即向上调一个字母。假设用户请求A,B或C,所以不必担心D和F之间的空档。
程序:

    #include<iostream>    #include<string>    int main()    {        using namespace std;        char fir_name[80], las_name[80];        char grade,next_grade;        int num_grade;        int age;        cout << "What is your first name?";        cin.getline(fir_name, 80);        cout << endl << "What is your last name?";        cin.getline(las_name, 80);        cout << endl << "What letter grade do you deserve?";        (cin >> grade).get();        cout << endl << "What is your age?";        (cin >> age).get();        num_grade = grade + 1;        next_grade = num_grade;        cout << "Name: " << fir_name << " , " << las_name << endl;        cout << "Grade: " << next_grade << endl;        cout << "Age: " << age;        cin.get();        return 0;        }

总结:
A的ASCII可以在charint 进行转换。值的类型将引导cout 选择如何显示值——这是智能对象的另一个例子。
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";    cin.get();    return 0;    }

使用C++ String:

    #include<iostream>    #include<string>    int main()    {    using namespace std;    string name, dessert;    cout << "Enter your name:\n";    getline(cin, name);     //include newline    cout << "Enter your favorite dessert:\n";    getline(cin, dessert);    cout << "I have some delicious " << dessert << " for you, " << name << endl;    cin.get();    return 0;    }

在使用charstring 的输入中,分别使用cin.getline(char,char_size)getline(cin,string) 同时都是有包括换行符。

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

    #include<iostream>#include<string>#include<cstring>#pragma warning(disable:4996)int main(){    using namespace std;    char fri_name1[50], las_name1[50];    string fri_name2, las_name2,name2;    cout << "Enter your frist name:";    (cin >> fri_name1).get();    (cin >> fri_name2).get();    cout << endl << "Enter your last name:";    (cin >> las_name1).get();    (cin >> las_name2).get();    strcat(las_name1,", ");    strcat(las_name1, fri_name1);    name2 = las_name2 + ", " + fri_name2;    cout << endl << "Here's the information in a single string :" << las_name1;    cout << endl << "Here's the information in a single string :" << name2;    cin.get();    return 0;}

    总结:字符串的组合,char 可以使用cstring 中的函数strcat(char1,char2) 进行组合,add char2 to end of char1。但是,string 的组合可以使用 str1=str2+str3 connect。

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

    #include<iostream>#include<string>int main(){    using namespace std;struct CandyBar{    string name;    double weight;    int calorie;} candy ={    "Mocha Munch",    2.3,    250};cout << "The name of CandyBar is " << candy.name << " ,and the weight is "<< candy.weight << " and calorie is " << candy.calorie << " . \n";cin.get();return 0;}

    可以同时完成定义结构和创建结构变量的工装。为此只需将变量名放在结束括号的后面即可:

    struct perks{    int key_number;    char car[20];}mr_smith,ms_jones;    //two perks variables

    甚至可以初始化以这种方式创建的变量:

    struct perks{    int key_number;    char car[20];}mr_glitz ={    7,               //value for mr_glitz.key_number menber    "Packard"        //value for mr_glitz.car member};

    然而,将结构定义和变量声明分开,可以使程序更易于阅读和理解。
    还可以声明没有名称的结构类型,方法使省略名称,同时定义一种结构类型和一个这种类型的变量:

    struct       //no tag{    int x;   //2 member    int y;}position;   //a structure variable

    这样将创建一个名为position的结构变量。可以使用成员运算符来访问它的成员(如 position.x),但这种类型没有名称,因此以后无法创建这种类型的变量。本书将不使用这种形式的结构。

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

    #include<iostream>#include<string>using namespace std;struct CandyBar{    string name;double weight;int calorie;};int main(){    CandyBar candy[3] =    {        {"samll rabbit",2.5,300},        {"big rabbit",4.5,500},        {"white rabbit", 1, 10}    };    cout << "These candys are " << candy[0].name << " ," << candy[1].name << " and " << candy[2].name<< ".And total weight is " << candy[0].weight + candy[1].weight + candy[2].weight<< " and total calorie is " << candy[0].calorie + candy[1].calorie + candy[2].calorie<< ".\n";    cin.get();    return 0;}
  • 7.Wliilam Wingate 从事比萨饼分析服务。对于每个披萨饼,他都需要记录下列信息:
    披萨饼公司的名称,可以有多个单词组成。
    披萨饼的直径。
    披萨饼的重量。
    请涉及一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或其它方法)和cout。

    #include<iostream>#include<string>using namespace std;struct pizza{    string company;    double diameter;    double weight;};int main(){    pizza single;    cout << "Plesa input company:";    getline(cin,single.company);    cout << "Please input diameter:";    (cin >> single.diameter).get();    cout << "Please input weight:";    (cin >> single.weight).get();    cout << "Company's name: " << single.company << "\nDiameter: " << single.diameter<< "\nWeight: " << single.weight;    cin.get();    return 0;}
  • 8.完成编程练习7,但使用new来为结构来分配内存,而不是声明一个结构变量。另外,让程序在请求输入比萨饼公司名称之前输入比萨饼的直径。

    #include<iostream>#include<string>using namespace std;struct pizza{    string company;    double diameter;    double weight;};int main(){    pizza * single = new pizza;    cout << "Please input diameter:";    (cin >> single->diameter).get();    cout << "Plesa input company:";    getline(cin, single->company);    cout << "Please input weight:";    (cin >> single->weight).get();    cout << "Company's name: " << single->company << "\nDiameter: " << single->diameter<< "\nWeight: " << single->weight;    delete single;    cin.get();    return 0;}
  • 9.完成编程练习6,但使用new来动态分配数组,而不是声明一个包含3个元素的CandyBar数组。

    #include<iostream>#include<string>using namespace std;struct CandyBar{    string name;    double weight;    int calorie;};int main(){    CandyBar * candy = new CandyBar [3];    candy[0] = { "samll rabbit",2.5,300 };    candy[1] = { "big rabbit",4.5,500 };    candy[2] = { "white rabbit", 1, 10 };    cout << "These candys are " << candy[0].name << " ," << candy[1].name << " and " << candy[2].name<< ".And total weight is " << candy[0].weight + candy[1].weight + candy[2].weight<< " and total calorie is " << candy[0].calorie + candy[1].calorie + candy[2].calorie<< ".\n";    delete[] candy;    cin.get();    return 0;}
  • 10.编写一个程序,让用户输入三次40码跑的成绩(如果你愿意,也可以让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。

    #include<iostream>#include<array>using namespace std;int main(){    array<double, 3> score;    cout << "Please input first score:";    cin >> score[0];    cout << "Please input second score:";    cin >> score[1];    cout << "Please input third score:";    cin >> score[2];    cout << "First time:" << score[0]<< "\nSecone time:" << score[1]<< "\nThird time:" << score[2]<< "\nAverage score:" << (score[0] + score[1] + score[2]) / 3<< endl;    cin.get();    cin.get();    return 0;}