Download | Plain Text | Line Numbers


/**
 * @module mean_mark
 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
 * @brief  Templates for mean_mark, mean_mark_student, remove_greater
 * @date   13.06.2009
 */
 
#ifndef MEAN_MARK_H
#define MEAN_MARK_H
 
#include <typeinfo>
#include <iostream>
 
#undef SOLVED_3
#define SOLVED_3
 
namespace Ti
{
  /**
   * @method mean_mark
   * @brief  computes mean mark in the range [first,last)
   * @param  first  forward iterator to the initial positions in a sequence
   * @param  last   forward iterator to the final positions in a sequence
   * @return computed mean mark
   * @globalvars none
   * @exception  none
   * @pre  all objects in the sequence must have a method mark returning a type
   *       convertible to double
   * @post none
   */
  template <typename Iter>
    double mean_mark(Iter first, Iter last)
    {
      double result = 0;
      unsigned count = 0;
      for(; first != last; ++first)
      {
        result += (*first)->mark();
        ++count;
      }
      return (count == 0) ? 0 : result / count;
    }
 
  /**
   * @method mean_mark_student
   * @brief  computes mean mark of objects of type Student in the range [first,last)
   *         (using RTTI)
   * @param  first  forward iterator to the initial positions in a sequence
   * @param  last   forward iterator to the final positions in a sequence
   * @return computed mean mark of objects of type Student
   * @globalvars none
   * @exception  none
   * @pre  All objects in the sequence must have a method mark returning a type
   *       convertible to double. And type Stundent must exist
   * @post none
   */
  template <typename Iter>
    double mean_mark_student(Iter first, Iter last)
    {
      double result = 0;
      unsigned count = 0;
      for(; first != last; ++first)
      {
        /*if (typeid(*(*first)) != typeid(Student))
          continue;*/
        Student *s = dynamic_cast<Student *>(&(*(*first)));
        if (s == NULL)
          continue;
        result += s->mark();
        ++count;
      }
      return (count == 0) ? 0 : result / count;
    }
 
  /**
   * @method remove_greater
   * @brief  Removes from the range [first,last) the elements with a mark greater
   *         than mark and returns an iterator to the new end of the range,
   *         which now includes only elements with a mark less than mark.
   * @param  first  forward iterator to the initial positions in a sequence
   * @param  last   forward iterator to the final positions in a sequence
   * @param  mark   maximal value for mark to keep
   * @return A forward iterator pointing to the new end of the sequence,
   *         which now includes all the elements with a mark less than mark.
   * @globalvars none
   * @exception  none
   * @pre  All objects in the sequence must have a method mark returning a type
   *       convertible to double.
   * @post This function does not alter the elements past the new end,
   *       which keep their old values and are still accessible.
   */
  template <class ForwardIterator>
    ForwardIterator remove_greater (ForwardIterator first, ForwardIterator last, int mark)
    {
      ForwardIterator result = first;
      for (; first != last; ++first)
      {
        if ((*first)->mark() <= mark)
        {
          *result = *first;
          ++result;
        }
      }
      return result;
    }
}
 
#endif
 
/* vim: set et sw=2 ts=2: */