标签:int namespace 三次 cout sse delete new cat uri
C++ Primer Pluse 课后习题答案 第四章
#include<iostream>
#include<string>
#include<cstring>
#include<array>
using namespace std;
// practice 1
void p4_1(void)
{
string firstname;
string lastname;
char grade;
int age;
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? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cin.get();
cout << "Name: " << lastname << ", " << firstname << endl;
grade = int(grade + 1);
cout << "Grade: " << char(grade) << endl;
cout << "Age: " << age << endl;
}
// practice 2
void p4_2(void)
{
const int ArSize = 20; // 这句可以去掉
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favourite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
return;
}
// practice 3
void p4_3()
{
char firstname[40];
char lastname[40];
char name[80] = { ‘\0‘ };
cout << "Enter your first name: ";
cin.getline(firstname, 40);
cout << "Enter your last name ";
cin.getline(lastname, 40);
strcpy_s(name, 80,lastname);
strcat_s(name, 80 ,", ");
strcat_s(name, 80, firstname);
cout << "Here‘s the information in a single string: " << name << endl;
}
// practice 4
void p4_4(void)
{
string firstname;
string lastname;
string name;
string band = ", ";
cout << "Enter your first name: ";
getline(cin, firstname);
cout << "Enter your last name ";
getline(cin, lastname);
name = lastname + band + firstname;
cout << "Here‘s the information in a single string: " << name << endl;
}
// practice 5
struct CandyBar
{
char brand[40];
float weight;
int calorie;
};
void p4_5(void)
{
CandyBar snack = { "Mocha Munch", 2.3, 350};
cout << "snack.brand = " << snack.brand << endl;
cout << "snack.weight = " << snack.weight << endl;
cout << "snack.calorie = " << snack.calorie << endl;
}
//practice 6
void p4_6(void)
{
CandyBar snack[3];
snack[0] = { "a",0.1,1 };
snack[1] = { "b",0.2,2 };
snack[2] = { "c",0.3,3 };
cout << "snack[0].brand = " << snack[0].brand << endl;
cout << "snack[1].weight = " << snack[1].weight << endl;
cout << "snack[2].calorie = " << snack[2].calorie << endl;
}
//practice 7
struct Pizza
{
char company[40];
double diameter;
double weight;
};
void p4_7(void)
{
Pizza example;
cout << "Enter the pizza company name: " << endl;
cin.getline(example.company, 40);
cout << "Enter the pizza diameter in inch: " << endl;
cin >> example.diameter;
cout << "Enter the pizza weight in pound: " << endl;
cin >> example.weight;
cout << "example.company = " << example.company << endl;
cout << "example.diameter = " << example.diameter << " inches" << endl;
cout << "example.weight = " << example.weight << " pounds" << endl;
}
//practice 8
void p4_8(void)
{
Pizza* pt = new Pizza;
cout << "Enter the pizza diameter in inch: " << endl;
cin >> pt->diameter;
cin.get();
cout << "Enter the pizza company name: " << endl;
cin.getline(pt->company, 40);
cout << "Enter the pizza weight in pound: " << endl;
cin >> pt->weight;
cout << "diameter = " << pt->diameter << endl;
cout << "company = " << pt->diameter << " inches" << endl;
cout << "weight = " << pt->weight << " pounds" << endl;
delete pt;
}
//practice 9
void p4_9(void)
{
CandyBar* snack = new CandyBar[3];
snack[0] = { "a",0.1,1 };
snack[1] = { "b",0.2,2 };
snack[2] = { "c",0.3,3 };
cout << "snack[0].brand = " << (*snack).brand << endl;
cout << "snack[1].weight = " << (*(snack+1)).weight << endl;
cout << "snack[2].calorie = " << snack[2].calorie << endl;
delete [] snack;
}
//practice 10
void p4_10()
{
double ave_grade;
array<double, 3>grade;
cout << "Enter your grades of 40-meters-match in seconds: " << endl;
cin >> grade[0] >> grade[1] >> grade[2];
ave_grade = (grade[0] + grade[1] + grade[2]) / 3.0;
cout << "第一次: " << grade[0] << " s" << endl;
cout << "第二次: " << grade[1] << " s" << endl;
cout << "第三次: " << grade[2] << " s" << endl;
cout << "平均成绩: " << ave_grade << " s" << endl;
}
int main()
{
p4_10();
return 0;
}
标签:int namespace 三次 cout sse delete new cat uri
原文地址:https://www.cnblogs.com/gyblogs/p/11666873.html