MOOC清华《面向对象程序设计》第5章:OOP版谁做的好事

xiaoxiao2021-02-27  170

#include <iostream> using namespace std; class Student{ char _theMan; bool _isEqual; public: Student(bool isEqual, char theMan): _isEqual(isEqual), _theMan(theMan){ } bool operator() (char thisMan); }; bool Student::operator() (char thisMan){ return _isEqual ? (thisMan == _theMan) : (thisMan != _theMan); } char solve(int number, int correct, Student* student){ for(int i = 0; i < number; i++){ int count = 0; char thisMan = 'A' + i; for(int j = 0; j < number; j++) count += student[j](thisMan); if(count == correct) return thisMan; } return '\0'; } int main(){ Student student[] = {Student(false, 'A'), Student(true, 'C'), Student(true, 'D'), Student(false, 'D')}; char theMan = solve(4, 3, student); if(theMan != '\0'){ cout << "It is " << theMan << endl; } else{ cout << "No solve." << endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-14493.html

最新回复(0)