鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c++ > >

C++PrimerPlus第七章课后练习

来源:互联网 作者:佚名 时间:2016-07-01 16:52
C++PrimerPlus第七章课后练习 无 /**********第七章练习**********/#include iostreamusing namespace std;// applicant should be defined before f_7_12_12struct applicant{char name[30];int credit_ratings[3];};struct box{char maker[40];float height
C++PrimerPlus第七章课后练习 <无>
/**********
第七章练习
**********/

#include <iostream>

using namespace std;

// applicant should be defined before f_7_12_12
struct applicant
{
	char name[30];
	int credit_ratings[3];
};

struct box
{
	char maker[40];
	float height;
	float width;
	float length;
	float volume;
};

const int SLEN = 30;
struct student
{
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};

void f_7_12_4();
void f_7_12_8(char *, char, char);
void f_7_12_12(applicant, applicant *);
void f_7_12_13(applicant *, applicant *);
void f_7_13_1();
void f_7_13_2();
// return double array
void f_7_13_2_input(double *, int);
// display array data
void f_7_13_2_display(const double *, int);
// cal average and return
double f_7_13_2_cal(const double *, int);
void f_7_13_3();
void f_7_13_4();
void f_7_13_5();
long long f_7_13_5_recursive(int);
void f_7_13_6();
void f_7_13_6_fill(double *, int);
void f_7_13_6_reverse(double *, int);
void f_7_13_6_show(const double *, int);
void f_7_13_7();
void f_7_13_7_fill(double *, double *);
void f_7_13_7_refactor(double *, double *, double r);
void f_7_13_7_show(double *, double *);
double f_7_13_7_get();
void f_7_13_8();
long double probability(unsigned int numbers, unsigned int picks);
void display_box(box b);
void set_box(box * bp);
double h_mean(double, double);
void f_7_13_9();
void f_7_13_9_getinfo(student pa[], int n);
void f_7_13_9_display1(student st);
void f_7_13_9_display2(const student * pst);
void f_7_13_9_display3(const student pa[], int n);
void f_7_13_10();
void f_7_13_10_caller(double, double, double (*pf)(double,double));
double f_7_13_10_add1(double x, double y);
double f_7_13_10_add2(double x, double y);

void f1(applicant * a);
const char * f2(const applicant * a1, const applicant * a2);



// compiler says main must return int
int main()
{
	// f_7_12_4();
	// char str[50] = "poliorg clears pw daily";  // char * str probes constant conversion warning while fails compilation same to directly passing cstring as parameter
	// f_7_12_8(str, 'r', 'g');
	applicant ap1 = {"freddy", {1,2,3}};
	// struct does not allow multi assignation
	applicant ap2 = {"milkyway", {4,5,6}};
	// f_7_12_12(ap1, &ap2);
	// f_7_12_13(&ap1, &ap2);
	// f_7_13_1();
	// f_7_13_2();
	// f_7_13_3();
	// f_7_13_4();
	// f_7_13_5();
	// f_7_13_6();
	// f_7_13_7();
	// f_7_13_8();
	// f_7_13_9();
	f_7_13_10();
	return 0;
}

void f_7_12_4()
{
	int values[10] = {1,2,3,4,5,6,7,8,9,0};
	int * pStart = values;
	int * pEnd = values+10;
	const int fixed_value = 99;

	while(pStart < pEnd)
	{
		*pStart = fixed_value;
		pStart++;
	}

	for(int i = 0; i < 10; i++)
	{
		cout << "values[" << i << "]=" << values[i] << endl;
	}
}

void f_7_12_8(char * str, char c1, char c2)
{
	int ct = 0;
	char * pt = str; // keeps initial position
	// cout takes '\0' as the end but while(str) will not do so 
	while(*str != '\0')
	{
		if(*str == c1)
		{
			*str = c2;
			ct++;
		}
		str++;
	}
	cout << " replaced str now = " << pt << endl;
	cout << " total replaced count = " << ct << endl;

}

void f_7_12_12(applicant ap, applicant * apos)
{
	cout << "applicant name by value:" << ap.name << endl;
	cout << "applicant name by position:" << apos->name << endl;
	cout << "applicant ratings by value:" ;
	for(int i = 0; i < 3; i++)
	{
		cout << ap.credit_ratings[i] << ",";
	}
	cout << endl;
	cout << "applicant ratings by position:" ;
	for(int i = 0; i < 3; i++)
	{
		cout << apos->credit_ratings[i] << ",";
	}
	cout << endl;
}

void f_7_12_13(applicant * a1, applicant * a2)
{
	// pointer to f1 and f2
	void (*p1)(applicant * a) = f1;
	const char * (*p2)(const applicant * a1, const applicant * a2) = f2;

	// 
	void (*ap[5])(applicant * a) = {f1,f1,f1,f1,f1};
	const char * (*pa[10])(const applicant * a1, const applicant * a2) = {f2,f2,f2,f2,f2,f2,f2,f2,f2,f2};

	// run it
	for(int i = 0; i < 10; i++)
	{
		cout << (*pa[i])(a1, a2) << endl;
		cout << "------another way runing by pointer " << endl;
		cout << pa[i](a1, a2) << endl;
	}	
}


