Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module mean_mark
  3.  * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
  4.  * @brief Templates for mean_mark, mean_mark_student, remove_greater
  5.  * @date 13.06.2009
  6.  */
  7.  
  8. #ifndef MEAN_MARK_H
  9. #define MEAN_MARK_H
  10.  
  11. #include <typeinfo>
  12. #include <iostream>
  13.  
  14. #undef SOLVED_3
  15. #define SOLVED_3
  16.  
  17. namespace Ti
  18. {
  19. /**
  20.   * @method mean_mark
  21.   * @brief computes mean mark in the range [first,last)
  22.   * @param first forward iterator to the initial positions in a sequence
  23.   * @param last forward iterator to the final positions in a sequence
  24.   * @return computed mean mark
  25.   * @globalvars none
  26.   * @exception none
  27.   * @pre all objects in the sequence must have a method mark returning a type
  28.   * convertible to double
  29.   * @post none
  30.   */
  31. template <typename Iter>
  32. double mean_mark(Iter first, Iter last)
  33. {
  34. double result = 0;
  35. unsigned count = 0;
  36. for(; first != last; ++first)
  37. {
  38. result += (*first)->mark();
  39. ++count;
  40. }
  41. return (count == 0) ? 0 : result / count;
  42. }
  43.  
  44. /**
  45.   * @method mean_mark_student
  46.   * @brief computes mean mark of objects of type Student in the range [first,last)
  47.   * (using RTTI)
  48.   * @param first forward iterator to the initial positions in a sequence
  49.   * @param last forward iterator to the final positions in a sequence
  50.   * @return computed mean mark of objects of type Student
  51.   * @globalvars none
  52.   * @exception none
  53.   * @pre All objects in the sequence must have a method mark returning a type
  54.   * convertible to double. And type Stundent must exist
  55.   * @post none
  56.   */
  57. template <typename Iter>
  58. double mean_mark_student(Iter first, Iter last)
  59. {
  60. double result = 0;
  61. unsigned count = 0;
  62. for(; first != last; ++first)
  63. {
  64. /*if (typeid(*(*first)) != typeid(Student))
  65.   continue;*/
  66. Student *s = dynamic_cast<Student *>(&(*(*first)));
  67. if (s == NULL)
  68. continue;
  69. result += s->mark();
  70. ++count;
  71. }
  72. return (count == 0) ? 0 : result / count;
  73. }
  74.  
  75. /**
  76.   * @method remove_greater
  77.   * @brief Removes from the range [first,last) the elements with a mark greater
  78.   * than mark and returns an iterator to the new end of the range,
  79.   * which now includes only elements with a mark less than mark.
  80.   * @param first forward iterator to the initial positions in a sequence
  81.   * @param last forward iterator to the final positions in a sequence
  82.   * @param mark maximal value for mark to keep
  83.   * @return A forward iterator pointing to the new end of the sequence,
  84.   * which now includes all the elements with a mark less than mark.
  85.   * @globalvars none
  86.   * @exception none
  87.   * @pre All objects in the sequence must have a method mark returning a type
  88.   * convertible to double.
  89.   * @post This function does not alter the elements past the new end,
  90.   * which keep their old values and are still accessible.
  91.   */
  92. template <class ForwardIterator>
  93. ForwardIterator remove_greater (ForwardIterator first, ForwardIterator last, int mark)
  94. {
  95. ForwardIterator result = first;
  96. for (; first != last; ++first)
  97. {
  98. if ((*first)->mark() <= mark)
  99. {
  100. *result = *first;
  101. ++result;
  102. }
  103. }
  104. return result;
  105. }
  106. }
  107.  
  108. #endif
  109.  
  110. /* vim: set et sw=2 ts=2: */
  111.