面向对象程序设计|函数模板

目录

题目一:倚天屠龙记(函数模板)

题目二:元素查找(函数模板)

题目三:谁的票数最高(函数模板)

题目四:排序函数模板

题目五:对象相加函数模板


题目一:倚天屠龙记(函数模板)

题目描述:

江湖中有一个传言,只要倚天剑和屠龙刀中暗藏的秘密拼到一起,就能得到天下无敌的内功秘笈。设计一个函数模板,完成拼凑的功能(将倚天剑的秘密连接到屠龙刀的后面),并将秘笈输出. 其中每个秘密由n个元素组成,类型为T。

输入要求:

第一行输入t表示有t个测试实例

第二行先输入一个大写字母表示数据类型,I表示整数类型,D表示双精度数类型,C表示字符型;然后输入n表示数据个数。

第三行输入倚天剑的n个数据

第四行输入屠龙刀的n个数据

依次输入t个实例

输出要求:

每行输出一个结果

输入样例:

2
I 5
5 3 51 27 9
27 0 0 5 1
C 5
kitty
hello

输出样例:

2700515351279
hellokitty

代码示例:

#include
#include
#include
#include 
#include
#include
using namespace std;

template//函数模板 输入
void in(T arr[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
}

template//函数模板 输出
void out(T arr[], T brr[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << brr[i];
	}
	for (int i = 0; i < n; i++)
	{
		cout << arr[i];
	}
	cout << endl;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		char type;
		int n;
		cin >> type >> n;
		switch (type)
		{
		case 'I':
		{
			int* yt = new int[n];
			int* tl = new int[n];
			in(yt, n);
			in(tl, n);
			out(yt, tl, n);
			break;
		}
		case 'D':
		{
			double* yt = new double[n];
			double* tl = new double[n];
			in(yt, n);
			in(tl, n);
			out(yt, tl, n);
			break;
		}
		case 'C':
		{
			char* yt = new char[n];
			char* tl = new char[n];
			in(yt, n);
			in(tl, n);
			out(yt, tl, n);
			break;
		}
		}
	}
	return 0;
}

题目二:元素查找(函数模板)

题目描述:

编写一个在数组中进行查找的函数模板,其中数组为具有n个元素,类型为T,要查找的元素为key。

注意:必须使用模板函数

输入要求:

第一行输入t表示有t个测试实例

第二行先输入一个大写字母表示数组类型,I表示整数类型,D表示双精度数类型,C表示字符型,S表示字符串型;然后输入n表示数组长度。

第三行输入n个数据

第四行输入key

依次输入t个实例

输出要求:

每行输出一个结果,找到输出key是数组中的第几个元素(从1开始),找不到输出0

输入样例:

4
I 5
5 3 51 27 9
27
D 3
-11.3 25.42 13.2
2.7
C 6
a b g e u q
a
S 4
sandy david eason cindy
cindy

输出样例:

4
0
1
4

代码示例:

#include
#include
#include
#include 
#include
#include
using namespace std;

template//函数模板 输入
void in(T arr[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
}

template//函数模板 查找
void find(T arr[], T x, int n)
{
	int mark = 0;
	for (int i = 0; i < n; i++)
	{
		if (arr[i] == x)
		{
			cout << i + 1 << endl;
			mark = 1;
		}
	}
	if (mark == 0)
	{
		cout << mark << endl;
	}
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		char type;
		int n;
		cin >> type >> n;
		switch (type)
		{
		case 'I':
		{
			int* i = new int[n];
			in(i, n);
			int target;
			cin >> target;
			find(i, target, n);
			break;
		}
		case 'D':
		{
			double* d = new double[n];
			in(d, n);
			double target;
			cin >> target;
			find(d, target, n);
			break;
		}
		case 'C':
		{
			char* c = new char[n];
			in(c, n);
			char target;
			cin >> target;
			find(c, target, n);
			break;
		}
		case 'S':
		{
			string* c = new string[n];
			in(c, n);
			string target;
			cin >> target;
			find(c, target, n);
			break;
		}
		}
	}
	return 0;
}

题目三:谁的票数最高(函数模板)

题目描述:

某小镇要票选镇长,得票最高者当选。但由于投票机制不健全,导致每届投票时,候选人在投票系统的识别码类型不一致。请编写函数模板,能针对多种类型的数据,查找出得票最高的元素。其中,每届投票的选票有n张,识别码类型为T

