C++PrimerPlus 练习题

C++ Primer plus 第4章练习题

 1. 第一题 按照提示输入信息,并输出 ----完全按照书上来的

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     // 第一题 按照提示输入信息,并输出 ----完全按照书上来的
 9     string first_name;
10     string last_name;
11     //年龄
12     int age;
13     // 成绩向下调整
14     char grade;
15 
16     cout << "What is your first name? \n";
17     cin >> first_name;
18     cout << "What is your last name? \n";
19     cin >> last_name;
20     cout << "What letter grade do you deserve? (A,B,C,D,E,F) \n";
21     cin >> grade;
22     cout << "What is your age? \n";
23     cin >> age;
24 
25     cout << "Name:" << last_name << "," << first_name << endl;
26     cout << "Grade:" << grade << endl;
27     cout << "Age:" << age << endl;
28     system("pause");
29     return 0;
30 }

2. 第二题     将Char数组转换成 string

 

#include <iostream>
#include <string>

using namespace std;

int main()
{
    const int Arsize = 20;
    string name;
    string eat_name;
    
    cout << "Enter your name:\n";
    cin >> name;
    cout << "Enter your favorite dessert:\n";
    cin>>eat_name;
    if (name.length() > Arsize)
        cout<< "输入错误"<<endl;
    else
        cout << "I have some delicious" << eat_name << "for you, " << name << endl;
}

 3  第五题 及第六题

#include <iostream>
#include <string>

using namespace std;

struct  Candy
{
    string brand;
    float weight;
    int calorie;
};



int main()
{
    Candy snack =
    {
        "Mocha Munch",
        12.52,
        123213,
    };

    Candy *snack1 = new Candy();
    snack1->brand = " Mocha Munch";
    snack1->weight = 231.1;
    snack1->calorie = 31321;

    Candy CandyBar[3];
    CandyBar[0].brand = "dadsa";
    CandyBar[0].weight = 231.3;
    CandyBar[0].calorie = 231;
    CandyBar[1].brand = "cnmzxcmz";
    CandyBar[1].weight = 3331.33;
    CandyBar[1].calorie = 2321;
    CandyBar[2].brand = "czxkckzx";
    CandyBar[2].weight = 9889.33;
    CandyBar[2].calorie = 942;
    cout << CandyBar[0].calorie << endl;
    cout << CandyBar[0].weight << endl;
    cout << CandyBar[0].brand << endl;

    cout << CandyBar[1].calorie << endl;
    cout << CandyBar[1].brand << endl;
    cout << CandyBar[1].weight << endl;

    cout << CandyBar[2].calorie << endl;
    cout << CandyBar[2].brand << endl;
    cout << CandyBar[2].weight << endl;
    system("pause");
    return 0;
}

 

posted @ 2018-08-16 16:55  Solitary_Mccree  阅读(203)  评论(0编辑  收藏  举报