C++ Premier Plus丨编程练习答案丨第四章

C++ Premier Plus丨编程练习答案丨第四章

#include<iostream>
#include<string>
#include<cstring>
#include<array>
using namespace std;
int main(){
	//4_1
	/*string firstName = "";
	string lastName = "";
	char grade = 'a';
	int age = 0;
	cout<<"Input firstname = ";
	getline(cin,firstName);
	cout<<"Input lastName = ";
	getline(cin,lastName);
	cout<<"Grade = ";
	cin>>grade;
	cout<<"Age = ";
	cin>>age;
	cout<<"FEEDBACK 4_1"<<endl;
	cout<<"Name = "<<firstName<<" "<<lastName<<endl;
	cout<<"Grade = "<<char(grade+'B'-'A')<<endl;//without CAST TYPE, output will be a (int)
	//cout<<"Grade = "<<char(grade + 1)<<endl;//Also correct
	cout<<"Age = "<<age<<endl;*/
	//4_2
	/*const int ArSize = 20;
	string name = "";
	string dessert = "";
	cout<<"Name = ";
	getline(cin, name);
	cout<<"Dessert = ";
	getline(cin,dessert);
	cout<<"I have some delicious "<<dessert<<" for you, "<<name<<"."<<endl;*/
	//4_3
	/*const char CONNECT[] = ", ";
	char firstName[20] = {};
	char lastName[20] = {};
	char wholeName[45] = {};
	cout<<"input first name = ";
	cin>>firstName;
	cout<<"input lastName = ";
	cin>>lastName;
	cout<<"FEEDBACK 4_3";
	cout<<"your whole name = ";
	strcpy(wholeName, lastName);
	strcat(wholeName, CONNECT);
	strcat(wholeName, firstName);
	cout<<"your whole name = "<<wholeName<<endl;*/
	//4_4
	/*string firstName = "";
	string lastName = "";
	cout<<"FirstName = ";
	cin>>firstName;
	cout<<"LastName = ";
	cin>>lastName;
	cout<<"FEEDBACK 4_4"<<endl;
	cout<<lastName + ", " + firstName<<endl;*/
	//4_5 and 4_6
	/*struct CandyBar{
		string brand;
		double weight;
		int calorie;
	};
	CandyBar snack = {
		"Mocha Munch",
		2.3,
		350
	};
	cout<<"FEEDBACK 4_5"<<endl;
	cout<<"snack's brand is "<<snack.brand<<endl;
	cout<<"snack's weight is "<<snack.weight<<endl;
	cout<<"snack's calorie is "<<snack.calorie<<endl;
	cout<<"FEEDBACK 4_6"<<endl;
	CandyBar arrCdB[3] = {
		{"candy first", 1.1, 1},
		{"candy second", 2.2, 2},
		{"candy third", 3.3, 3}
	};
	cout<<"Candy Bar ARRAY: "<<endl;
	cout<<"[0]: "<<arrCdB[0].brand<<" , "<<arrCdB[0].weight<<" , "<<arrCdB[0].calorie<<endl;
	cout<<"[1]: "<<arrCdB[1].brand<<" , "<<arrCdB[1].weight<<" , "<<arrCdB[1].calorie<<endl;
	cout<<"[2]: "<<arrCdB[2].brand<<" , "<<arrCdB[2].weight<<" , "<<arrCdB[2].calorie<<endl;*/
	//4_7
	/*struct Pizza{
		char name[20];
		int diameter;
		int weight;
	};
	Pizza user;
	cout<<"Input Name = ";
	cin.getline(user.name, 20);
	cout<<"Input Diameter = ";
	cin>>user.diameter;
	cout<<"Input Weight = ";
	cin>>user.weight;
	cout<<"FEEDBACK 4_7";
	cout<<"User's Pizza have these information:"<<endl;
	cout<<"name = "<<user.name<<endl;
	cout<<"diameter = "<<user.diameter<<endl;
	cout<<"weight = "<<user.weight<<endl;*/
	//4_8
	/*struct Pizza{
		char name[20];
		int diameter;
		int weight;
	};
	Pizza* pUser = new Pizza;
	cout<<"Input Diameter = ";
	cin>>pUser->diameter;
	cin.get();//ATTENTION! If cin NUMBER then cin.getline(,), have to put a CIN.GET in between!
	cout<<"Input Name = ";
	cin.getline(pUser->name, 20);
	cout<<"Input Weight = ";
	cin>>pUser->weight;
	cout<<"FEEDBACK 4_8";
	cout<<"User's Pizza have these information:"<<endl;
	cout<<"name = "<<pUser->name<<endl;
	cout<<"diameter = "<<pUser->diameter<<endl;
	cout<<"weight = "<<pUser->weight<<endl;
	delete pUser;*/
	//4_9
	/*struct CandyBar
	{
		string brand;
		float weight;
		int calorie;
	};
		CandyBar* snack = new CandyBar;//注意这个 *,new CandyBar是创建了一个CandyBar指针而不是一个CandyBar 所以没有 * 就会类型无法转换
		snack->brand = "Mocha Munch";
		snack->weight = 2.3;
		snack->calorie = 350;
		cout<<"CanduBar* snack = new CandyBar"<<endl;
		cout<<"snack->brand is "<<snack->brand<<endl;
		cout<<"snack->weight is "<<snack->weight<<endl;
		cout<<"(*snack).calorie is "<<(*snack).calorie<<endl;//注意这里 想要用.而不是指针的->来引用时,需要加括号!不然运算符优先级会让它出现编译错误
		delete snack;
		*/
	//4_10
	/*const int times = 3;
	array<int, times> grade;
	cout<<"Input grade1 = ";
	cin>>grade[0];
	cout<<"Input grade2 = ";
	cin>>grade[1];
	cout<<"Input grade3 = ";
	cin>>grade[2];
	cout<<"FEEDBACK 4_10"<<endl;
	cout<<"Times = "<<times<<endl;
	cout<<"Average Grade = "<<(grade[0]+grade[1]+grade[2])/(double)times<<endl;*/



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值