注意:必须使用模板函数

输入要求:

第一行输入t表示有t个测试实例

第二行先输入一个大写字母表示识别码类型,I表示整数类型,C表示字符型,S表示字符串型;然后输入n表示数组长度。

第三行输入n个数据

依次输入t个实例

输出要求:

每行输出一个结果,分别输出当选者的识别码和得票数,以空格分开。

输入样例:

3
I 10
5 3 5 2 9 7 3 7 2 3
C 8
a b a e b e e q
S 5
sandy david eason cindy cindy

输出样例:

3 3
e 3
cindy 2

代码示例:

#include
#include
#include
#include 
#include
#include
using namespace std;

template//函数模板 输入
void in(T arr[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
}

template//函数模板 输出票数最多的
void out(T arr[], int n)
{
	for (int i = 0; i < n - 1; i++)//选择排序
	{
		for (int j = i + 1; j < n; j++)
		{
			if (arr[i] > arr[j])
			{
				T temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
	T this_t = arr[0];//统计最高票数的算法
	T most_t = arr[0];
	int this_choose = 1;
	int most_choose = 0;
	for (int i = 0; i < n; i++)
	{
		if (this_t != arr[i + 1])
		{
			if (this_choose > most_choose)
			{
				most_choose = this_choose;
				most_t = this_t;
			}
			this_choose = 1;
			this_t = arr[i + 1];
		}
		else
		{
			this_choose++;
		}
	}
	cout << most_t << " " << most_choose << endl;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		char type;
		int n;
		cin >> type >> n;
		switch (type)
		{
		case 'I':
		{
			int* i = new int[n];
			in(i, n);
			out(i, n);
			break;
		}
		case 'C':
		{
			char* c = new char[n];
			in(c, n);
			out(c, n);
			break;
		}
		case 'S':
		{
			string* c = new string[n];
			in(c, n);
			out(c, n);
			break;
		}
		}
	}
	return 0;
}

记住模型 template + 函数

题目四:排序函数模板

题目描述:

编写一个对n个元素的数组升序排序的函数模板mysort,其中元素类型可以是基本数据类型,也可以是点对象(按点到原点的距离比较)。(要求不能用C++提供的sort函数模板)

输入要求:

第一行输入测试次数

每次测试输入二行,第1行先输入一个大写字母表示数组类型,I表示整数类型,S表示字符串型,D表示双精度数类型,P表示点,最后输入n表示数组长度。第2行输入n个数据。

输出要求:

每次测试输出一行排序后的结果

输入样例:

4
I 10
15 3 51 27 9 35 78 14 65 8
D 3
-11.3 25.42 13.2
P 6
1.1 2.2 2.4 -6.5 12 32 1.2 1.3 -3.5 0.1 9.2 1.1
S 4
sandy david eason cindy

输出样例:

3 8 9 14 15 27 35 51 65 78 
-11.3 13.2 25.42 
(1.2, 1.3) (1.1, 2.2) (-3.5, 0.1) (2.4, -6.5) (9.2, 1.1) (12.0, 32.0) 
cindy david eason sandy 

代码示例:

#include
#include
#include
#include 
#include
#include
using namespace std;

class Point
{
private:
	double x, y, dis;
public:
	Point() {}
	double getDis()
	{
		return dis;
	}
	friend istream& operator >> (istream& in, Point& rhs);
	friend ostream& operator << (ostream& out, Point& rhs);
	friend bool operator > (Point& p1, Point& p2);
};

istream& operator >> (istream& in, Point& rhs)
{
	in >> rhs.x >> rhs.y;
	rhs.dis = sqrt(pow(rhs.x, 2) + pow(rhs.y, 2));
	return in;
}

ostream& operator << (ostream& out, Point& rhs)
{
	cout << fixed << setprecision(1) << "(" << rhs.x << ", " << rhs.y << ")";
	return out;
}

bool operator > (Point& p1, Point& p2)
{
	if (p1.getDis() > p2.getDis())
	{
		return true;
	}
	else
	{
		return false;
	}
}

template//函数模板 输入
void input(T arr[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
}

template//函数模板 排序
void mysort(T arr[], int n)
{
	for (int i = 0; i < n - 1; i++)//选择排序
	{
		for (int j = i + 1; j < n; j++)
		{
			if (arr[i] > arr[j])
			{
				T temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		char type;
		int n;
		cin >> type >> n;
		switch (type)
		{
		case 'I':
		{
			int* i = new int[n];
			input(i, n);
			mysort(i, n);
			break;
		}
		case 'D':
		{
			double* c = new double[n];
			input(c, n);
			mysort(c, n);
			break;
		}
		case 'S':
		{
			string* c = new string[n];
			input(c, n);
			mysort(c, n);
			break;
		}
		case'P':
		{
			Point* c = new Point[n];
			input(c, n);
			mysort(c, n);
			break;
		}
		}
	}
	return 0;
}

题目五:对象相加函数模板

题目描述:

时钟类CClock有时、分、秒;人民币类CRmb有元、角、分三个数据成员。试为这种类型的类对象定义一个两两相加的函数模板add,包括三个参数:2个对象和一个int表示进制。(要求不能用函数重载的方法)

主函数如下所示:

...

CClock c1(...), c2(...), c;

c = add(c1, c2, 60);

cout << c << endl;

CRmb r1(...), r2(...), r;

r = add(r1, r2, 10);

cout << r << endl;

输入要求:

第一个时钟对象的时分秒

第二个时钟对象的时分秒

第一个人民币对象的元角分

第二个人民币对象的元角分

输出要求:

两个时钟对象相加的结果

两个人民币对象相加的结果

输入样例:

15 34 25
7 25 36
5 6 7
3 4 5

输出样例:

23 0 1
9 1 2

代码示例:

#include
#include
#include
#include 
#include
#include
using namespace std;


class Clock
{
	int hour, minute, second;
public:
	Clock() {}
	Clock(int h, int m, int s) : hour(h), minute(m), second(s) {}
	int getHour() { return hour; }
	int getMinute() { return minute; }
	int getSecond() { return second; }
	Clock operator + (Clock& rhs)//类内对+重构
	{
		int hh = hour + rhs.getHour();
		int mm = minute + rhs.getMinute();
		int ss = second + rhs.getSecond();
		if (ss > 59)
		{
			ss = ss % 10;
			mm++;
		}
		if (mm > 59)
		{
			mm = mm % 10;
			hh++;
		}
		if (hh > 23)
		{
			hh %= 24;
		}
		Clock temp(hh, mm, ss);
		return temp;
	}
	friend ostream& operator << (ostream& out, Clock& rhs);//输出运算符重载
};
class RMB
{
	int yuan, jiao, fen;
public:
	RMB() {}
	RMB(int y, int j, int f) : yuan(y), jiao(j), fen(f) {}
	int getYuan() { return yuan; }
	int getJiao() { return jiao; }
	int getFen() { return fen; }
	RMB operator + (RMB& rhs)//类内对+重构
	{
		int yy = yuan + rhs.getYuan();
		int jj = jiao + rhs.getJiao();
		int ff = fen + rhs.getFen();
		if (ff > 9)
		{
			ff = ff % 10;
			jj++;
		}
		if (jj > 9)
		{
			jj = jj % 10;
			yy++;
		}
		RMB temp(yy, jj, ff);
		return temp;
	}
	friend ostream& operator << (ostream& out, RMB& rhs);//同理
};
ostream& operator << (ostream& out, Clock& rhs)
{
	out << rhs.hour << ' ' << rhs.minute << ' ' << rhs.second;
	return out;
}
ostream& operator << (ostream& out, RMB& rhs)
{
	cout << rhs.yuan << ' ' << rhs.jiao << ' ' << rhs.fen;
	return out;
}
template//函数模板 相加函数
T add(T& a, T& b, int n)
{
	T temp = a + b;
	return temp;
}
int main() {
	int h1, m1, s1, h2, m2, s2;
	cin >> h1 >> m1 >> s1 >> h2 >> m2 >> s2;
	Clock c1(h1, m1, s1), c2(h2, m2, s2), c;
	c = add(c1, c2, 60);
	cout << c << endl;
	int y1, j1, f1, y2, j2, f2;
	cin >> y1 >> j1 >> f1 >> y2 >> j2 >> f2;
	RMB r1(y1, j1, f1), r2(y2, j2, f2), r;
	r = add(r1, r2, 10);
	cout << r << endl;
	return 0;
}

你可能感兴趣的:(Simple,C++,算法,C++,开发语言)