C++PrimerPlus(第6版)中文版第四章实例代码

C++PrimerPlus(第6版)中文版第四章实例代码

实例4.1

#include<iostream>
int main()
{
	using namespace std;
	int yams[3];
	yams[0] = 7;
	yams[1] = 8;
	yams[2] = 6;
	
	int yamcosts[3] = { 20,30,5 };

	cout << "Total yams = ";
	cout << yams[0] + yams[1] + yams[2] << endl;
	cout << "The package with " << yams[1] << " yams costs ";
	cout << yamcosts[1] << " cents per yam.\n";
	int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1];
	total = total + yams[2] * yamcosts[2];
	cout << "The total yam expense is " << total << " cents.\n";

	cout << "\nSize of yam array = " << sizeof yams;
	cout << " bytes.\n";
	cout << "Size of one element = " << sizeof yams[0];
	cout << " bytes.\n";
	system("pause");
	return 0;
}

实例4.2

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	const int Size = 15;
	char name1[Size];
	char name2[Size] = "C++owboy";

	cout << "Howdy!I'm " << name2;
	cout << "!What's your name?\n";
	cin >> name1;
	cout << "Well," << name1 << ",your name has ";
	cout << strlen(name1) << " letters and is stored\n";
	cout << "in an array of " << sizeof name1 << " bytes.\n";
	cout << "Your initial is " << name1[0] << ".\n";
	name2[3] = '\0';
	cout << "Here are the first 3 charachters of my name:";
	cout << name2 << endl;
	system("pause");
	return 0;
}

实例4.3

#include<iostream>
int main()
{
	using namespace std;
	const int Arsize = 20;
	char name[Arsize];
	char dessert[Arsize];

	cout << "Enter your name:\n";
	cin >> name;
	cout << "Enter your favourite dessert:\n";
	cin >> dessert;
	cout << "I have some delicious " << dessert;
	cout << " for you," << name << ".\n";
	system("pause");
	return 0;
}

实例4.4

#include<iostream>
int main()
{
	using namespace std;
	const int Arsize = 20;
	char name[Arsize];
	char dessert[Arsize];

	cout << "Enter your name.\n";
	cin.getline(name, Arsize);
	cout << "Enter your favourite dessert.\n";
	cin.getline(dessert, Arsize);
	cout << "I have some delicious " << dessert;
	cout << " for you," << name << ".\n";
	system("pause");
	return 0;
}

实例4.5

#include<iostream>
int main()
{
	using namespace std;
	const int Arsize = 20;
	char name[Arsize];
	char dessert[Arsize];

	cout << "Enter your name.\n";
	cin.get(name, Arsize).get();
	cout << "Enter your favorite dessert.\n";
	cin.get(dessert, Arsize).get();
	cout << "I have some delicious " << dessert;
	cout << " for you," << name << ".\n";
	system("pause");
	return 0;
}

实例4.6

#include<iostream>
int main()
{
	using namespace std;
	cout << "What year was your house built>\n";
	int year;
	cin >> year;
	cout << "What is its street address?\n";
	char address[80];
	cin.getline(address, 80);
	cout << "Year built:" << year << endl;
	cout << "Address:" << address << endl;
	cout << "Done!\n";
	system("pause");
	return 0;
}

实例4.7

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	char charr1[20];
	char charr2[20] = "jaguar";
	string str1;
	string str2 = "panther";

	cout << "Enter a kind of feline:";
	cin >> charr1;
	cout << "Enter another kind of feline:";
	cin >> str1;
	cout << "Here are some felines:\n";
	cout << charr1 << " " << charr2 << " "
		<< str1 << " " << str2
		<< endl;
	cout << "The third letter in " << charr2 << " is "
		<< charr2[2] << endl;
	cout << "The third letter in " << str2 << " is "
		<< str2[2] << endl;
	system("pause");
	return 0;
}

实例4.8

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	string s1 = "penguin";
	string s2, s3;

	cout << "You can assign one string object to another:s2 = s1\n";
	s2 = s1;
	cout << "s1: " << s1 << ",s2:" << s2 << endl;
	cout << "You can assign a C-style string to a string object.\n";
	cout << "s2 = \"buzzard\"\n";
	s2 = "buzzard";
	cout << "s2:" << s2 << endl;
	cout << "You can concatenate strings:s3 = s1 +s2\n";
	s3 = s1 + s2;
	cout << "s3:" << s3 << endl;
	cout << "You can append strings.\n";
	s1 += s2;
	cout << "s1 += s2 yields s1 = " << s1 << endl;
	s2 += " for a day";
	cout << "s2 += \" for a day\" yileds s2 = " << s2 << endl;
	system("pause");
	return 0;
}

实例4.9

#include<iostream>
#include<string>
#include<cstring>
/*
	由于使用vs2017编译该程序可能存在需要使用strcpy_s和strcat_s等函数
*/
int main()
{
	using namespace std;
	char charr1[20];
	char charr2[20] = "jaguar";
	string str1;
	string str2 = "panther";

	str1 = str2;
	strcpy(charr1, charr2);

	str1 += " paste";
	strcat(charr1, " juice");

	int len1 = str1.size();
	int len2 = strlen(charr1);

	cout << "The string " << str1 << " contains " << len1 << " characters.\n";
	cout << "The string " << charr1 << " contains " << len2 << " characters.\n";

	system("pause");
	return 0;
}