void f1(applicant * a)
{
	cout << "sample function for f_7_12_13 " << endl;
}

const char * f2(const applicant * a1, const applicant * a2)
{
	return a1->name;
}

void f_7_13_1()
{
	cout << "Enter two numbers (0 terminates)";
	double x,y;
	while(1)
	{
		cout << "x:";
		cin >> x;
		if(x == 0) break;
		// clear enter
		cin.get();
		cout << "y:";
		cin >> y;
		if (y == 0)
		{
			break;
		}
		double result = h_mean(x,y);
		cout << "2.0 * x * y /(x + y) " << result << endl;		
	}
	cout << "0 entered close function" << endl;
	

	// another way to input 
	// while(cin >> x && cin >> y && x*y != 0)

}

double h_mean(double x, double y)
{
	return 2.0 * x * y /(x + y);
}

void f_7_13_2()
{
	// -1 terminate input allowed therefore score needs initialization properly
	double score[10] = {0,0,0,0,0,0,0,0,0,0};
	f_7_13_2_input(score, 10);
	f_7_13_2_display(score, 10);
}

void f_7_13_2_input(double * score, int num)
{
	int i = 0;
	while(i < num)
	{
		// clear enter only necessary on ch = cin.get()?, functions correctly with out it
		// cin.get();
		cout << "input " << i <<" value:";
		cin >> score[i];
		if(score[i] == -1)
		{
			score[i] = 0;
			break;
		}
		i++;
	}
}

double f_7_13_2_cal(const double * score, int num)
{
	double sum = 0;
	for(int i = 0; i < num; i++)
	{
		sum += score[i];
	}

	return sum /10;
}

void f_7_13_2_display(const double * score, int num)
{
	double ave = f_7_13_2_cal(score, num);
	cout << "average score = " << ave << endl;
}

void f_7_13_3()
{
	box b = {"arston arfupt", 20.0, 15.12,59.02,0.0};
	display_box(b);
	set_box(&b);
	display_box(b);
}

void display_box(box b)
{
	cout << "box maker" << b.maker << endl;
	cout << "box height" << b.height << endl;
	cout << "box width" << b.width << endl;
	cout << "box length" << b.length << endl;
	cout << "box volume" << b.volume << endl;
}

void set_box(box * bp)
{
	bp->volume = bp->height * bp->width * bp->length;
}

void f_7_13_4()
{
	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" << endl;
}

long double probability(unsigned int numbers, unsigned int picks)
{
	long double result = 1.0;
	long double n;
	unsigned int p;
	for(n = numbers, p = picks; p > 0; n--,p--)
	{
		result = result * n / p;
	}
	return result;
}

void f_7_13_5()
{
	unsigned int n;
	cout << "input a positive integer: ";
	cin >> n;
	cout << f_7_13_5_recursive(n) << endl;
}

long long f_7_13_5_recursive(int n)
{
	long long result;
	// exit choices
	if(n == 0)
	{
		result = 1;
	}
	else if(n == 1)
	{
		result = 1;
	}
	// recursive choices
	else 
	{
		result = n * f_7_13_5_recursive(n-1);
	}

	return result;
}

void f_7_13_6()
{
	const int ARC_SIZE = 10;
	double arr[ARC_SIZE];
	int i = 0;
	cout << "Enter up to " << ARC_SIZE << " values(q to quit): " << endl;	
	f_7_13_6_fill(arr, ARC_SIZE);
	f_7_13_6_reverse(arr, ARC_SIZE);
	f_7_13_6_show(arr, ARC_SIZE);
}

void f_7_13_6_fill(double * arr, int num)
{
	int i = 0;
	while(cin >> arr[i] && i++ < num-1)
	{
		// non digit input
		if(!cin)
			break;
	}
}

void f_7_13_6_reverse(double * arr, int num)
{
	// pointer to head and tail
	double * ps = &arr[0];
	double * pt = &arr[num-1];
	double temp;
	while(ps < pt)
	{
		temp = *pt;
		*pt = *ps;
		*ps = temp;

		ps++;
		pt--;
	}
}

void f_7_13_6_show(const double * arr, int num)
{
	for(int i = 0; i < num; i++)
	{
		cout << "arr[" << i << "] = " << arr[i] << endl;
	}
}

void f_7_13_7()
{
	const int ARC_SIZE = 10;
	double arr[ARC_SIZE];
	f_7_13_7_fill(arr, arr+(ARC_SIZE-1));
	f_7_13_7_show(arr, arr+(ARC_SIZE-1));
	cout << "input refactor number \n";
	double factor = f_7_13_7_get();
	f_7_13_7_refactor(arr, arr+(ARC_SIZE-1), factor);
	f_7_13_7_show(arr, arr+(ARC_SIZE-1));
}

