码迷,mamicode.com
首页 > 编程语言 > 详细

《C++ primer plus 英文版 第六版》Chapter 4

时间:2018-04-17 00:04:13      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:etl   integer   oid   space   bin   hat   lin   .com   character   

Chapter Review

1

a. char actors[30];

b. short betsie[100];

c. float chunk[13];

d. long double dipsea[64];

2

a. array<char, 30> actors;

b. array<short, 100> betsie;

c. array<float, 13> chunk;

d. array<long double, 64> dipsea;

3

int oddly[5] = {1, 3, 5, 7, 9};

4

int even = oddly[0] + oddly[4];

5

cout << ideas[1] << "\n"; // or << endl;

6

char lunch[13] = "cheeseburger"; // number of characters + 1
or
char lunch[] = "cheeseburger"; // let the compiler count elements

7

string lunch = "Waldorf Salad";
or, if you don‘t have a using directive,
std::string lunch = "Waldorf Salad";

8

struct fish
{
    char kind[20];
    int weight;
    float length;
};

9

fish petes = 
{
    "trout",
    12,
    26.66
}

10

enum Response {No, Yes, Maybe};

11

double * pd = &ted;
cout << *pd << "\n";

12

float * pf = treacle; // or = &treacle[0]
cout << pf[0] << " " << pf[9] << "\n";
// or use *pf and *(pf + 9)

13

This assumes that the iostream and vector header files have been included and that there is a using directive:

unsigned int size;
cout << "Enter a positive integer: ";
cin >> size;
int * dyn = new int [size];
vector<int> dv(size);

14

Yes, it is valid. The expression "Home of the jolly bytes" is a string constant; hence it evaluates as the address of the beginning of the string. The cout object interprets the address of a char as an invitation to print a string, but the type cast (int *) converts the address to type pointer-to-int, which is then printed as an address. In short, the statement prints the address of the string, assuming the int type is wide enough to hold an address.

15

struct fish
{
    char kind[20];
    int weight;
    float length;
};

fish * pole = new fish;
cout << "Enter kind of fish: ";
cin >> pole -> kind;

16

Using cin >> address causes a program to skip over whitespace until it finds non-whitespace. It then reads characters until it encounters whitespace again. Thus, it will skip over the newline following the numeric input, avoiding that problem. On the other hand, it will read just a single word, not an entire line.

17

#include <string>
#include <vector>
#include <array>
const int Str_num {10}; // or = 10
...
std::vector<std::string> vstr(Str_num);
std::array<std::string, Str_num> astr;

Programming Exercises

1

#include <iostream>

int main()
{
    using namespace std;
    
    cout << "What is your first name? ";
    char first_name[20];
    cin.getline(first_name, 20);
    
    cout << "What is your last name? ";
    char last_name[20];
    cin.getline(last_name, 20);
    
    cout << "What letter grade do you deserve? ";
    char grade;
    cin.get(grade);
    
    cout << "What is your age? ";
    int age;
    cin >> age;
    
    cout << "Name: " << last_name << ", " << first_name << endl
         << "Grade: " << grade << "\n"
         << "Age: " << age << endl;
    
    return 0;
}

2

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    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

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;
    
    char first_name[20], last_name[20];
    char combined[40];
    
    cout << "Enter your first name: ";
    cin >> first_name;
    cout << "Enter your favorite last name: ";
    cin >> last_name;
    
    strcpy(combined, last_name);
    strcat(combined, ", ");
    strcat(combined, first_name);
    cout << "Here's the information in a single string: " << combined << endl;
    
    return 0;
}

4

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    string first_name, last_name, combined;
    
    cout << "Enter your first name: ";
    cin >> first_name;
    cout << "Enter your favorite last name: ";
    cin >> last_name;
    
    combined = last_name + ", " + first_name;
    cout << "Here's the information in a single string: " << combined << endl;
    
    return 0;
}

5

#include <iostream>
#include <string>

struct CandyBar
{
    std::string brand;
    float weight;
    int calories;
};

