C++ Primer Plus 第六版 第四章编程练习答案参考(仅仅用作学习交流,若有错误,请联系作者,谢谢)

第一题:

#include <iostream>
#include <string>
using namespace std;
int main()
{   
	char fname[20],lname[20];
	char grade;
	int age;
	cout<<"What is your first name? ";
	cin.getline(fname,20);
	cout<<"What is your last name? ";
	cin.getline(lname,20);
	cout<<"what letteer grade do yo deserve? ";
	cin>>grade;
	cout<<"What is your age? ";
	cin>>age;
	cout<<"Name: "<<lname<<", "<<fname<<endl;
	cout<<"Grade: "<<char(grade+1)<<endl;
	cout<<"Age: "<<age<<endl;
	return 0;
} 

第二题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	const int Arsize=20;
	string name;
	string dessert;
	cout<<"Enter you 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()
{
	const int Arsize=20;
	char fname[Arsize];
	char lname[Arsize];
	cout<<"Enter your first name: ";
    cin>>fname;
    cin.get();
	cout<<"Enter your last name: ";
	cin>>lname;
	strcat(lname,", ");
	cout<<"Here's information in a single string: "<<lname<<fname<<endl;
	return 0;
}

第四题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string fname,lname;
	cout<<"Enter your first name: ";
    getline(cin,fname);
	cout<<"Enter your last name: ";
	getline(cin,lname);
	lname=lname+", ";
	cout<<"Here's information in a single string: "<<lname<<fname<<endl;
	return 0;

}

第五题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	struct CandyBar
	{
		string name;
		double weight;
		int cal;
	};
	CandyBar snack={"Mocha Munch",2.3,350};
	cout<<"name: "<<snack.name<<" weight: "<<snack.weight<<" calories: "<<snack.cal<<endl;
	return 0;
}

第六题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	struct CandyBar
	{
		string name;
		double weight;
		int cal;
	};
	CandyBar member[3]=
	{
		{"Baidu",1.1,100},
		{"Alibaba",1.2,200},
		{"Tenxun",1.3,300}
	};
	cout<<"name: "<<member[0].name<<" weight: "<<member[0].weight<<" calories: "<<member[0].cal<<endl;
	cout<<"name: "<<member[1].name<<" weight: "<<member[1].weight<<" calories: "<<member[1].cal<<endl;
	cout<<"name: "<<member[2].name<<" weight: "<<member[2].weight<<" calories: "<<member[2].cal<<endl;
	return 0;
}

第七题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	struct Pizza
	{
		string name;
		double rayon;
		double weight;
	};
	Pizza one;
	cout<<"Enter the name: ";
	getline(cin,one.name);
	cout<<"Enter the rayon: ";
	cin>>one.rayon;
	cout<<"Enter the weight: ";
	cin>>one.weight;
	cout<<"The name: "<<one.name<<" rayon: "<<one.rayon<<" weight: "<<one.weight<<endl;
	return 0;
}

第八题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	struct Pizza
	{
		string name;
		double rayon;
		double weight;
	};
	Pizza *p=new Pizza;
	cin>>p->rayon;
	cin.get();//处理换行符 
	cout<<"Enter the name: ";
	getline(cin,p->name);
	cout<<"Enter the weight: ";
	cin>>p->weight;
	cout<<"The name: "<<p->name<<" rayon: "<<p->rayon<<" weight: "<<p->weight<<endl;
	delete p;
	return 0;
}

第九题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	struct CandyBar
	{
		string name;
		double weight;
		int cal;
	};
	/*CandyBar *p=new CandyBar[3]
	{
		{"Baidu",1.1,100},
		{"Alibaba",1.2,200},
		{"Tenxun",1.3,300}
	};*/ 
	//c++11 可以直接初始化 
	CandyBar *p=new CandyBar[3];
	cin>>p->name;
	cin>>p->weight;
	cin>>p->cal;
	cin.get();
	cin>>(p+1)->name;
	cin>>(p+1)->weight;
	cin>>(p+1)->cal;
	cin.get();
	cin>>(p+2)->name;
	cin>>(p+2)->weight;
	cin>>(p+2)->cal;
	cout<<"name: "<<p->name<<" weight: "<<p->weight<<" calories: "<<p->cal<<endl;
	cout<<"name: "<<(p+1)->name<<" weight: "<<(p+1)->weight<<" calories: "<<(p+1)->cal<<endl;
	cout<<"name: "<<(p+2)->name<<" weight: "<<(p+2)->weight<<" calories: "<<(p+2)->cal<<endl;
	return 0;
}



第十题:

#include <iostream>
//#include <array>
using namespace std;
int main()
{
	/*array<float, 3> grade;
	float sum=0;
	cout<<"Enter first grade: ";
	cin>>grade[0];
	cout<<"Enter second grade: ";
	cin>>grade[1];
	cout<<"Enter last grade: ";
	cin>>grade[2];*/
	float grade[3],sum=0;
	cin>>grade[0];
	cin>>grade[1];
	cin>>grade[2];
	sum=sum+grade[0]+grade[1]+grade[2];
	cout<<"Times: 3 average grade is: "<<sum/3<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值