第4章复合类型-创新互联

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
//猜测应用到指针的运算,不确定答案

创新互联建站长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为洮北企业提供专业的网站设计制作、网站设计,洮北网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
#includeint main()
{using namespace std;
	char firname[15], lastname[10], lettergrade, outgrade;
	unsigned int age;
	cout<< "What is your first name? ";
	cin >>firname;
	cout<< "What is your lastname? ";
	cin >>lastname;
	cout<< "What letter grade do you deserve? ";
	cin >>lettergrade;
	switch (lettergrade) {case 'A':
			outgrade = 'B';
		case'B':
			outgrade = 'C';
		case'C':
			outgrade = 'D';
		case'D':
			outgrade = 'A';
	}
	cout<< "What is your age? ";
	cin >>age;
	cout<< "Name: "<< lastname<< ", "<< firname<< endl
		<< "Grade: "<< outgrade<< endl
		<< "Age: "<< age<< endl;
	return 0;
}

2023/1/16更新如下:

#include#includeint main()
{using namespace std;
	string firname, lastname;
	char lettergrade;
	unsigned int age;
	cout<< "What is your first name? ";
	cin >>firname;
	cout<< "What is your lastname? ";
	cin >>lastname;
	cout<< "What letter grade do you deserve? ";
	cin >>lettergrade;
	cout<< "What is your age? ";
	cin >>age;
	cout<< "Name: "<< lastname<< ", "<< firname<< endl
		<< "Grade: "<< ++lettergrade<< endl
		<< "Age: "<< age<< endl;
	return 0;
}
二 cin.getline(name,ArSize);报错(待解)

在这里插入图片描述

//instr2.cpp -- reading more than one word with getline
#include#includeint main()
{using namespace std;
	const int ArSize = 20;
	string name, dessert;
	cout<< "Enter your name:\n";
	getline(cin,name);
	cout<< "Enter your favorite dessert:\n";
	getline(cin,dessert);
	cout<< "I hace some delious "<< dessert
		<< " for you, "<< name<< ".\n";
	return 0;
}

使用cin.getline(name,ArSize);会报错,待解;截图如下
在这里插入图片描述
在这里插入图片描述

三 cstring

在这里插入图片描述
在这里插入图片描述

#include#includeint main()
{using namespace std;
	char firstname[15],lastname[10];
	cout<< "Enter your first name: ";
	cin.getline(firstname, 15);
	cout<< "Enter your last name: ";
	cin.getline(lastname,15);
	cout<< "Here's the infoamation in a single: "<< lastname<< ", "<< firstname<
四 string

在这里插入图片描述

#include#includeint main()
{using namespace std;
	string firstname, lastname;
	cout<< "Enter your first name: ";
	cin >>firstname;
	cout<< "Enter your lastname: ";
	cin >>lastname;
	cout<< "Here's the information in a single string: "<< lastname<< ", "<< firstname;

	return 0;
}
五 struct

在这里插入图片描述

#includeusing namespace std;

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

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

	return 0;
}
六 struct+数组⑤

在这里插入图片描述

#includeusing namespace std;

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

int main()
{CandyBar snack[3]
	{{"Mocha Munch",2.3,350 },
		{"Rose Water",1.2,600},
		{"Apple grow",3.0,250}
	};

	cout<< snack[0].brand<< ", "<< snack[0].weight<< ", "<< snack[0].calorie<
七 struct

在这里插入图片描述
在这里插入图片描述

#includeusing namespace std;

struct pizza{string company;
	float length;
	float weight;
};

int main()
{pizza quest1;
	cout<< "Enter the name of pizza company: ";
	cin >>quest1.company;
	cout<< "Enter the length of pizza(cm): ";
	cin >>quest1.length;
	cout<< "Enter the weight of pizza(g): ";
	cin >>quest1.weight;
	
	cout<< "Company: "<< quest1.company<< endl
		<< "length: "<< quest1.length<<"cm"<
八 new⑦

在这里插入图片描述

#includeusing namespace std;

struct pizza {string company;
	float length;
	float weight;
};

int main()
{pizza *quest1=new pizza;
	cout<< "Enter the length of pizza(cm): ";
	cin >>quest1->length;
	cout<< "Enter the name of pizza company: ";
	cin >>quest1->company;
	cout<< "Enter the weight of pizza(g): ";
	cin >>quest1->weight;

	cout<< "Company: "<< quest1->company<< endl
		<< "length: "<< quest1->length<< "cm"<< endl
		<< "weight: "<< quest1->weight<< "g";
	delete quest1;

	return 0;
}
九 new⑥

在这里插入图片描述

#includeusing namespace std;

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

int main()
{CandyBar* snack = new CandyBar[3]
	{{"Mocha Munch",2.3,350 },
		{"Rose Water",1.2,600},
		{"Apple grow",3.0,250}
	};

	cout<< snack[0].brand<< ", "<< snack[0].weight<< ", "<< snack[0].calorie<< endl;
	cout<< snack[1].brand<< ", "<< snack[1].weight<< ", "<< snack[1].calorie<< endl;
	cout<< snack[2].brand<< ", "<< snack[2].weight<< ", "<< snack[2].calorie<< endl;
	delete [] snack;

	return 0;
}
十 array

在这里插入图片描述

#include#include

int main() {using namespace std;
	arrayscore;
	cout<< "Enter the first score(s/100m): ";
	cin >>score[0];
	cout<< "Enter the second score(s/100m): ";
	cin >>score[1];
	cout<< "Enter the third score(s/100m): ";
	cin >>score[2];
	cout<< "Times: 3"<< endl
		<< "Average score: "<< (score[0] + score[1] + score[2]) / 3<< "s/100m";

	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站名称:第4章复合类型-创新互联
分享路径:http://hbruida.cn/article/cecocc.html