signed

QiShunwang

“诚信为本、客户至上”

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

2021/5/14 21:07:11   来源:

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

实例7.1

#include<iostream>
void simple();

int main()
{
	using namespace std;
	cout << "main() will call the simple() function:\n";
	simple();
	cout << "main() is finished with the simple() function.\n";
	system("pause");
	return 0;
}

void simple()
{
	using namespace std;
	cout << "I'm but a simple function.\n";
}

实例7.2

#include<iostream>
void cheers(int);
double cube(double x);
int main()
{
	using namespace std;
	cheers(5);
	cout << "Give me a number:";
	double side;
	cin >> side;
	double volume = cube(side);
	cout << "A " << side << " foot cube has a volume of ";
	cout << volume << " cubic feet.\n";
	cheers(cube(2));
	system("pause");
	return 0;
}

void cheers(int n)
{
	using namespace std;
	for (int i = 0; i < n; i++)
		cout << "Cheers!";
	cout << endl;
}

double cube(double x)
{
	return x * x * x;
}

实例7.3

#include<iostream>
using namespace std;
void n_chars(char, int);
int main()
{
	int times;
	char ch;

	cout << "Enter a character:";
	cin >> ch;
	while (ch != 'q')
	{
		cout << "Enter an integer:";
		cin >> times;
		n_chars(ch, times);
		cout << "\nEnter another character or press the"
			" q-key to quit:";
		cin >> ch;
	}
	cout << "The value of times is " << times << ".\n";
	cout << "Bye\n";
	system("pause");
	return 0;
}

void n_chars(char c, int n)
{
	while (n-- > 0)
		cout << c;
}

实例7.4

#include<iostream>
long double probability(unsigned numbers, unsigned pick);
int main()
{
	using namespace std;
	double total, choices;
	cout << "Enter the total number of choices on the game card and\n"
		"the number of picks allowed:\n";
	while ((cin >> total >> choices) && choices <= total)
	{
		cout << "You have one chance in ";
		cout << probability(total, choices);
		cout << " of winning.\n";
		cout << "Next two numbers (q to quit):";
	}
	cout << "bye\n";
	system("pause");
	return 0;
}

long double proability(unsigned numbers, unsigned picks)
{
	long double result = 1.0;
	long double n;
	unsigned p;

	for (n = numbers, p = picks; p > 0; n--, p--)
		result = result * n / p;
	system("pasue");
	return result;
}

实例7.5

#include<iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n);
int main()
{
	using namespace std;
	int cookies[ArSize] = { 1,2,4,8,16,32,64,128 };

	int sum = sum_arr(cookies, ArSize);
	cout << "Total cookies eaten: " << sum << "\n";
	system("pause");
	return 0;
}

int sum_arr(int arr[], int n)
{
	int total = 0;
	for (int i = 0; i < n; i++)
		total = total + arr[i];
	return total;
}

实例7.6

#include<iostream>
const int ArSize = 8;
int sum_arr(int arr[], int n);

int main()
{
	int cookies[ArSize] = { 1,2,4,8,16,32,64,128 };
	std::cout << cookies << " = array address, ";
	std::cout << sizeof cookies << " = sizeof cookies\n";
	int sum = sum_arr(cookies, ArSize);
	std::cout << "Total cookies eaten: " << sum << std::endl;
	sum = sum_arr(cookies,3);
	std::cout << "First three eaters ate " << sum << " cookies.\n";
	sum = sum_arr(cookies + 4, 4);
	std::cout << "Last four eaters ate " << sum << " cookies.\n";
	system("pause");
	return 0;
}

int sum_arr(int arr[], int n)
{
	int total = 0;
	std::cout << arr << " = arr";

	std::cout << sizeof arr << " = sizeof arr\n";
	for (int i = 0; i < n; i++)
		total = total + arr[i];
	return total;
}

实例7.7

