MOOC清华《面向对象程序设计》第7章:统计考试及格率v2.0(采用迭代器、容器、模板方法)

//main.cpp

#include <iostream>
#include <cstdlib>
#include "ArrayCollection.h"
#include "LinkedListIterator.h"
#include "LinkedListCollection.h"
using namespace std;

template <class _iterator>
void analyze(_iterator begin, _iterator end){
	int passed = 0, count = 0;
	for(_iterator p = begin; p != end; p++){
		if(*p >= 60)
			passed++;
		count++;
	}
	cout << "passing rate = " << (float)passed / count << endl;
}

int main(int argc, char** argv) {
	float scores[] = {90,20,40,40,30,  60,70,30,90,100};
	/*
	Collection* c = new ArrayCollection(10, scores);
	cout << "passing rate = "
		<< (float)count_if(c->begin(), c->end(), passed()) / c.size() << endl;
	*/
	ArrayCollection<float> collection1(10, scores);
	LinkedListCollection<float> collection2(10, scores);
	
	analyze(scores, scores + 10);
	analyze(collection1.begin(), collection1.end());
	analyze(collection2.begin(), collection2.end());
	
	system("pause");
	return EXIT_SUCCESS;
}


//ArrayCollection.h

#ifndef ArrayCollection_h
#define ArrayCollection_h

template <class T>
class ArrayCollection{
	T* _data;
	int _size;
public:
	ArrayCollection():_size(10){
		_data = new T[_size];
	}
	ArrayCollection(int size):_size(size){
		_data = new T[_size];
	}
	ArrayCollection(int size, T* data):_size(size){
		_data = new T[_size];
		for(int i = 0; i < size; i++)
			*(_data + i) = *(data + i);
	}
	~ArrayCollection(){
		delete[] _data;
	}
	int size(){
		return _size;
	}//其实这个函数没有用上 
	T* begin(){
		return _data;
	}
	T* end(){
		return (_data + _size);
	}
};

#endif

//LinkedListIterator.h

#ifndef LinkedListIterator_h
#define LinkedListIterator_h

template <class T>
struct LinkedListNode{
	T _data;
	LinkedListNode *next;
	LinkedListNode():next(NULL){}
	LinkedListNode(T data):_data(data), next(NULL){}
};

template <class T>
struct LinkedListIterator{
	LinkedListNode<T> *pointer;
	LinkedListIterator(LinkedListNode<T> *p):pointer(p){}
	LinkedListIterator(const LinkedListIterator<T>& it):pointer(it.pointer){}
	LinkedListIterator<T>& operator++(){
		pointer = pointer->next;
		return *this;
	}
	const LinkedListIterator<T> operator++(int){
		LinkedListIterator<T> temp = *this;
		pointer = pointer->next;
		return temp;
	}
	T& operator*() const{
		return pointer->_data;
	}
	T* operator->() const{
		return &(pointer->_data);
	}
	bool operator!=(const LinkedListIterator<T> &other){
		return pointer != other.pointer;
	}
	bool operator==(const LinkedListIterator<T> &other){
		return pointer == other.pointer;
	}
};

#endif

//LinkedListCollection.h

#ifndef LinkedListCollection_h
#define LinkedListCollection_h

template <class T>
class LinkedListCollection{
	LinkedListNode<T>* _head;
public:
	LinkedListIterator<T> begin(){
		return LinkedListIterator<T>(_head);
	}
	LinkedListIterator<T> end(){
		return LinkedListIterator<T>(NULL);
	}
	LinkedListCollection():_head(NULL){}
	LinkedListCollection(int size, T* data){
		//...
		_head = NULL;
		for(int i = 0; i < size; i++){
			LinkedListNode<T>* Node_data = new LinkedListNode<T>;
			Node_data->_data = *(data + i);
			Node_data->next = _head;
			_head = Node_data;
		}
	}//这个函数视频里省略了,我自己写的 
	~LinkedListCollection(){
		//...
		while(_head){  
    		LinkedListNode<T>* tmp = _head;  
    		_head = _head->next;  
    		delete tmp;  
		} 
	}//这个函数视频里省略了,我自己写的
};

#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值