MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.4(单科及格与总分及格)
来源:互联网 发布:mysql limit缺陷 编辑:程序博客网 时间:2024/04/28 23:49
新需求:如何分别统计单科及格的人数和总分及格的人数?这种算法似乎在Microsoft Office. Excel中用得很多。视频中漏掉了一页课件,下载的PPT中有。
//main.cpp#include <iostream>#include <cstdlib>#include "ArrayCollection.h"#include "LinkedListCollection.h"#include "Score.h"#include "isPass.h"using namespace std;template <class _iterator, class _isPass>void analyze(_iterator begin, _iterator end, _isPass isPass){int passed = 0, count = 0;for(_iterator p = begin; p != end; p++){if(isPass(p))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;for(int i = 0; i < 3; i++)collection4.addFirst(sarray[i]);analyze(sarray, sarray + 3, IsPass_ForEach<Score*, Score>(Score(70, 60, 60)));analyze(collection3.begin(), collection3.end(), IsPass_ForEach<Score*, Score>(Score(50, 60, 60)));analyze(collection4.begin(), collection4.end(), IsPass_ForEach<LinkedListIterator<Score>, Score>(Score(60, 60, 60)));cout << endl;analyze(sarray, sarray + 3, IsPass_Total<Score*, Score>(Score(70, 60, 60)));analyze(collection3.begin(), collection3.end(), IsPass_Total<Score*, Score>(Score(50, 60, 60)));analyze(collection4.begin(), collection4.end(), IsPass_Total<LinkedListIterator<Score>, Score>(Score(60, 60, 60)));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);}bool operator>=(Score pass){return (value[0] >= pass.value[0] && value[1] >= pass.value[1] && value[2] >= pass.value[2]);}float getTotal(){return value[0] + value[1] + value[2];}};ostream& operator<<(ostream& out, const Score& s){out << '{' << s.value[0] << ',' << s.value[1] << ',' << s.value[2] << '}';return out;}//注意这个流运算符重载不在类Score的内部定义,而要拿到外面来 #endif
//IsPass.h#ifndef IsPass_h#define IsPass_htemplate <class _iterator, class T>class IsPass_ForEach{T _pass;public:IsPass_ForEach(const T& pass):_pass(pass){}bool operator()(const _iterator& p){return (*p >= _pass);}};template <class _iterator, class T>class IsPass_Total{T _pass;public:IsPass_Total(const T& pass):_pass(pass){}bool operator()(const _iterator& p){return (p->getTotal() >= _pass.getTotal());}};#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
//LinkedListCollection.h#ifndef LinkedListCollection_h#define LinkedListCollection_htemplate <class T>struct LinkedListNode{T _data;LinkedListNode *_next;LinkedListNode():_next(NULL){}LinkedListNode(T data):_data(data), _next(NULL){}LinkedListNode(T data, LinkedListNode<T>* next):_data(data), _next(next){}//注意上面这一行的第二个形参,其类型必须是LinkedListNode<T>*,否则//将与 LinkedListCollection::addFirst(T&) 中的_head的类型不匹配,编译器会报错! };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;}};template <class T>class LinkedListCollection{LinkedListNode<T>* _head;public:LinkedListCollection():_head(NULL){}~LinkedListCollection(){clear();}bool empty(){return (_head == NULL);}void addFirst(const T& data){_head = new LinkedListNode<T>(data, _head);}bool removeFirst(){if(_head != NULL){LinkedListNode<T>* p = _head;_head = _head->_next;delete p;return true;}else{return false;}}T* getFirst(){return (_head != NULL) ? &(_head->_data) : NULL;}LinkedListNode<T>* lastNode(){LinkedListNode<T>* p;for(p = _head; p->_next != NULL; p = p->_next) ;return p;}void addLast(const T& data){if(_head != NULL){lastNode()->_next = new LinkedListNode<T>(data);}else{_head = new LinkedListNode<T>(data);}}T* getLast(){return (_head != NULL) ? &(lastNode()->_data) : NULL;}bool removeLast(){if(_head != NULL){if(_head->_next != NULL){LinkedListNode<T>* p;for(p = _head; p->_next != NULL; p = p->_next) ;delete p->_next;p->_next = NULL;return true;}else{delete _head;_head = NULL;return true;}}elsereturn false;}void clear(){while(removeFirst()) ;}LinkedListIterator<T> begin(){return LinkedListIterator<T>(_head);}LinkedListIterator<T> end(){return LinkedListIterator<T>(NULL);}};#endif
阅读全文
0 0
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.4(单科及格与总分及格)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.1(“记住”及格分数线)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.2(用文件输入及格线)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.3(采用函数对象)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.0(某科及格线不是60分)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v1.0(采用迭代器模式)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v2.0(采用迭代器、容器、模板方法)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v3.0(自定义数据类型)
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v4.0(定义抽象结构)
- sqlserver 统计及格率
- MOOC清华《VC++面向对象与可视化程序设计》第5章:菜单资源例程(创建统计计算菜单项)
- MOOC清华《VC++面向对象与可视化程序设计》第3章:Windows文本-例(4)竖排的古诗
- MOOC清华《VC++面向对象与可视化程序设计》第4章:键盘实例程序(组合键)
- MOOC清华《VC++面向对象与可视化程序设计》第4章:键盘操作例程(五个对话框)
- MOOC清华《VC++面向对象与可视化程序设计》第4章:鼠标实例程序(光标九宫格)
- MOOC清华《VC++面向对象与可视化程序设计》第4章:鼠标操作例程(灰白选框)
- 学生成绩及格与不及格
- 一个MM考试东及格的理由!(搞笑)
- Lambda 表达式
- 部分jar包及功能
- 启动docker容器时虚拟机端口转发外部无法访问
- 数字图像处理成长之路2:mat与通道
- elasticsearch5.4.0 kibana小问题记录
- MOOC清华《面向对象程序设计》第7章:统计考试及格率v5.4(单科及格与总分及格)
- The VMRC console has disconnected solution
- Android BLE开发之Android手机与BLE终端通信的几篇文章
- Android基于元注解实现一个简单的字段注解
- Android ViewPager的工作原理
- hiho字符串
- equals和==的区别
- js excel导出 自定义excel文件名
- 核心交换机哪种好?