int main()
{
    using namespace std;
    
    CandyBar snack = {"Mocha Munch", 2.3, 350};
    
    cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight
         << "\nCalories = " << snack.calories << endl;
    
    return 0;
}

6

#include <iostream>
#include <string>

struct CandyBar
{
    std::string brand;
    float weight;
    int cal;
};

int main()
{
    using namespace std;
    
    CandyBar snack[3] = {{"Mocha Munch", 2.3, 350}, {"Wei Long", 0.5, 222}, {"crisps", 1.0, 500}};
    
    cout << "Brand: " << snack[0].brand << endl
         << "Weight: " << snack[0].weight << endl
         << "Calories: " << snack[0].cal << endl << endl;
    
    cout << "Brand: " << snack[1].brand << endl
         << "Weight: " << snack[1].weight << endl
         << "Calories: " << snack[1].cal << endl << endl;
    
    cout << "Brand: " << snack[2].brand << endl
         << "Weight: " << snack[2].weight << endl
         << "Calories: " << snack[2].cal << endl << endl;
    
    return 0;
}

7

#include <iostream>
#include <string>

using namespace std;

struct Pizza
{
    string company;
    float diameter;
    float weight;
};

int main()
{
    Pizza piz;
    
    cout << "Enter the pizza's company: ";
    getline(cin, piz.company);
    cout << "Enter the pizza's diameter(in CM): ";
    cin >> piz.diameter;
    cout << "Enter the pizza's weight(in Kg): ";
    cin >> piz.weight;
    
    cout << "Company: " << piz.company << endl;
    cout << "Diameter: " << piz.diameter << " CM" << endl;
    cout << "Weight: " << piz.weight << " Kg" << endl;
    
    return 0;
}

8

#include <iostream>
#include <string>

using namespace std;

struct Pizza
{
    string company;
    float diameter;
    float weight;
};

int main()
{
    Pizza * piz = new Pizza;
    
    cout << "Enter the pizza's company: ";
    getline(cin, piz -> company);
    cout << "Enter the pizza's diameter(in CM): ";
    cin >> piz -> diameter;
    cout << "Enter the pizza's weight(in Kg): ";
    cin >> piz -> weight;
    
    cout << "Company: " << piz -> company << endl;
    cout << "Diameter: " << piz -> diameter << " CM" << endl;
    cout << "Weight: " << piz -> weight << " Kg" << endl;
    
    return 0;
}

9

#include <iostream>
#include <string>

struct CandyBar
{
    std::string brand;
    float weight;
    int cal;
};

int main()
{
    using namespace std;
    
    CandyBar * snack = new CandyBar[3];
    snack[0] = {"Mocha Munch", 2.3, 350};
    snack[1] = {"Wei Long", 0.5, 222};
    snack[2] = {"crisps", 1.0, 500};
    
    cout << "Brand: " << snack[0].brand << endl
         << "Weight: " << snack[0].weight << endl
         << "Calories: " << snack[0].cal << endl << endl;
    
    cout << "Brand: " << snack[1].brand << endl
         << "Weight: " << snack[1].weight << endl
         << "Calories: " << snack[1].cal << endl << endl;
    
    cout << "Brand: " << snack[2].brand << endl
         << "Weight: " << snack[2].weight << endl
         << "Calories: " << snack[2].cal << endl << endl;
    
    delete [] snack;
    
    return 0;
}

10

#include <iostream>
#include <array>

int main()
{
    using namespace std;
    
    cout << "Enter your three 40-yard runnings' scores: ";
    array<float, 3> scores;
    cin >> scores[0] >> scores[1] >> scores[2];
    cout << "Average: " << (scores[0] + scores[1] + scores[2]) / 3.0 << endl;
    
    return 0;
}

《C++ primer plus 英文版 第六版》Chapter 4

标签:etl   integer   oid   space   bin   hat   lin   .com   character   

原文地址:https://www.cnblogs.com/narisu/p/8859108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!