#include<iostream>
const int Max = 5;
int fill_array(double ar[], int limit);
void show_array(const double ar[], int n);
void revalue(double r, double ar[], int n);

int main()
{
	using namespace std;
	double properties[Max];

	int size = fill_array(properties, Max);
	show_array(properties, size);
	if (size > 0)
	{
		cout << "Enter revaluation factor:";
		double factor;
		while (!(cin >> factor))
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;Please enter a number:";
		}
		revalue(factor, properties, size);
		show_array(properties, size);
	}
	cout << "Done.\n";
	cin.get();
	cin.get();
	return 0;
}

int fill_array(double ar[], int limit)
{
	using namespace std;
	double temp;
	int i;
	for (i = 0; i < limit; i++)
	{
		cout << "Enter value #" << (i + 1) << ": ";
		cin >> temp;
		if (!cin)
		{
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input;input process terminated.\n";
			break;
		}
		else if (temp < 0)
			break;
		ar[i] = temp;
	}
	return i;
}

void show_array(const double ar[], int n)
{
	using namespace std;
	for (int i = 0; i < n; i++)
	{
		cout << "Property #" << (i + 1) << ":$";
		cout << ar[i] << endl;
	}
}

void revalue(double r, double ar[], int n)
{
	for (int i = 0; i < n; i++)
		ar[i] *= r;
}

实例7.8

#include<iostream>
const int ArSize = 8;
int sum_arr(const int * begin, const int * end);
int main()
{
	using namespace std;
	int cookies[ArSize] = { 1,2,4,8,16,32,64,128 };
	int sum = sum_arr(cookies, cookies + ArSize);
	cout << "Total cookies eaten:" << sum << endl;
	sum = sum_arr(cookies, cookies + 3);
	cout << "First three eaters ate " << sum << " cookies.\n";
	sum = sum_arr(cookies + 4, cookies + 8);
	cout << "Last four eaters ate " << sum << " cookies.\n";
	system("pause");
	return 0;
}

int sum_arr(const int *begin, const int *end)
{
	const int *pt;
	int total = 0;

	for (pt = begin; pt != end; pt++)
		total = total + *pt;
	return total;
}

实例7.9

#include<iostream>
unsigned int c_in_str(const char* str, char ch);
int main()
{
	using namespace std;
	char mmm[15] = "minimum";

	const char *wail = "ululate";
	unsigned int ms = c_in_str(mmm, 'm');
	unsigned int us = c_in_str(wail, 'u');
	cout << ms << " m chararcters in " << mmm << endl;
	cout << us << " u characters in " << wail << endl;
	system("pause");
	return 0;
}

unsigned int c_in_str(const char* str, char ch)
{
	unsigned int count = 0;
	
	while (*str)
	{
		if (*str == ch)
			count++;
		str++;
	}
	return count;
}

实例7.10

#include<iostream>
char *buildstr(char c, int n);
int main()
{
	using namespace std;
	int times;
	char ch;

	cout << "Enter a character:";
	cin >> ch;
	cout << "Enter an integer:";
	cin >> times;
	char *ps = buildstr(ch, times);
	cout << ps << endl;
	delete[] ps;
	ps = buildstr('+', 20);
	cout << ps << "-Done-" << ps << endl;
	delete[] ps;
	system("pause");
	return 0;
}

char *buildstr(char c, int n)
{
	char *pstr = new char[n + 1];
	pstr[n] = '\0';
	while (n-- > 0)
		pstr[n] = c;
	return pstr;
}

实例7.11

#include<iostream>
struct travel_time
{
	int hours;
	int mins;
};
const int Mins_per_hr = 60;

travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);

int main()
{
	using namespace std;
	travel_time day1 = { 5,45 };
	travel_time day2 = { 4,55 };

	travel_time trip = sum(day1, day2);
	cout << "Two day total:";
	show_time(trip);

	travel_time day3 = { 4,32 };
	cout << "Three-day total:";
	show_time(sum(trip, day3));

	system("pause");
	return 0;
}