实例4.10

#include<iostream>
#include<string>
#include<cstring>
int main()
{
	using namespace std;
	char charr[20];
	string str;

	cout << "Length of string in charr before input:" << strlen(charr) << endl;
	cout << "Length of string in str before input:" << str.size() << endl;
	cout << "Enter a line of text:\n";
	cin.getline(charr,20);
	cout << "You entered:" << charr << endl;
	cout << "Enter another line of text:\n";
	getline(cin, str);
	cout << "Length of string in charr after input:" << strlen(charr) << endl;
	cout << "Length of string in str after input:" << str.size() << endl;
	system("pause");
	return 0;
}

实例4.11

#include<iostream>
struct inflatable 
{
	char name[20];
	float volume;
	double price;
};

int main()
{
	using namespace std;
	inflatable guest =
	{
		"Glorious Gloria",
		1.88,
		29.99
	};
	inflatable pal =
	{
		"Audacious Arthur",
		3.12,
		32.99
	};

	cout << "Expand your guest list with " << guest.name;
	cout << " and " << pal.name << "!\n";

	cout << "You can have both for $";
	cout << guest.price + pal.price << "!\n";
	system("pause");
	return 0;
}

实例4.12

#include<iostream>
struct inflatable
{
	char name[20];
	float volume;
	double price;
};
int main()
{
	using namespace std;
	inflatable bouquet =
	{
		"sunflowers",
		0.20,
		12.49
	};
	inflatable choice;
	cout << "bouquet:" << bouquet.name << " for $";
	cout << bouquet.price << endl;

	choice = bouquet;
	cout << "choice:" << choice.name << " for $";
	cout << choice.price << endl;
	system("pause");
	return 0;
}

实例4.13

#include<iostream>
struct inflatable
{
	char name[20];
	float volume;
	double price;
};
int main()
{
	using namespace std;
	inflatable guests[2] = 
	{
		{"Bambi",0.5,21.99},
		{"Godzilla",2000,565.99}
	};

	cout << "The guests " << guests[0].name << " and " << guests[1].name << "\nhave a combined volume of " << guests[0].volume + guests[1].volume << " cubic feet.\n";
	system("pause");
	return 0;
}

实例4.14

#include<iostream>
int main()
{
	using namespace std;
	int donuts = 6;
	double cups = 4.5;

	cout << "donuts value = " << donuts;
	cout << " and donuts address = " << &donuts << endl;


	cout << "cups value = " << cups;
	cout << " and cups address = " << &cups << endl;
	system("pause");
	return 0;
}

实例4.15

#include<iostream>
int main()
{
	using namespace std;
	int updates = 6;
	int *p_updates;
	p_updates = &updates;


	cout << "Values:updates = " << updates;
	cout << ",*p_updates = " << *p_updates << endl;


	cout << "Addresses:&updates = " << &updates;
	cout << ",p_updates = " << p_updates << endl;

	*p_updates = *p_updates + 1;
	cout << "Now updates = " << updates << endl;
	system("pause");
	return 0;
}

实例4.16

#include<iostream>
int main()
{
	using namespace std;
	int higgens = 5;
	int *pt = &higgens;
	
	cout << "Value of higgens = " << higgens << ";Address of higgens = " << &higgens << endl;
	cout << "Value of *pt = " << *pt << ";Value of pt = " << pt << endl;
	system("pause");
	return 0;
}

实例4.17

#include<iostream>
int main()
{
	using namespace std;
	int nights = 1001;
	int *pt = new int;
	*pt = 1001;

	cout << "nights value = ";
	cout << nights << ":location " << &nights << endl;
	cout << "int ";
	cout << "value = " << *pt << ":location = " << pt << endl;
	double *pd = new double;
	*pd = 10000001.0;

	cout << "double ";
	cout << "value = " << *pd << ":location = " << pd << endl;
	cout << "location of pointer pd:" << &pd << endl;
	cout << "size of pt = " << sizeof(pt);
	cout << ":size of *pt = " << sizeof(*pt) << endl;
	cout << "size of pd = " << sizeof pd;
	cout << ":size of *pd = " << sizeof(*pd) << endl;

	delete pt;
	delete pd;

	system("pause");
	return 0;
}

实例4.18

#include<iostream>
int main()
{
	using namespace std;
	double *p3 = new double[3];
	p3[0] = 0.2;
	p3[1] = 0.5;
	p3[2] = 0.8;
	cout << "p3[1] is " << p3[1] << ".\n";
	p3 = p3 + 1;
	cout << "Now p3[0] is " << p3[0] << " and ";
	cout << "p3[1] is " << p3[1] << ".\n";
	p3 = p3 - 1;
	delete[] p3;
	system("pause");
	return 0;
}

实例4.19

