summaryrefslogtreecommitdiffstats
path: root/ue5/mean_mark.hpp
blob: 614a67119b5eb9dc014c75d1aae1f91cfcb9e8c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * @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: */