C++ primer plus 6 第四章习题答案
//4.4 使用string对象和头文件
#include<iostream>
#include<string>
using namespace std;
int main()
{
string first_name, last_name, full_name;
cout << "Enter your first name: ";
getline(cin, first_name);
cout << "Enter your last name: ";
getline(cin, last_name);
full_name = last_name + "." + first_name;
cout << "Here's the information in a single string: ";
cout << full_name << endl;
return 0;
}
// 4.5 CandyBar 糖块
#include<iostream>
using namespace std;
struct CandyBar
{
char brand[20];
float weight;
unsigned int calorie;
};
int main()
{
CandyBar snack = { "Mocha Munch", 2.3, 350 };
cout << "My favourite CandyBar is " << snack.brand << "." << endl;
cout << "And its weight is " << snack.weight << ", calorie is " << snack.calorie;
cout << "." << endl;
return 0;
}
//4.6 Candy 结构相关
#include<iostream>
using namespace std;
struct CandyBar
{
char brand[20];
float weight;
unsigned int calorie;
};
int main()
{
CandyBar snack[3] = { {"Mocha Munch", 2.3, 350},{"Hershey bar", 4.2, 550},{"Musketeers", 2.6, 430} };
cout << "My 1st CandyBar is " << snack[0].brand << "." << endl;
cout << "And its weight is " << snack[0].weight << ", calorie is " << snack[0].calorie;
cout << "." << endl;
cout << "My 2st CandyBar is " << snack[1].brand << "." << endl;
cout << "And its weight is " << snack[1].weight << ", calorie is " << snack[1].calorie;
cout << "." << endl;
cout << "My 3st CandyBar is " << snack[2].brand << "." << endl;
cout << "And its weight is " << snack[2].weight << ", calorie is " << snack[2].calorie;
cout << "." << endl;
return 0;
}
//4.7 william wingate 披萨
#include<iostream>
using namespace std;
struct Pizza
{
char company[40];
float diameter;
float weight;
};
int main()
{
Pizza dinner;
cout << "Enter the PIzza's information: " << endl;
cout << "Pizza's company:";
cin.getline(dinner.company, 40);
cout << "Pizza's diameter(inchs): ";
cin >> dinner.diameter;
cout << "CandBar's weight(pounds):";
cin >> dinner.weight;
cout << "The lunch pizza is " << dinner.company << "." << endl;
cout << "And its diameter is " << dinner.diameter << " inch,weight is " << dinner.weight;
cout << "pounds." << endl;
return 0;
}
//4.8 披萨公司相关
#include<iostream>
using namespace std;
struct Pizza
{
char company[40];
float diameter;
float weight;
};
int main()
{
Pizza* ppizza = new Pizza;
cout << "Enter the pizza's information: " << endl;
cout << "Pizza's diameter(inchs): ";
cin >> ppizza->diameter;
cout << "Pizza's company:";
cin.getline(ppizza->company, 40);
cout << "CandBar's weight(pounds): ";
cin >> ppizza->weight;
cout << "The lunch pizza is " << ppizza->company << "." << endl;
cout << "And it's diameter is " << ppizza->diameter << " inch,weight is" << ppizza->weight;
cout << "pounds. " << endl;
delete ppizza;
return 0;
}
//4.9 使用new动态分配数组
#include<iostream>
using namespace std;
struct CandyBar
{
char brand[20];
float weight;
unsigned int calorie;
};
int main()
{
CandyBar*pc = new CandyBar[3];
strcpy_s(pc[0].brand, "Mocha Munch");
pc[0].weight = 2.3;
pc[0].calorie = 350;
strcpy_s(pc[1].brand, "Hershey bar");
pc [1].weight = 24.2;
pc [1].calorie = 550;
strcpy_s(pc[2].brand, "Musketeers");
pc [2].weight = 2.6;
pc [2].calorie = 430;
cout << "My 1st CandyBar is " << pc->brand << "." << endl;
cout << "And its weight is " << pc->weight << ", calorie is " << pc->calorie;
cout << "." << endl;
cout << "My 2st CandyBar is " << (pc+1)->brand << "." << endl;
cout << "And its weight is " << (pc+1)->weight << ", calorie is " <<( pc+1)->calorie;
cout << "." << endl;
cout << "My 3st CandyBar is " << (pc+2)->brand << "." << endl;
cout << "And its weight is " << (pc+2)->weight << ", calorie is " << (pc+2)->calorie;
cout << "." << endl;
delete[]pc;
return 0;
}
// 4.10 跑步成绩
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<float, 3> record_list;
float average;
cout << "Please input three record of 40 miles.\n";
cout << "First record: ";
cin >> record_list[0];
cout << "Second record: ";
cin >> record_list[1];
cout << "Third record: ";
cin >> record_list[2];
cout << "ok, you input:\n1." << record_list[0] << "\n2." << record_list[1] << "\n3." << record_list[2] << endl;
average = (record_list[0] + record_list[1] + record_list[2]) / 3;
cout << "Congratylate, your average performance is " << average << ".";
return 0;
}
第四章终于敲完了
假期冲冲冲