2010年8月12日 星期四

老師常考的分數區間

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <limits>

template<typename ScoreType>
class InclusiveRange {
private:
  char const *desc;
  ScoreType lower, upper;
public:
  InclusiveRange(char const *description, ScoreType lowerBound, ScoreType upperBound)
    : desc(description), lower(lowerBound), upper(upperBound) {  }
  InclusiveRange(InclusiveRange const &source)
    : desc(source.desc), lower(source.lower), upper(source.upper) {  }
public:
  bool isInRange(ScoreType score) const {
    return lower <= score && score <= upper;
  }
  char const *getDescription() const {
    return desc;
  }
};

int main() {
  using namespace std;
  typedef InclusiveRange<int> Range;

  vector<Range> ranges ;
  ranges.push_back(Range("90%",  90, numeric_limits<int>::max()));
  ranges.push_back(Range("80%",  80,  89));
  ranges.push_back(Range("70%",  70,  79));
  ranges.push_back(Range("60%",  60,  69));
  ranges.push_back(Range("Under 60%", numeric_limits<int>::min(), 59));

  int score = 99;
  for(vector<Range>::const_iterator it = ranges.begin(); it != ranges.end(); ++it) {
    switch(it->isInRange(score)) {
    case true:
      cout << it->getDescription() << endl;
      break;
    case false:
      break;
    }
  }
  system("pause");
  return EXIT_SUCCESS;
}

沒有留言:

張貼留言