travel_time sum(travel_time t1, travel_time t2)
{
	travel_time total;

	total.mins = (t1.mins + t2.mins) % Mins_per_hr;
	total.hours = t1.hours + t2.hours + (t1.mins+ t2.mins) / Mins_per_hr;

	return total;
}

void show_time(travel_time t)
{
	using namespace std;
	cout << t.hours << " hours, "
		<< t.mins << " minutes\n";
}

实例7.12

#include<iostream>
#include<cmath>

struct polar
{
	double distance;
	double angle;
};

struct rect
{
	double x;
	double y;
};

polar rect_to_polar(rect xypos);
void show_polar(polar dapos);

int main()
{
	using namespace std;
	rect rplace;
	polar pplace;

	cout << "Enter the x and y values:";
	while (cin >> rplace.x >> rplace.y)
	{
		pplace = rect_to_polar(rplace);
		show_polar(pplace);
		cout << "Next two numbers(q to quit):";
	}
	cout << "Done.\n";
	system("pause");
	return 0;
}

polar rect_to_polar(rect xypos)
{
	using namespace std;
	polar answer;

	answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
	answer.angle = atan2(xypos.y, xypos.x);
	return answer;
}

void show_polar(polar dapos)
{
	using namespace std;
	const double Rad_to_deg = 57.29577951;

	cout << "distance = " << dapos.distance;
	cout << ",angle = " << dapos.angle * Rad_to_deg;
	cout << " degress\n";
}

实例7.13

#include<iostream>
#include<cmath>

struct polar
{
	double distance;
	double angle;
};

struct rect
{
	double x;
	double y;
};

void rect_to_polar(const rect* pxy, polar *pda);
void show_polar(const polar *pda);

int main()
{
	using namespace std;
	rect rplace;
	polar pplace;
	cout << "Enter the x and y values:";
	while (cin >> rplace.x >> rplace.y)
	{
			rect_to_polar(&rplace, &pplace);
			show_polar(&pplace);
			cout << "Next two numbers (q to quit):";
	}
	cout << "Done.\n";
	system("pause");
	return 0;
}

void show_polar(const polar *pda)
{
	using namespace std;
	const double Rad_to_deg = 57.29577951;

	cout << "distance" << pda->distance;
	cout << ",angle = " << pda->angle * Rad_to_deg;
	cout << " degrees\n";
}

void rect_to_polar(const rect * pxy, polar *pda)
{
	using namespace std;
	pda->distance = sqrt(pxy->x * pxy->x + pxy->y * pxy->y);
	pda->angle = atan2(pxy->y, pxy->x);
}

实例7.14

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

const int SIZE = 5;
void display(const string sa[], int n);
int main()
{
	string list[SIZE];
	cout << "Enter your " << SIZE << " favourite astronomical sights.\n";
	for (int i = 0; i < SIZE; i++)
	{
		cout << i + 1 << ":";
		getline(cin, list[i]);
	}

	cout << "Your list:\n";
	display(list, SIZE);

	system("pause");
	return 0;
}

void display(const string sa[], int n)
{
	for (int i = 0; i < n; i++)
		cout << i + 1 << ":" << sa[i] << endl;
}

实例7.15

#include<iostream>
#include<array>
#include<string>

const int Seasons = 4;
const std::array<std::string, Seasons>Snames = { "Spring","Summer","Fall","Winter" };

void fill(std::array<double, Seasons> *pa);
void show(std::array<double, Seasons> da);

int main()
{
	std::array<double, Seasons> expenses;
	fill(&expenses);
	show(expenses);
	system("pause");
	return 0;
}

void fill(std::array<double, Seasons> *pa)
{
	using namespace std;
	for (int i = 0; i < Seasons; i++)
	{
		cout << "Enter " << Snames[i] << " expenses:";
		cin >> (*pa)[i];
	}
}

