《C++ Primer Plus(第六版)》(5)(第四章 复合类型 答案2)

来源:互联网 发布:it行业 云计算 编辑:程序博客网 时间:2024/05/01 17:34

4.13 编程练习

1.

string firstName = "";string lastName = "";cout << "What is your first name? " ;getline(cin, firstName);cout << "What is your last name? ";getline(cin, lastName);cout << "What letter grade do you deserve? ";char grade;cin >> grade;grade++;cout << "What is your age?";int age;cin >> age;cout << "Name: " << lastName << ", " << firstName << endl;cout << "Grade: " << grade << endl;cout << "Age: " << age << endl;

2.

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 << endl;

3.

cout << "Enter your first name:";char firstName[20];cin.getline(firstName, 20);cout << "Enter your last name:";char lastName[20];cin.getline(lastName, 20);char name[80] = {0};//这个不初始化会出现中断strcat_s(name, firstName);strcat_s(name, ", ");strcat_s(name, lastName);cout << "Here's the information in a single string: "<< name;

4.

string firstName = "";string lastName = "";cout << "Enter your first name:";getline(cin, firstName);cout << "Enter your last name:";getline(cin, lastName);string name = firstName + ", " + lastName;cout << "Here's the information in a single string: " << name;

5.

struct CandyBar{string brand;//品牌double weight;//重量int calorie;//卡路里}; CandyBar snack = { "Mocha Munch", 2.3, 350 };cout << "Candy brand:" << snack.brand << " weight:" << snack.weight << " calorie:" << snack.calorie;

6.

struct CandyBar{string brand;//品牌double weight;//重量int calorie;//卡路里void getInfo(){ cout << "Candy brand:" << brand << " weight:" << weight  << " calorie:" << calorie << "\n";}}; CandyBar snack = { "Mocha Munch", 2.3, 350 };snack.getInfo();CandyBar candyArr[3] = { { "Mocha Munch", 2.3, 350 }, { "Big Rabbit", 3.4, 150 }, { "Alps", 4.5, 650 } };candyArr[0].getInfo();candyArr[1].getInfo();candyArr[2].getInfo();

7.
struct Pizza{string company;//品牌double weight;//重量double diameter;//直径}; Pizza p = { "", 0, 0 };cout << "Please enter company:";getline(cin, p.company);cout << "Please enter diameter:";cin >> p.diameter;cout << "Please enter weight:";cin >> p.weight;cout << "Pizza company:" << p.company << "\ndiameter:" << p.diameter << "\nweight:" << p.weight << "\n";

8.

struct Pizza{string company;//品牌double weight;//重量double diameter;//直径}; Pizza* p = new Pizza{ "", 0, 0 };cout << "Please enter diameter:";cin >> p->diameter;getline(cin, p->company);//把回车符读取了cout << "Please enter company:";getline(cin, p->company);cout << "Please enter weight:";cin >> p->weight;cout << "Pizza company:" << p->company << "\ndiameter:" << p->diameter << "\nweight:" << p->weight << "\n";delete p;p = NULL;

9.

struct CandyBar{string brand;//品牌double weight;//重量int calorie;//卡路里void printInfo(){ cout << "Candy brand:" << brand << " weight:" << weight  << " calorie:" << calorie << "\n";}}; CandyBar* candyArr = new CandyBar[3];candyArr[0] = { "Mocha Munch", 2.3, 350 };candyArr[1] = { "Big Rabbit", 3.4, 150 };candyArr[2] = { "Alps", 4.5, 650 } ;candyArr[0].printInfo();candyArr[1].printInfo();candyArr[2].printInfo();delete[] candyArr;candyArr = NULL;

10.

array<double, 3> socreArr;cout << "Please enter you score:";cin >> socreArr[0];cout << "Please enter you score:";cin >> socreArr[1];cout << "Please enter you score:";cin >> socreArr[2];double totalScore = socreArr[0] + socreArr[1] + socreArr[2];cout << "times:" << 3 << " averageScore:" << totalScore/3;






1 0
原创粉丝点击