#include<iostream>
int main()
{
	using namespace std;
	double wages[3] = { 10000.0,20000.0,30000.0 };
	short stacks[3] = { 3,2,1 };


	double *pw = wages;
	short *ps = &stacks[0];

	cout << "pw = " << pw << ",*pw = " << *pw << endl;
	pw = pw + 1;
	cout << "add 1 to the pw pointer:\n";
	cout << "pw = " << pw << ",*pw=" << *pw << "\n\n";
	cout << "ps = " << ps << ",*ps = " << *ps << endl;
	ps = ps + 1;
	cout << "add 1 to the ps pointer:\n";
	cout << "ps = " << ps << ",*ps = " << *ps << "\n\n";

	cout << "access two elements with array notation\n";
	cout << "statcks[0] = " << stacks[0] << ",stacks[1] = " << stacks[1] << endl;
	cout << "access two elements with pointer notation\n";
	cout << "*stacks = " << *stacks << ",*(stacks + 1) = " << *(stacks + 1) << endl;

	cout << sizeof(wages) << " = size of wages array\n";
	cout << sizeof(pw) << " = size of pw pointer\n";
	system("pause");
	return 0;
}

实例4.20

#include<iostream>
#include<cstring>
#pragma warning(disable:4996)					//不进行4996错误的报告
int main()
{
	using namespace std;
	char animal[20] = "bear";
	const char *bird = "wren";
	char *ps;

	cout << animal << " and ";
	cout << bird << "\n";

	cout << "Enter a kind of animal:";
	cin >> animal;

	ps = animal;
	cout << ps << "!\n";
	cout << "Before using strcpy():\n";
	cout << animal << " at " << (int *)animal << endl;
	cout << ps << " at " << (int *)ps << endl;

	ps = new char[strlen(animal) + 1];
	strcpy(ps, animal);
	cout << "After using strcpy():\n";
	cout << animal << " at " << (int *)animal << endl;
	cout << ps << " at " << (int *)ps << endl;
	delete[] ps;
	system("pause");
	return 0;
}

实例4.21

#include<iostream>
struct inflatable
{
	char name[20];
	float volume;
	double price;
};
int main()
{
	using namespace std;
	inflatable *ps = new inflatable;
	cout << "Enter name of inflatable item:";
	cin.get(ps->name, 20);
	cout << "Enter volume in cubic feet:";
	cin >> (*ps).volume;
	cout << "Enter price:$";
	cin >> ps->price;
	cout << "Name:" << (*ps).name << endl;
	cout << "Volume:" << ps->volume << " cubic feet\n";
	cout << "Price:%" << ps->price << endl;
	delete ps;
	system("pause");
	return 0;
}

实例4.22

#include<iostream>
#include<cstring>
#pragma warning(disable:4996)						//不进行4996错误的报告
using namespace std;
char *getname(void);
int main()
{
	char *name;

	name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;

	name = getname();
	cout << name << " at " << (int *)name << "\n";
	delete[] name;
	system("pause");
	return 0;
}

char *getname()
{
	char temp[80];
	cout << "Enter last name:";
	cin >> temp;
	char *pn = new char[strlen(temp) + 1];
	strcpy(pn, temp);
	
	return pn;
}

实例4.23

#include<iostream>

struct antarctica_years_end
{
	int year;
};

int main()
{
	antarctica_years_end s01, s02, s03;
	s01.year = 1998;
	antarctica_years_end *pa = &s02;
	pa->year = 1999;
	antarctica_years_end trio[3];
	trio[0].year = 2003;
	std::cout << trio->year << std::endl;
	const antarctica_years_end *arp[3] = { &s01,&s02,&s03 };
	std::cout << arp[1]->year << std::endl;
	const antarctica_years_end **ppa = arp;
	auto ppb = arp;

	std::cout << (*ppa)->year << std::endl;
	std::cout << (*(ppb + 1))->year << std::endl;
	system("pause");
	return 0;
}

实例4.24

#include<iostream>
#include<vector>
#include<array>
int main()
{
	using namespace std;
	//C,original C++
	double a1[4] = { 1.2,2.4,3.6,4.8 };
	//C++98 STL
	vector<double> a2(4);
	//no simple way to initialize in C98
	a2[0] = 1.0 / 3.0;
	a2[1] = 1.0 / 5.0;
	a2[2] = 1.0 / 7.0;
	a2[3] = 1.0 / 9.0;
	//C++11 -- create and initialize array object
	array<double, 4> a3 = { 3.14,2.72,1.62,1.41 };
	array<double, 4> a4;
	a4 = a3;
	//use array notation
	cout << "a1[2]:" << a1[2] << " at " << &a1[2] << endl;
	cout << "a2[2]:" << a2[2] << " at " << &a2[2] << endl;
	cout << "a3[2]:" << a3[2] << " at " << &a3[2] << endl;
	cout << "a4[2]:" << a4[2] << " at " << &a4[2] << endl;
	//missed
	a1[-2] = 20.2;
	cout << "a1[-2]:" << a1[-2] << " at " << &a1[-2] << endl;
	cout << "a3[2]:" << a3[2] << " at " << &a3[2] << endl;
	cout << "a4[2]:" << a4[2] << " at " << &a3[2] << endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值