第四章
第一题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct info
{
string FirstName;
string LastName;
char LetterGrade;
int age;
};
info stu {};//Only available with -std = c++11
cout<<"What is your first name? ";
getline(cin,stu.FirstName);
cout<<"What is your last name? ";
getline(cin,stu.LastName);
cout<<"What letter grade do you deserve? ";
cin>>stu.LetterGrade;
cout<<"What is your age? ";
cin>>stu.age;
cout<<"Name: "<<stu.LastName<<", "<<stu.FirstName<<endl;
cout<<"Grade: "<<(char)(stu.LetterGrade+1)<<endl;
cout<<"Age: "<<stu.age;
}
第二题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
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;
}
第三题:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char FirstName[20];
char LastName[20];
cout<<"Enter your first name: ";
cin.getline(FirstName,20);
cout<<"Enter your last name: ";
cin.getline(LastName,20);
cout<<"Here's the information in a single string: "
<<LastName<<", "<<FirstName;
}
第四题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string FirstName;
string LastName;
cout<<"Enter your first name: ";
getline(cin,FirstName);
cout<<"Enter your last name: ";
getline(cin,LastName);
cout<<"Here's the information in a single string: "
<<LastName<<", "<<FirstName;
}
第五题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct CandyBar
{
string brand;
double weight;
int calorie;
};
CandyBar snack{"Mocha Munch",2.3,350};//only available with -std = c++11
cout<<"Name: snack"<<endl;
cout<<"Brand: "<<snack.brand<<endl;
cout<<"Weight: "<<snack.weight<<endl;
cout<<"Calorie: "<<snack.calorie<<endl;
}
第六题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct CandyBar
{
string brand;
double weight;
int calorie;
};
CandyBar snack[3]=
{
{"Mocha Munch",2.3,350},
{"Cheese Cake",2.5,450},
{"Nougat",1.7,265}
};
cout<<"Name: snack"<<endl;
//The following code can be realized by loop which will be talked about in Ch.5
cout<<"Brand: "<<snack[0].brand<<endl;
cout<<"Weight: "<<snack[0].weight<<endl;
cout<<"Calorie: "<<snack[0].calorie<<endl<<endl;
cout<<"Brand: "<<snack[1].brand<<endl;
cout<<"Weight: "<<snack[1].weight<<endl;
cout<<"Calorie: "<<snack[1].calorie<<endl<<endl;
cout<<"Brand: "<<snack[2].brand<<endl;
cout<<"Weight: "<<snack[2].weight<<endl;
cout<<"Calorie: "<<snack[2].calorie<<endl;
}
第七题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct info
{
string company;
double diameter;
double weight;
};
info Pizza;
cout<<"Please enter the name of the company: ";
getline(cin,Pizza.company);
cout<<"Please enter the diameter: ";
cin>>Pizza.diameter;
cout<<"Please enter the weight: ";
cin>>Pizza.weight;
cout<<"Company: "<<Pizza.company<<endl;
cout<<"Diameter: "<<Pizza.diameter<<endl;
cout<<"Weight: "<<Pizza.weight<<endl;
}
第八题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct info
{
string company;
double diameter;
double weight;
};
info *Pizza=new info;
//In the following part, (*Pizza).xxxx can be replaced by Pizza->xxxx
cout<<"Please enter the diameter: ";
cin>>(*Pizza).diameter;
cin.get();//Necessary! P77 explains this problem.
cout<<"Please enter the name of the company: ";
getline(cin,(*Pizza).company);
cout<<"Please enter the weight: ";
cin>>(*Pizza).weight;
cout<<"Company: "<<(*Pizza).company<<endl;
cout<<"Diameter: "<<(*Pizza).diameter<<endl;
cout<<"Weight: "<<(*Pizza).weight<<endl;
delete Pizza;
}
第九题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct CandyBar
{
string brand;
double weight;
int calorie;
};
CandyBar *snack=new CandyBar[3];
cout<<"Name: snack"<<endl;
cout<<"Please enter the brand: ";
getline(cin,snack[0].brand);
cout<<"Please enter the weight: ";
cin>>snack[0].weight;
cout<<"Please enter the calorie: ";
cin>>snack[0].calorie;
cin.get();
cout<<"Please enter the brand: ";
getline(cin,snack[1].brand);
cout<<"Please enter the weight: ";
cin>>snack[1].weight;
cout<<"Please enter the calorie: ";
cin>>snack[1].calorie;
cin.get();
cout<<"Please enter the brand: ";
getline(cin,snack[2].brand);
cout<<"Please enter the weight: ";
cin>>snack[2].weight;
cout<<"Please enter the calorie: ";
cin>>snack[2].calorie;
cin.get();
cout<<"Brand: "<<snack[0].brand<<endl;
cout<<"Weight: "<<snack[0].weight<<endl;
cout<<"Calorie: "<<snack[0].calorie<<endl<<endl;
cout<<"Brand: "<<snack[1].brand<<endl;
cout<<"Weight: "<<snack[1].weight<<endl;
cout<<"Calorie: "<<snack[1].calorie<<endl<<endl;
cout<<"Brand: "<<snack[2].brand<<endl;
cout<<"Weight: "<<snack[2].weight<<endl;
cout<<"Calorie: "<<snack[2].calorie<<endl;
}
第十题:
//Tips: If you use dev c++ as the IDE,
//click Tools-Compile Options-Add the command when compiling: -std=c++11
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<double,3> FortyMeters;
cout<<"Please enter the time of 40m running(three time): "<<endl;
cin>>FortyMeters[0];
cin>>FortyMeters[1];
cin>>FortyMeters[2];
double ave=0;
ave=(FortyMeters[0]+FortyMeters[1]+FortyMeters[2])/3;
cout<<"1: "<<FortyMeters[0]<<endl;
cout<<"2: "<<FortyMeters[1]<<endl;
cout<<"3: "<<FortyMeters[2]<<endl;
cout<<"Avergae: "<<ave<<endl;
}