MOOC清华《面向对象程序设计》第7章:统计考试及格率v3.0(自定义数据类型)
来源:互联网 发布:手机特效魔法软件 编辑:程序博客网 时间:2024/04/28 16:02
如果考试三科,每科都高于60分,这个学生才算及格,那么程序该怎么编?代码如下。通过这个例子,更深刻地体现算法与数据解耦的编程思想。
//main.cpp#include <iostream>#include <cstdlib>#include "ArrayCollection.h"#include "LinkedListIterator.h"#include "LinkedListCollection.h"#include "Score.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) {Score sarray[3];sarray[0] = Score(60, 60, 60);sarray[1] = Score(70, 70, 70);sarray[2] = Score(50, 80, 80);ArrayCollection<Score> collection3(3, sarray);LinkedListCollection<Score> collection4(3, sarray);analyze(sarray, sarray + 3);analyze(collection3.begin(), collection3.end());analyze(collection4.begin(), collection4.end());system("PAUSE");return EXIT_SUCCESS;}
//Score.h#ifndef Score_h#define Score_h#include <iostream>using namespace std;struct Score{float value[3];Score(){}Score(float f1, float f2, float f3){value[0] = f1;value[1] = f2;value[2] = f3;}Score& operator=(const Score& s){value[0] = s.value[0];value[1] = s.value[1];value[2] = s.value[2];return *this;}bool operator>=(float pass){return (value[0] >= pass && value[1] >= pass && value[2] >= pass);}};ostream& operator<<(ostream& out, const Score& s){out << '{' << s.value[0] << ',' << s.value[1] << ',' << s.value[2] << '}';return out;}//注意这个流运算符重载不在类Score的内部定义,而要拿到外面来 #endif
//ArrayCollection.h#ifndef ArrayCollection_h#define ArrayCollection_htemplate <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;}T* begin(){return _data;}T* end(){return (_data + _size);}};#endif
//LinkedListIterator.h#ifndef LinkedListIterator_h#define LinkedListIterator_htemplate <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_htemplate <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
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v3.0(自定义数据类型)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.4(单科及格与总分及格)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.1(“记住”及格分数线)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.2(用文件输入及格线)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v1.0(采用迭代器模式)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v2.0(采用迭代器、容器、模板方法)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v4.0(定义抽象结构)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.3(采用函数对象)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.0(某科及格线不是60分)
- MOOC清华《VC++面向对象与可视化程序设计》第5章:菜单资源例程(创建统计计算菜单项)
- MOOC清华《面向对象程序设计》第9章:听课感想
- MOOC清华《程序设计基础》第7章:统计活跃用户数
- MOOC清华《面向对象程序设计》第8章:栈的实现(基于数组)
- MOOC清华《面向对象程序设计》第8章:栈的实现(基于STL vector)
- MOOC清华《面向对象程序设计》第8章:栈的实现(采用适配器模式)
- MOOC清华《面向对象程序设计》第8章:栈的实现(采用模板实例化)
- MOOC清华《面向对象程序设计》第7章:负载监视器的设计v2.0(采用基于模板的策略模式)
- MOOC清华《面向对象程序设计》第3章:对象组合实验
- Leetcode c语言-3Sum
- RocketMQ概述
- @Component、@Repository、@Service、@Controller区别
- 【万云独家译文】DPOS共识算法——缺失的白皮书
- Sass基础--变量与导入
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v3.0(自定义数据类型)
- 错误码: 1630 FUNCTION count does not exist. Check the 'Function Name Parsing and Resolution' section in
- 剑指offer——40.数组中只出现一次的数字
- 虹膜识别与虹膜定位
- linux 下fish shell的安装、设置、以及使用
- 9月12日云栖精选夜读:人工智能在细分及新兴领域的应用
- Android开发优化之——使用软引用和弱引用
- c++学习阶段总结(1)
- 11g R2 rac listener 监听配置