【C++】C++ primer plus 编程练习—第4章

//4-1

#include<iostream>
#include<string> 

int main()
{
	using namespace std;
	string fname,lname;
	char gra;
	int  age;
	
	cout<<"what is your first name?"<<endl;
	getline(cin,fname);
	
	cout<<"what is yuor last name?"<<endl;
	getline(cin,lname);
	
	cout<<"what letter grade do yuo deserve?"<<endl;
	cin>>gra;
	gra++;
	
	cout<<"what is yuor age?"<<endl;
	cin>>age;
	
	cout<<"name :"<<lname<<","<<fname<<endl;;
	cout<<"grade:"<<gra<<endl;
	cout<<"age  :"<<age<<endl;
	
	return 0;
}

//4-2

#include<iostream>
#include<string> 

int main()
{
	using namespace std;
	string name,dessert;
	
	
	cout<<"Enter your name:"<<endl;
	getline(cin,name);
	
	cout<<"Enter your favorite dessert"<<endl;
	getline(cin,dessert);
	
	cout<<"I have some delicious "<<dessert;
	cout<<" for you,"<<name<<'.'<<endl;
	
	return 0;
}

//4-3

#include<iostream>
#include<cstring>

int main()
{
	using namespace std;
	const int size=20;
	char fname[size],lname[size],name[size*2];
	
	cout<<"Enter your first name:";
	cin>>fname;
	
	cout<<"Enter your last name:";
	cin>>lname;
	
	strcpy(name,lname);
	strcat(name,",");
	strcat(name,fname);
	
	cout<<"Here is a information of a single string:"<<name<<endl;
	
	return 0;
}

//4-4

#include<iostream>
#include<string>

int main()
{
	using namespace std;
	string fname,lname,name;
	
	cout<<"Enter your first name:";
	getline(cin,fname);
	
	cout<<"Enter your last name:";
	getline(cin,lname);
	
	name=lname+","+fname;
	
	cout<<"Here is a information of a single string:"<<name<<endl;
	
	return 0;
}

//4-5

#include<iostream>
#include<string>
using namespace std;

struct CandyBar
{
	string brand;
	double weigh;
	int calorie;
};

int main()
{
	CandyBar snack=
	{
		"mocha munch",
		2.3,
		350
	};
	
	cout<<snack.brand<<endl;
	cout<<snack.weigh<<endl;
	cout<<snack.calorie<<endl;
	
	return 0;
}

//4-6

#include<iostream>
#include<string>
using namespace std;

struct CandyBar
{
	string brand;
	double weigh;
	int calorie;
};

int main()
{
	CandyBar snack[3]=
	{
		{"mocha A",2.3,350},
		{"mocha B",2.4,354},
		{"mocha C",2.6,358},
	};
	cout<<"the information of snack#1"<<endl;
	cout<<"brand:   "<<snack[0].brand<<endl;
	cout<<"weight:  "<<snack[0].weigh<<endl;
	cout<<"calorie: "<<snack[0].calorie<<endl;
	
	cout<<"\nthe information of snack#2"<<endl;
	cout<<"brand:   "<<snack[1].brand<<endl;
	cout<<"weight:  "<<snack[1].weigh<<endl;
	cout<<"calorie: "<<snack[1].calorie<<endl;
	
	cout<<"\nthe information of snack#3"<<endl;
	cout<<"brand:   "<<snack[2].brand<<endl;
	cout<<"weight:  "<<snack[2].weigh<<endl;
	cout<<"calorie: "<<snack[2].calorie<<endl;
	
	return 0;
}

//4-7

#include<iostream>
#include<string>
using namespace std;

struct Pizza
{
	string brand;
	double diameter;
	double weight;
};

int main()
{
	Pizza snack;
	cout<<"enter this pizaa's brand:";
	getline(cin,snack.brand);
	cout<<"enter this pizza's diameter:";
	cin>>snack.diameter;
	cout<<"enter this snack's weight:";
	cin>>snack.weight; 
	
	cout<<snack.brand<<endl;
	cout<<snack.diameter<<endl;
	cout<<snack.weight<<endl;
	
	return 0;
}

//4-8

#include<iostream>
#include<string>
using namespace std;

struct Pizza
{
	string brand;
	double diameter;
	double weight;
};

int main()
{
	Pizza* ps=new Pizza;
	
	cout<<"enter this pizaa's brand:";
	getline(cin,ps->brand);
	cout<<"enter this pizza's diameter:";
	cin>>ps->diameter;
	cout<<"enter this snack's weight:";
	cin>>ps->weight; 
	
	cout<<"brand   :"<<ps->brand<<endl;
	cout<<"diameter:"<<ps->diameter<<endl;
	cout<<"weight  :"<<ps->weight<<endl;
	delete ps;
	
	return 0;
}

//4-9

#include<iostream>
#include<string>
using namespace std;

struct CandyBar
{
	string brand;
	double weigh;
	int calorie;
};

int main()
{
	CandyBar * p =new CandyBar[3] ;
	*p=	{"mocha A",2.3,350};
	*(p+1)=	{"mocha B",2.4,354};
	p[2]={"mocha C",2.6,358};
	
	cout<<"the information of snack#1"<<endl;
	cout<<"brand:   "<<p[0].brand<<endl;
	cout<<"weight:  "<<p[0].weigh<<endl;
	cout<<"calorie: "<<p[0].calorie<<endl;
	
	cout<<"\nthe information of snack#2"<<endl;
	cout<<"brand:   "<<(p+1)->brand<<endl;
	cout<<"weight:  "<<(p+1)->weigh<<endl;
	cout<<"calorie: "<<(p+1)->calorie<<endl;
	
	cout<<"\nthe information of snack#3"<<endl;
	cout<<"brand:   "<<p[2].brand<<endl;
	cout<<"weight:  "<<p[2].weigh<<endl;
	cout<<"calorie: "<<p[2].calorie<<endl;
	
	delete [] p;
	
	return 0;
}

//4-10

#include<iostream>
#include<array>
using namespace std;

int main()
{
	array<double, 3> vole;
	cout<<"volecity#1: ";
	cin >> vole[0];
	cout << "volecity#2: ";
	cin >> vole[1];
	cout << "volecity#3: ";
	cin >> vole[2];

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值