void f_7_13_7_fill(double * ps, double * pt)
{
	double num_db;
	while(ps <= pt)
	{
		num_db = f_7_13_7_get();
		*ps = num_db;
		ps++;
	}
}

void f_7_13_7_refactor(double * ps, double * pt, double factor)
{
	while(ps <= pt)
	{
		*ps = *ps * factor;
		ps++;
	}

}

void f_7_13_7_show(double * ps, double * pt)
{
	while(ps <= pt)
	{
		cout << "number: " << *ps << endl;
		ps++;
	}
}

double f_7_13_7_get()
{
	cout << "double number: ";
	double factor;
	cin >> factor;
	// check this funny part of input checking
	if(!cin) // bad input
	{
		cin.clear();
		while(cin.get() != '\n')
			continue; // terminates on reading keyboard enter
		cout << "Bad input; input process terminated.\n";
		exit(1); // ?
	}

	return factor;
}

void f_7_13_8()
{
	const char * seasons[] = {"spring","summer","autumn","winter"};
	double num[4];
	for(int i = 0; i < 4; i++)
	{
		cout << "number for season " << seasons[i] << ": ";
		num[i] = f_7_13_7_get();
	}

	f_7_13_7_show(num, num+3);
}

void f_7_13_9()
{
	cout << "Enter class size: ";
	int class_size;
	cin >> class_size;
	while(cin.get() != '\n')
		continue;

	student * ptr_stu = new student[class_size];
	f_7_13_9_getinfo(ptr_stu, class_size);
	for(int i = 0; i < class_size; i++)
	{
		f_7_13_9_display1(ptr_stu[i]);
		f_7_13_9_display2(&ptr_stu[i]);
	}
	f_7_13_9_display3(ptr_stu, class_size);
	delete [] ptr_stu;
	cout << "Done\n";
}

void f_7_13_9_getinfo(student * ptr_stu, int class_size)
{
	int i = 0;
	char * name;
	char * hobby;
	int ooplevel;
	while(i < class_size)
	{
		cout << "student name: ";
		cin.getline(ptr_stu->fullname, SLEN);
		cout << "student hobby: ";
		cin.getline(ptr_stu->hobby, SLEN);
		cout << "student ooplevel: ";
		cin >> ptr_stu->ooplevel;
		ptr_stu++;
		i++;
		cin.get();
	}
}

void f_7_13_9_display1(student stu)
{
	cout << "----------display1----------" << endl;
	cout << "student name: " << stu.fullname<< endl;
	cout << "student hobby: " << stu.hobby<< endl;
	cout << "student ooplevel: " << stu.ooplevel << endl;
}

void f_7_13_9_display2(const student * ptr_stu)
{
	cout << "----------display2----------" << endl;
	cout << "student name: " << ptr_stu->fullname<< endl;
	cout << "student hobby: " << ptr_stu->hobby<< endl;
	cout << "student ooplevel: " << ptr_stu->ooplevel << endl;	
}

void f_7_13_9_display3(const student * ptr_stu, int n)
{
	cout << "----------display3----------" << endl;
	for(int i = 0; i < n; i++,ptr_stu++)
	{
		f_7_13_9_display2(ptr_stu);
	}
}

void f_7_13_10()
{
	double x = 1.1, y = 2.2;
	f_7_13_10_caller(x,y,f_7_13_10_add1);
	f_7_13_10_caller(x,y,f_7_13_10_add2);
	cout << "initializing array of pf pointers" << endl;
	double (*pa[2])(double,double) = {f_7_13_10_add1,f_7_13_10_add2};
	for(int i = 0; i < 2; i++)
	{
		f_7_13_10_caller(x,y,pa[i]);
	}
	// 指向[(指针数组)指针]的指针测试
	// pa(指针数组)的名称,名称即是地址,也是数组的第一个元素。其调用方法如上所示
	// pa=&pa[0],但是&pa是真个数组的地址(即三个指针块),从数字上说pa=&pa[0]
	// 但是pa+1为数组中下一个元素的地址,而&pa+1则是pa后面一个12字节内存块的地址。
	// 关于解除引用 pa[0] == *pa == **&pa
	// 那么再加一根指针,让它指向&pa,该指针可以理解为指向[(指针数组)指针]的指针。
	auto pd = &pa; 
	// 手工定义
	double (*(*pf)[2])(double,double) = &pa;
	// 函数指针的两种调用方式debatable
	cout << "calling (*pf)[0](x,y) returns " << (*pf)[0](x,y) << endl;
	cout << "calling (*(*pf)[1])(x,y) returns " << (*(*pf)[1])(x,y) << endl;
}

void f_7_13_10_caller(double x, double y, double (*pf)(double, double))
{
	cout << "calling add by pf:(*pf)(x,y)";
	cout << (*pf)(x,y) << endl;
	
}

double f_7_13_10_add1(double x, double y)
{
	cout << "in add_1" << endl;
	return x + y;
}

double f_7_13_10_add2(double x, double y)
{
	cout << "in add_2" << endl;
	return (x + y) * 1.234;
}
上一篇:图形绘制
下一篇:数据类型
网友评论
<