C++:策略模式+模板+虚(virtual)继承+排序

C++:策略模式+模板+虚继承+排序

一时兴起,纯原创! ZJUT_YC

文件说明

  • 排序函数接口定义文件:SortAlgorithmInterface.h
  • 冒泡排序文件实现:BubbleSort.h
  • 选择排序文件实现:SelectSort.h
  • 策略模式Contex文件定义: SortAlgorithmContext.h
  • 测试文件:main.cpp

代码详情

SortAlgorithmInterface.h

#pragma once
template<typename T>
class SortAlgorithmInterface
{
public:
	virtual void sort(T data_array[],int size)=0;
};

BubbleSort.h

#pragma once
#include "SortAlgorithmInterface.h"
#include <exception>
#include <iostream>
template<typename T>
class BubbleSort :
	public SortAlgorithmInterface<T>
{
public:
	virtual void sort(T data_array[], int size);
};

template<typename T>
inline void BubbleSort<T>::sort(T data_array[], int size)
{
	std::cout << "BubbleSort:\n";
	/*if (sizeof(data_array) != 0 && (sizeof(data_array) / sizeof(data_array[0])) != size) {
		throw std::exception("size error!");
		return;
	}*/
	T* data = new T();
	for (int i = 0; i < size - 1; i++)
	{
		for (int j = 0; j < size - 1 - i; j++)
		{
			if (data_array[j] > data_array[j + 1]) {
				*data = data_array[j];
				data_array[j] = data_array[j + 1];
				data_array[j + 1] = *data;
			}
		}
	}
	delete data;
	data = nullptr;
}

SelectSort.h

#pragma once
#include "SortAlgorithmInterface.h"
#include <exception>
#include <iostream>
template<typename T>
class SelectSort :
	public SortAlgorithmInterface<T>
{
public:
	virtual void sort(T data_array[], int size);
};

template<typename T>
inline void SelectSort<T>::sort(T data_array[], int size)
{
	std::cout << "SelctSort:\n";
	/*if (sizeof(data_array) != 0 && (sizeof(data_array) / sizeof(data_array[0])) != size) {
		throw std::exception("size error!");
		return;
	}*/
	T* data = new T();
	for (int j = size - 1; j > 0; j--) {
		for (int i = 0; i < j; i++) {
			if (data_array[j] < data_array[i]) {
				*data = data_array[j];
				data_array[j] = data_array[i];
				data_array[i] = *data;
			}
		}
	}
	delete data;
	data = nullptr;
}

SortAlgorithmContext.h

#pragma once
#include"SortAlgorithmInterface.h"

template<typename T>
class SortAlgorithmContext
{
private:
	SortAlgorithmInterface<T>* m_alg;
public:
	SortAlgorithmContext();
	~SortAlgorithmContext();
	void set(SortAlgorithmInterface<T>* alg);
	void sort(T data_array[], int size);
};

template<typename T>
inline SortAlgorithmContext<T>::SortAlgorithmContext()
{
	m_alg = nullptr;
}

template<typename T>
inline SortAlgorithmContext<T>::~SortAlgorithmContext()
{
	delete m_alg;
	m_alg = nullptr;
}

template<typename T>
inline void SortAlgorithmContext<T>::set(SortAlgorithmInterface<T>* alg)
{
	m_alg = alg;
}

template<typename T>
inline void SortAlgorithmContext<T>::sort(T data_array[], int size)
{
	m_alg->sort(data_array, size);
}

main.cpp

#include "sort_algorithm.h"
#include "SortAlgorithmInterface.h"
#include "BubbleSort.h"
#include "SelectSort.h"
#include "SortAlgorithmContext.h"

int main() {
	const int size = 100;
	int a[size];
	initial_int_array(a, size);
	SortAlgorithmInterface<int>* sort_alg_0 = new SelectSort<int>();
	SortAlgorithmInterface<int>* sort_alg_1 = new BubbleSort<int>();

	SortAlgorithmContext<int>* context = new SortAlgorithmContext<int>();
	context->set(sort_alg_0);
	context->sort(a,size);
	print_int_array(a, size);

	context->set(sort_alg_1);
	context->sort(a, size);
	print_int_array(a, size);
	/*initial_int_array(a, size);
	bubble_sort(a, size);
	print_int_array(a, size);*/
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值