第八章 函数探幽

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_24994943/article/details/82253902

2、

#include <iostream>
using namespace std;

struct CandyBar
{
	char *name;
	double weight;
	int hot;
};

void Set(CandyBar &candy, const char *c = "Milennium Munch", const double &w = 2.85, const int &h = 350);
void Show(const CandyBar &candy);

int main()
{
	CandyBar candyOne, candyTwo;
	Set(candyOne);
	Set(candyTwo, "CandyTwo", 3.50, 285);
	Show(candyOne);
	Show(candyTwo);
	return 0;
}

void Set(CandyBar & candy, const char * c, const double & w, const int & h)
{
	int n = strlen(c);
	candy.name = new char[n + 1];
	int i;
	for (i = 0; i < n; i++)
	{
		candy.name[i] = c[i];
	}
	candy.name[i] = '\0';//若此处写成candy.name[i + 1] = '\0';则第i个位置就不知道是什么了,系统打印将出错
	candy.weight = w;
	candy.hot = h;
}

void Show(const CandyBar &candy)
{
	cout << candy.name << " " << candy.weight << " "  << candy.hot << endl;
}


3、

#include <iostream>
#include <string>
using namespace std;
void ChangeToUp(string &s);


int main()
{
	string s;
	char c[50];
	cout << "Enter a string (q to quit):";
	cin.getline(c, 49);
	while (c[0]!='q')
	{
		s = c;
		ChangeToUp(s);
		cout << s << endl;
		cout << "Enter a string (q to quit):";
		cin.getline(c, 49);
	}

	cout << "Bye.";
	return 0;
}

void ChangeToUp(string & s)
{
	for (int i = 0; i < s.size(); i++)
		s[i] = toupper(s[i]);
}

4、

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

struct stringy
{
	char * str;
	int ct;
};

void set(stringy &beany, const char *c);
void show(const stringy &b, const int &i = 1);
void show(const char *c, const int &i = 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!");

	return 0;
}

void set(stringy & beany, const char * c)
{
	int n = strlen(c);
	beany.str = new char[n + 1];
	for (int i = 0; i < n; i++)
	{
		beany.str[i] = c[i];
	}
	beany.str[n] = '\0';
}

void show(const stringy & b, const int &i)
{
	int n = i;
	while (n)
	{
		cout << b.str << endl;
		n--;
	}
}

void show(const char * c, const int &i)
{
	int n = i;
	while (n)
	{
		cout << c << endl;
		n--;
	}
}

5、

#include <iostream>
using namespace std;

template <typename T>
T max5(T a[])
{
	T max = a[0];
	for (int i = 1; i < 5; i++)
	{
		if (a[i] > max) max = a[i];
	}
	return max;
}


int main()
{
	int a[5] = { 5,66,42,24,72 };
	double b[5] = { 31.42, 32.56, 65.41, 124.67, 22.21 };
	int max1 = max5(a);
	cout << max1 << endl;
	double max2 = max5(b);
	cout << max2 << endl;
	return 0;
}

6、

#include <iostream>
using namespace std;

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

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


int main()
{
	int a[6] = { 5, 66, 42, 24, 72, 23 };
	double b[4] = { 31.42, 32.56, 65.41, 124.67 };
	int max1 = maxn(a, 6);
	cout << max1 << endl;
	double max2 = maxn(b, 4);
	cout << max2 << endl;
	char c1[] = "Hello";
	char c2[] = "World.";
	char c3[] = "!";
	char *name[3] = { c1,c2,c3 };
	char *max3 = maxn(name, 3);
	cout << max3 << endl;
	return 0;
}

//返回一个包含n个元素的数组中的最大值
template<typename T>
T maxn(T a[], int n)
{
	T max = a[0];
	for (int i = 0; i < n; i++)
	{
		if (a[i] > max) max = a[i];
	}
	return max;
}

template<>
char * maxn(char* a[], int  n)
{
	char *max = a[0];
	for (int i = 0; i < n; i++)
	{
		if (strlen(a[i]) > strlen(max)) max = a[i];
	}
	return max;
}

猜你喜欢

转载自blog.csdn.net/sinat_24994943/article/details/82253902