码迷,mamicode.com
首页 > 编程语言 > 详细

C++Primer Plus第6版 4.13编程练习答案

时间:2016-05-06 15:26:55      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:


1、答案:
#include <iostream>
#include <string>


int main()
{
using namespace std;
char* fname = new char[10];
char* lname = new char[6];
char grade;
int age;
cout<< "What is your first name? ";
cin.getline(fname,10);
cout<< "What is your last name? ";
cin.getline(lname,6);
cout<< "What letter grade do you deserve? ";
cin>>grade;
cout<< "What is your age? ";
cin>>age;
cout <<"Name: "<<lname<<", "<<fname<<endl;
cout <<"Grade: "<<char(grade+1)<<endl;
cout <<"Age: "<<age;


delete [] fname;
delete [] lname;
return 0;

}

2、答案:



#include <iostream>
#include <string>


int main()
{
using namespace std;
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;
}

3、答案:



#include <iostream>
#include <cstring>


int main()
{
using namespace std;
char* fname = new char [15];
char* lname = new char [8];


cout << "Enter your first name: ";
cin.getline(fname,5);
cout<<"Enter your last name:";
cin.getline(lname,8);
strcat(fname,",");
strcat(fname,lname);
cout << "Here‘s the information in a single string: "
<<fname;
delete [] fname;
delete [] lname;
return 0;
}

4、答案:

#include <iostream>
#include <string>


int main()
{
using namespace std;
string fname;
string lname;


cout << "Enter your first name: ";
getline(cin,fname);
cout<<"Enter your last name:";
getline(cin,lname);
fname +=", "; 
fname += lname;
cout << "Here‘s the information in a single string: "
<<fname;


return 0;
}

5、答案

#include <iostream>
#include <string>


using namespace std;


struct CandyBar
{
string brand;
float weight;
int carlo;


};


int main()
{
CandyBar snack = {"Mocha Munch",2.3,350};
cout<<"brand = "<<snack.brand<<endl
<<"weight = "<<snack.weight<<endl
<<"carlo = "<<snack.carlo<<endl;
return 0;

}

6、答案

#include <iostream>
#include <string>


using namespace std;


struct CandyBar
{
string brand;
float weight;
int carlo;


};


int main()
{
CandyBar snack[3] = {{"Mocha Munch",2.3,350},{"Mocha Middle",2.0,300},{"Mocha Little",3.2,600}};
for(int i=0;i<3;i++)
cout<<"snack[i].brand = "<<snack[i].brand<<endl
<<"snack[i].weight = "<<snack[i].weight<<endl
<<"snack[i].carlo = "<<snack[i].carlo<<endl;
return 0;

}

7、答案

#include <iostream>
#include <string>


using namespace std;


struct pizaInfo
{
string coname;
int diameter;
float weight;


};


int main()
{

pizaInfo  p1;
cout << "Enter the infomation:\n";
cout << "Corpotion name:";
getline(cin,p1.coname);
cout <<"piza diameter: ";
cin>>p1.diameter;
cout << "Piza weight: ";
cin >>p1.weight;
cout<<"\n\n"<<"Corpotion name:"<<p1.coname<<endl
<<"piza diameter:"<<p1.diameter<<endl
<<"Piza weight: "<<p1.weight;
}

8、答案

#include <iostream>
#include <string>


using namespace std;


struct pizaInfo
{
string coname;
int diameter;
float weight;


};


int main()
{

pizaInfo* ps = new pizaInfo;


cout << "Enter the infomation:\n";
cout <<"piza diameter: ";
cin>>ps->diameter;
cout << "Corpotion name:";
cin>>ps->coname;


cout << "Piza weight: ";
cin >>ps->weight;
cout<<"\n\n"<<"Corpotion name:"<<ps->coname<<endl
<<"piza diameter:"<<ps->diameter<<endl
<<"Piza weight: "<<ps->weight;
delete ps;


}

9、答案

#include <iostream>
#include <string>


using namespace std;

struct CandyBar
{
string brand;
float weight;
int carlo;
};

int main()
{
CandyBar* snack = new CandyBar[3];
snack[0].brand = "Mocha Munch";
snack[0].carlo= 2.3;
snack[0].weight = 350;
snack[1].brand = "Mocha Middle";
snack[1].carlo= 2.0;
snack[1].weight = 300;
snack[2].brand = "Mocha Little";
snack[2].carlo= 3.2;
snack[2].weight = 600;


for(int i=0;i<3;i++)
cout<<"snack[i].brand = "<<snack[i].brand<<endl
<<"snack[i].weight = "<<snack[i].weight<<endl
<<"snack[i].carlo = "<<snack[i].carlo<<endl;


delete [] snack;


return 0;

}

10、答案

#include <iostream>
#include <array>


using namespace std;


int main()
{
array<float,3> scores;
cout<<"Please enter three times score of 40-meters-run: \n";
for(int i =0;i<3;i++)
{
cout<<"The "<<i<<" time score:";
cin>>scores[i];
}

cout << "The average of scores is :"
<<(scores[0]+scores[1]+scores[2])/3;


return 0;


}

C++Primer Plus第6版 4.13编程练习答案

标签:

原文地址:http://blog.csdn.net/walkman_lfq/article/details/51321214

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!