c++primer plus 第八章习题答案(自己写的)

#include<iostream>
#include<stdlib.h>
//第八章
using namespace std;
void showstr(char *str, int n = 0);
int main()
{
	char *p = "I am a boy!";
	showstr(p);
	for (int i = 0; i < 3; i++)
	{
		cout << i << ":";
		showstr(p, i);
		cout<< endl;
	}
	
	system("pause");
	return 0;
}
void showstr(char *str, int n)
{
	int limit = 0;
	if (n == 0)
	{
		limit = 1;
	}
	static int use = 0;
	limit = ++use;
	for (int i = 0; i < limit; i++)
	{
		cout << str<<endl;
	}
}





#include<iostream>
#include<stdlib.h>
//第八章
using namespace std;
struct CandyBar
{
	char* brand;
	double weight;
	int height;
};
void showCandyBar(const CandyBar &candy);
void setCandyBar(CandyBar&, char *brand = "Millennium Munch", double weight = 2.85, int height = 350);
int main()
{
	CandyBar candy1,candy2;
	setCandyBar(candy1);
	showCandyBar(candy1);
	setCandyBar(candy2,"I love you",3.5,1);
	showCandyBar(candy2);
	system("pause");
	return 0;
}
void setCandyBar(CandyBar &candy, char *brand, double weight, int height)
{
	candy.brand = brand;
	candy.height = height;
	candy.weight = weight;
}
void showCandyBar(const CandyBar &candy)
{
	cout <<"brand:"<< candy.brand << endl;
	cout << "height:" << candy.height << endl;
	cout << "weight:" << candy.weight << endl<<endl;
}




#include<iostream>
#include<stdlib.h>
#include<string>
//第八章
using namespace std;
void up(string &);
int main()
{
	string str;
	cout << "Enter a string(q to quit):";
	while (getline( cin,str)&&str != "q")
	{
		up(str);
		cout << str << endl;
		cout << "Next string:";
	};
	cout << "bye!" << endl;
	system("pause");
	return 0;
}
void up(string &str)
{
	int strsize = str.size();
	for (int i = 0; i < strsize; i++)
	{
		if (isalpha(str[i]))
			str[i] = toupper(str[i]);
	}
}





#include<iostream>
#include<stdlib.h>
#include<string>
#include<cstring>
#define _CRT_SECURE_NO_WARNINGS
//第八章
using namespace std;
struct stringy
{
	char *str;
	int ct;
};
void set(stringy&, char *);
void show(const stringy&, int n = 1);
void show(const char *, int n = 1);
int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	set(beany, testing);
	show(beany);
	show(beany, 2);
	testing[0] = 'D';
	testing[1] = 'u';
	show(testing);
	show(testing, 3);
	show("Done!");
	delete[]beany.str;
	beany.str = NULL;
	system("pause");
	return 0;
}

void set(stringy& str, char *testing)
{
	str.ct = strlen(testing);
	str.str = new char[str.ct + 1];
	str.str=testing;
}
void show(const char *str, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << str << endl;
	}
}
void show(const stringy &str, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << str.str << endl;
	}
}




#include<iostream>
#include<stdlib.h>
#include<string>
#include<cstring>
#define _CRT_SECURE_NO_WARNINGS
//第八章
using namespace std;

template<typename T>
T max5(T a[]);

int main()
{
	int a[5] = { 1, 2, 3, 4, 5 };
	double b[5] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
	int i=max5(a);
	double j = max5(b);
	cout << i << endl;
	cout << j << endl;
	system("pause");
	return 0;
}
template<typename T>
T max5(T a[])
{
	T max = a[0];
	for (int i = 0; i < 5; i++)
	{
		if (a[i]>max)
			max = a[i];
	}
	return max;
}




#include<iostream>
#include<stdlib.h>
#include<string>
#include<cstring>
#define _CRT_SECURE_NO_WARNINGS
//第八章
using namespace std;

template<typename T>
T maxn(T a[],int num);

template<>char *maxn(char* a[], int num);

int main()
{
	int a[] = { 1, 2, 3, 4, 5 };
	double b[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
	char *str[3] = { "I am wqf", "I love you", "who are you" };
	int a1 = sizeof(a)/sizeof(int) + 1;
	int b1 = sizeof(b) / sizeof(double)+1;
	int i = maxn(a,a1);
	double j = maxn(b,b1);
	char *add = maxn(str, 3);
	cout << i << endl;
	cout << j << endl;
	cout << (int)add << endl;
	system("pause");
	return 0;
}
template<typename T>
T maxn(T a[],int num)
{
	T max = a[0];
	for (int i = 0; i < num; i++)
	{
		if (a[i]>max)
			max = a[i];
	}
	return max;
}
template<>char* maxn(char* a[], int num)
{
	int max = strlen(a[0]);
	int count;
	for (int i = 0; i < num; i++)
	{
		if (strlen(a[i])>max)
		{
			max = strlen(a[i]);
			count = i;
		}
	}
	return a[count];
}




#include<iostream>
#include<stdlib.h>
#include<string>
//第八章
using namespace std;

template<class T> double SumArray(T arr[], int n)
{
	cout << "template A\n";
	double	sum = 0;
	for (int i = 0; i < n; i++)
		sum += arr[i];
	return (sum);
}

template<class T> double SumArray(T * arr[], int n)
{
	cout << "template B\n";
	double	sum = 0;
	for (int i = 0; i < n; i++)
		sum += *arr[i];
	return (sum);
}


struct debts
{
	char name[50];
	double amount;
};
int main()
{
	int things[6] = { 13, 31, 103, 301, 2, 4 };
	struct debts mr[3] = 
	{
		{"I love you", 2400.0 },
		{"I hate you",1200.0},
		{"I fuck you",1100.0 },
	};
	double * pd[3];
	for (int i = 0; i < 3; i++)
		pd[i] = &mr[i].amount;
	cout << "the total number of Mr. E's things: " << SumArray(things, 6) << endl;
	cout << "the sum of Mr. E's all debts: " << SumArray(pd, 3) << endl;
	system("pause");
	return 0;
}

第八章习题答案
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值