void show(std::array<double, Seasons> da)
{
	using namespace std;
	double total = 0.0;
	cout << "\nEXPENSES\n";
	for (int i = 0; i < Seasons; i++)
	{
		cout << Snames[i] << ":$" << da[i] << endl;
		total += da[i];
	}
	cout << "Total Expenses:$" << total << endl;
}

实例7.16

#include<iostream>
void countdown(int n);

int main()
{
	countdown(4);
	system("pause");
	return 0;
}

void countdown(int n)
{
	using namespace std;
	cout << "Counting down…" << n << endl;
	if (n > 0)
		countdown(n - 1);
	cout << n << ":Kaboom!\n";
}

实例7.17

#include<iostream>

const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int level);
int main()
{
	char ruler[Len];
	int i;
	for (i = 1; i < Len - 2; i++)
		ruler[i] = ' ';
	ruler[Len - 1] = '\0';
	int max = Len - 2;
	int min = 0;
	ruler[min] = ruler[max] = '|';
	std::cout << ruler << std::endl;
	for (i = 1; i <= Divs; i++)
	{
		subdivide(ruler, min, max, i);
		std::cout << ruler << std::endl;
		for (int j = 1; j < Len - 2; j++)
			ruler[j] = ' ';
	}
	std::cin.get();
	system("pouse");
	return 0;
}

void subdivide(char ar[], int low, int high, int level)
{
	if (level == 0)
		return;
	int mid = (high + low) / 2;
	ar[mid] = '|';
	subdivide(ar, low, mid, level - 1);
	subdivide(ar, mid, high, level - 1);
}

实例7.18

#include<iostream>
double betsy(int);
double pam(int);

void estimate(int lines, double(*pf)(int));

int main()
{
	using namespace std;
	int code;
	cout << "How many lines of code do you need?";
	cin >> code;
	cout << "Here's Betsy's estimate:\n";
	estimate(code, betsy);
	cout << "Here's Pam's estimate:\n";
	estimate(code, pam);
	system("pause");
	return 0;
}

double betsy(int lns)
{
	return 0.05*lns;
}

double pam(int lns)
{
	return 0.03*lns + 0.0004*lns*lns;
}

void estimate(int lines, double(*pf)(int))
{
	using namespace std;
	cout << lines << " lines will take ";
	cout << (*pf)(lines) << " hour(s)\n";
}

实例7.19

#include<iostream>

const double *f1(const double ar[], int n);
const double *f2(const double[], int);
const double *f3(const double *, int);

int main()
{
	using namespace std;
	double av[3] = { 1112.3,1542.6,2227.9 };

	const double *(*p1)(const double*, int) = f1;
	auto p2 = f2;

	cout << "Using pointers to functions:\n";
	cout << " Address Value\n";
	cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl;
	cout << p2(av, 3) << ": " << *p2(av, 3) << endl;

	const double *(*pa[3])(const double*, int) = { f1,f2,f3 };

	auto pb = pa;

	cout << "\nUsing an array of pointers to functions:\n";
	cout << " Address Value\n";
	for (int i = 0; i < 3; i++)
		cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl;
	cout << "\nUsing a pointer to a pointer to a function:\n";
	cout << " Address Value\n";
	for (int i = 0; i < 3; i++)
		cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl;

	cout << "\nUsing pointers to an array of pointers:\n";
	cout << " Address Value\n";

	auto pc = &pa;

	cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << endl;

	const double *(*(*pd)[3])(const double *, int) = &pa;

	const double *pdb = (*pd)[1](av, 3);
	cout << pdb << ": " << *pdb << endl;

	cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd)[2])(av, 3) << endl;

	system("pause");
	return 0;
}

const double *f1(const double * ar, int n)
{
	return ar;
}
const double *f2(const double ar[], int n)
{
	return ar + 1;
}
const double *f3(const double ar[], int n)
{
	return ar + 2;
}