C++ Premier Plus丨编程练习答案丨第四章
#include<iostream>
#include<string>
#include<cstring>
#include<array>
using namespace std;
int main(){
//4_1
/*string firstName = "";
string lastName = "";
char grade = 'a';
int age = 0;
cout<<"Input firstname = ";
getline(cin,firstName);
cout<<"Input lastName = ";
getline(cin,lastName);
cout<<"Grade = ";
cin>>grade;
cout<<"Age = ";
cin>>age;
cout<<"FEEDBACK 4_1"<<endl;
cout<<"Name = "<<firstName<<" "<<lastName<<endl;
cout<<"Grade = "<<char(grade+'B'-'A')<<endl;//without CAST TYPE, output will be a (int)
//cout<<"Grade = "<<char(grade + 1)<<endl;//Also correct
cout<<"Age = "<<age<<endl;*/
//4_2
/*const int ArSize = 20;
string name = "";
string dessert = "";
cout<<"Name = ";
getline(cin, name);
cout<<"Dessert = ";
getline(cin,dessert);
cout<<"I have some delicious "<<dessert<<" for you, "<<name<<"."<<endl;*/
//4_3
/*const char CONNECT[] = ", ";
char firstName[20] = {};
char lastName[20] = {};
char wholeName[45] = {};
cout<<"input first name = ";
cin>>firstName;
cout<<"input lastName = ";
cin>>lastName;
cout<<"FEEDBACK 4_3";
cout<<"your whole name = ";
strcpy(wholeName, lastName);
strcat(wholeName, CONNECT);
strcat(wholeName, firstName);
cout<<"your whole name = "<<wholeName<<endl;*/
//4_4
/*string firstName = "";
string lastName = "";
cout<<"FirstName = ";
cin>>firstName;
cout<<"LastName = ";
cin>>lastName;
cout<<"FEEDBACK 4_4"<<endl;
cout<<lastName + ", " + firstName<<endl;*/
//4_5 and 4_6
/*struct CandyBar{
string brand;
double weight;
int calorie;
};
CandyBar snack = {
"Mocha Munch",
2.3,
350
};
cout<<"FEEDBACK 4_5"<<endl;
cout<<"snack's brand is "<<snack.brand<<endl;
cout<<"snack's weight is "<<snack.weight<<endl;
cout<<"snack's calorie is "<<snack.calorie<<endl;
cout<<"FEEDBACK 4_6"<<endl;
CandyBar arrCdB[3] = {
{"candy first", 1.1, 1},
{"candy second", 2.2, 2},
{"candy third", 3.3, 3}
};
cout<<"Candy Bar ARRAY: "<<endl;
cout<<"[0]: "<<arrCdB[0].brand<<" , "<<arrCdB[0].weight<<" , "<<arrCdB[0].calorie<<endl;
cout<<"[1]: "<<arrCdB[1].brand<<" , "<<arrCdB[1].weight<<" , "<<arrCdB[1].calorie<<endl;
cout<<"[2]: "<<arrCdB[2].brand<<" , "<<arrCdB[2].weight<<" , "<<arrCdB[2].calorie<<endl;*/
//4_7
/*struct Pizza{
char name[20];
int diameter;
int weight;
};
Pizza user;
cout<<"Input Name = ";
cin.getline(user.name, 20);
cout<<"Input Diameter = ";
cin>>user.diameter;
cout<<"Input Weight = ";
cin>>user.weight;
cout<<"FEEDBACK 4_7";
cout<<"User's Pizza have these information:"<<endl;
cout<<"name = "<<user.name<<endl;
cout<<"diameter = "<<user.diameter<<endl;
cout<<"weight = "<<user.weight<<endl;*/
//4_8
/*struct Pizza{
char name[20];
int diameter;
int weight;
};
Pizza* pUser = new Pizza;
cout<<"Input Diameter = ";
cin>>pUser->diameter;
cin.get();//ATTENTION! If cin NUMBER then cin.getline(,), have to put a CIN.GET in between!
cout<<"Input Name = ";
cin.getline(pUser->name, 20);
cout<<"Input Weight = ";
cin>>pUser->weight;
cout<<"FEEDBACK 4_8";
cout<<"User's Pizza have these information:"<<endl;
cout<<"name = "<<pUser->name<<endl;
cout<<"diameter = "<<pUser->diameter<<endl;
cout<<"weight = "<<pUser->weight<<endl;
delete pUser;*/
//4_9
/*struct CandyBar
{
string brand;
float weight;
int calorie;
};
CandyBar* snack = new CandyBar;//注意这个 *,new CandyBar是创建了一个CandyBar指针而不是一个CandyBar 所以没有 * 就会类型无法转换
snack->brand = "Mocha Munch";
snack->weight = 2.3;
snack->calorie = 350;
cout<<"CanduBar* snack = new CandyBar"<<endl;
cout<<"snack->brand is "<<snack->brand<<endl;
cout<<"snack->weight is "<<snack->weight<<endl;
cout<<"(*snack).calorie is "<<(*snack).calorie<<endl;//注意这里 想要用.而不是指针的->来引用时,需要加括号!不然运算符优先级会让它出现编译错误
delete snack;
*/
//4_10
/*const int times = 3;
array<int, times> grade;
cout<<"Input grade1 = ";
cin>>grade[0];
cout<<"Input grade2 = ";
cin>>grade[1];
cout<<"Input grade3 = ";
cin>>grade[2];
cout<<"FEEDBACK 4_10"<<endl;
cout<<"Times = "<<times<<endl;
cout<<"Average Grade = "<<(grade[0]+grade[1]+grade[2])/(double)times<<endl;*/
return 0;
}