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

这次Debug的最大收获就是:凡是头文件,都要加上“防止重复编译三句话”。代码中有些语句似乎留与不留都可以编译通过,不知为什么。

//main.cpp

#include <iostream>
#include <cstdlib>
#include "Iterator.h"
using namespace std;

void analyze(Iterator* begin, Iterator* end);

int main(int argc, char** argv) {
	float scores[] = {90,20,40,40,30,  60,70,30,90,100};
	Collection* collection = new ArrayCollection(10, scores);
	analyze(collection->begin(), collection->end());
	system("pause");
	return EXIT_SUCCESS;
}

//Iterator.h

#ifndef Iterator_h
#define Iterator_h

//注意编译的顺序,其他的子头文件不能写在迭代器前面,要放在后面 

class Iterator{
public:
	virtual ~Iterator(){}
	virtual bool operator!=(const Iterator &other) const = 0;
	virtual const Iterator& operator++() = 0;
	virtual const Iterator& operator++(int) = 0;
	virtual float& operator*() const = 0;
	virtual float* operator->() const = 0;
	bool operator==(const Iterator &other) const {
		return !(*this != other);
	}
};

#include "ArrayIterator.h"
#include "Collection.h"
#include "ArrayCollection.h"
//其实这三条包含语句不写也可以通过,可是main函数并没有包含它们啊,不知为什么? 

#endif

//Analyze.cpp

#include <iostream>
#include "Iterator.h"
using namespace std;

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;
}

//ArrayIterator.h

#ifndef ArrayIterator_h
#define ArrayIterator_h

//#include "Iterator.h"

class ArrayIterator:public Iterator{
	float *_data;
	int _index;
public:
	ArrayIterator(float* data, int index):_data(data),_index(index){}
	ArrayIterator(const ArrayIterator& other):_data(other._data),_index(other._index){}
	~ArrayIterator(){}
	const Iterator& operator++();
	const Iterator& operator++(int);
	float& operator*() const;
	float* operator->() const;
	bool operator!=(const Iterator &other) const;
};

#endif

//ArrayIterator.cpp

#include "Iterator.h"
#include "ArrayIterator.h"

const Iterator& ArrayIterator::operator++(){
	_index++;
	return *this;
}

const Iterator& ArrayIterator::operator++(int){
	_index++;
	return ArrayIterator(_data, _index - 1);
}

float& ArrayIterator::operator*() const{
	return *(_data + _index);
}

float* ArrayIterator::operator->() const{
	return (_data + _index);
}

bool ArrayIterator::operator!=(const Iterator &other) const{
	return (_data != ((ArrayIterator*)(&other))->_data || 
			_index != ((ArrayIterator*)(&other))->_index);
}

//Collection.h

#ifndef Collection_h
#define Collection_h

#include "Iterator.h"

class Collection{
public:
	virtual ~Collection(){}
	virtual Iterator* begin() const = 0;
	virtual Iterator* end() const = 0;
	virtual int size() = 0;
};

#endif

//ArrayCollection.h

#ifndef ArrayCollection_h
#define ArrayCollection_h

//#include "Collection.h"

class ArrayCollection:public Collection{
	friend class ArrayIterator;
	float* _data;
	int _size;
public:
	ArrayCollection():_size(10){
		_data = new float[_size];
	}
	ArrayCollection(int size, float* data):_size(size){
		_data = new float[_size];
		for(int i = 0; i < size; i++)
			*(_data + i) = *(data + i);
	}
	~ArrayCollection(){
		delete[] _data;
	}
	int size(){
		return _size;
	}
	Iterator* begin() const;
	Iterator* end() const;
};

#endif

//ArrayCollection.cpp

#include "Iterator.h"
#include "ArrayCollection.h"

Iterator* ArrayCollection::begin() const{
	return new ArrayIterator(_data, 0);
}

Iterator* ArrayCollection::end() const{
	return new ArrayIterator(_data, _size);
}

=================================分割线=================================

2017.09.13:我找到上述代码混乱的原因了:头文件不应该互相包含。解决办法:直接把各个头文件依次包含在main函数开头就行了,这样就不用写得那么“畸形”“难看”,代码量其实差不多的。示例参见下一篇博文。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值