C++ Primer Plus(第六版)第四章课后习题
4.13.1
#include
int main()
{
using namespace std;
const int fixed=30;
char first_name[fixed],last_name[fixed];
char grade;
int age;
cout << "What is your first name? ";
cin.get( first_name,fixed).get();
cout << "What is your last name? ";
cin.get( last_name,fixed);
cout << "What letter grade do you deserve? ";
cin >> grade;
cout << "What is your age? ";
cin >> age;
cout << "Name: " << last_name << “,” << first_name << endl;
grade++;
cout << "Grade: "<< grade <<endl;
cout << "Age: " << age;
return 0;
}
4.13.2
#include
#include
int main()
{
using namespace std;
string name, dessert;
cout << “Enter your name:\n”;
getline(cin,name);
cout << “Enter your favorite dessert:\n”;
getline(cin,dessert);
cout << " I have some delicious " << dessert << " for you, "<< name << endl;
return 0;
}
4.13.3
#include
#include
int main()
{
using namespace std;
char first_name[20],last_name[20];
cout <<"Enter your first name: ";
cin.getline(first_name,20);
cout <<"Enter your last name: ";
cin.getline (last_name,20);
cout <<"Here’s the information in a single string: “<< last_name <<” " <<first_name<<endl;
return 0;
}
4.13.4
#include
#include
int main()
{
using namespace std;
string first_name,last_name,name;
cout << "Enter your first name: ";
getline(cin,first_name);
cout << "Enter your last name: “;
getline(cin, last_name);
name=last_name+”, "+first_name;
cout << "Here’s the information in a single string: "<<name <<endl;
return 0;
}
4.13.5
#include
#include
int main()
{
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
CandyBar snack={“Mocha Munch”,2.3,350};
cout << " The brand of candy is "<< snack.brand << endl;
cout << " The weigt of candy is " << snack.weight << endl;
cout << " The calorie of candy is " << snack.calorie <<endl;
return 0;
}
4.13.6
#include
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar snack[3]={{"a",33.5,222},{"b",55.3,666},{"c",22.1,442}};
cout << " The brand of candy is "<< snack[0].brand<< endl;
cout << " The weigt of candy is " << snack[1].weight <<endl;
cout << " The calorie of candy is " << snack[2].calorie <<endl;
return 0;
}
4.13.7
#include
#include
int main()
{
using namespace std;
struct pizza
{
string name;
double diameter;
int weight;
};
pizza A;
cout <<"Enter the name of company: ";
getline(cin, A.name);
cout <<"Enter the diameter of pizza: ";
cin >>A.diameter;
cout << "Enter the weight of pizza: ";
cin >> A.weight ;
cout << " The name of company is “<< A.name<<”,the diameter of pizza is "<< A.diameter
<< ", the weight of pizza is "<< A.weight <<endl;
return 0;
}
4.13.8
#include
struct pizza
{
char brand[20];
double diameter;
int weight;
};
int main()
{
using namespace std;
pizza * point= new pizza;
cout << "Enter the diameter of pizza:";
cin >> point->diameter ;
cin.get();
cout << "Enter the name of pizza company:";
cin.getline(point->brand ,20);
cout << "Enter the weight of pizza:";
cin >> point->weight;
cout << "wo zhenshuai";
cout << "The brand of pizza company is " << (*point)brand <<", the diameter of pizza is "<<point->diameter<<", and the weight of pizza is " << point->weight<<endl;
delete point;
return 0;
}
4.13.9
#include
const int NUM=3;
struct CandyBar
{
char brand[20];
double weight;
int calorie;
};
int main()
{
using namespace std;
CandyBar *snack=new CandyBar[NUM];
for(int i=0;i<NUM;i++)
{
cout << "Enter the brand of candy: ";
cin.getline(snack[i].brand, 20);
cout << "Enter the weight of candy: ";
cin >> snack[i].weight;
cout << "Enter the calorie of candy: ";
cin >> snack[i].calorie;
cin.get();
}
cout << " The brand of candy is "<< snack[0].brand<< endl;
cout << " The weigt of candy is " << snack[1].weight <<endl;
cout << " The calorie of candy is " << snack[2].calorie <<endl;
return 0;
}
4.13.10
#include
#include
int main()
{
using namespace std;
array<double,3> grade;
cout << “Enter the first grade:”;
cin >> grade[0];
cout <<“Enter the second grade:”;
cin >> grade[1];
cout << “Enter the third grade:”;
cin >>grade[2];
double even=(grade[0]+grade[1]+grade[2])/3.0;
cout <<"The even of grades is "<< even<< endl;
return 0;
}