栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++primer plus第六版第八章编程练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++primer plus第六版第八章编程练习

1.

#include
using namespace std;
int n = 0;
void show(const char* a,int b=0)
{
	n++;
	if (b == 0)
	{
		cout << a< 

2.

#include
using namespace std;
struct candybar {
	const char *name;//必须加const ,原因还未知。
	double heavy;
	int hot;
};
void cand(candybar& a, const char* b = "Millennium Munch",const double c = 2.85,const int d = 350)
{
	a.name = b;
	a.heavy = c;
	a.hot = d;
}
void show(candybar& a)
{
	cout << "name:" << a.name< 

3.

#include
#include
using namespace std;
void transfer(string& a)
{
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] == ' ')
			continue;
		a[i] = a[i] - 32;
	}
}
int main()
{
	string a;
	cout << "Enter a string (q to quit):";
	getline(cin,a);
	while (a[0] != 'q' || a.size() > 1)
	{
		transfer(a);
		cout << a< 

4.

#include
using namespace std;
#include
struct stringy {
	char* str;
	int ct;
};
void set(stringy& a, char* ch)
{
	a.str = new char[strlen(ch) + 1];
	a.ct = strlen(ch);
	a.str = ch;
}
void show(const stringy&a,const int b = 1)
{
	for(int i=0;i 

5.

#include
using namespace std;
template 
T show(T *a)
{
	T max = 0;
	for (int i = 0; i < 5; i++)
	{
		if (i == 0)
			max = a[i];
		else if (a[i] > max)
			max = a[i];
	}
	return max;
}
int main()
{
	int a[5] = { 2,1,56,3565,489 };
	double b[5] = { 135.23,13.12,487.66,45.12,56544.12 };
	cout << show(a)< 

6.

#include
using namespace std;
template 
T maxn(T A[], int len)
{
	T max=A[0];
	for (int i = 0; i < len; i++)
		max = A[i] > max ? A[i] : max;
	return max;
}
template<>char* maxn(char* a[], int len)
{
	int max = -1;
	 char* p = NULL;
	for (int i = 0; i < len; ++i)
	{
		if (strlen(a[i]) > max)
		{
			max = strlen(a[i]);
			p = a[i];
		}
	}
	return p;
}
int main()
{
	int a[6] = { 1, 2, 3, 4, 5, 6 };
	double b[4] = { 1.1, 2.2, 3.3, 4.4 };
	const char* c[5] = { "fuck", "fuckfuck", "fuckfuckfuck", "fuckfuckfuckfuck", "fuckfuckfuckfuckfuck" };
	cout << maxn(a, 6) << endl;
	cout << maxn(b, 4) << endl;
	cout << maxn(c, 5) << endl;
	return 0;
}

7.

#include 
template             // template A
void SumArray(T arr[], int n);
template             // template B
void SumArray(T* arr[], int n);
struct debts
{
	char name[50];
	double amount;
};
int main()
{
	using namespace std;
	int things[6] = { 13, 31, 103, 301, 310, 130 };
	struct debts mr_E[3] =
	{
		{"Ima Wolfe", 2400.0},
		{"Ura Foxe", 1300.0},
		{"Iby Stout", 1800.0}
	};
	double* pd[3];
	// set pointers to the amount members of the structures in mr_E
	for (int i = 0; i < 3; i++)
		pd[i] = &mr_E[i].amount;
	cout << "Listing Mr. E's counts of things:n";
	// things is an array of int
	SumArray(things, 6);  // uses template A
	cout << "Listing Mr. E's debts:n";
	// pd is an array of pointers to double
	SumArray(pd, 3);      // uses template B (more specialized)
	cin.get();
	return 0;
}
template 
void SumArray(T arr[], int n)
{
	using namespace std;
	T sum = 0;
	cout << "template An";
	for (int i = 0; i < n; i++)
		sum = sum + arr[i];
	cout << sum << endl;
}
template 
void SumArray(T* arr[], int n)
{
	using namespace std;
	T sum = 0;
	cout << "template Bn";
	for (int i = 0; i < n; i++)
		sum = sum + *arr[i];
	cout << sum << endl;
}

第七题直接抄的,感觉知识点都一样

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847214.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号