fork download
  1. #ifndef modulus_h_
  2. #define modulus_h_
  3.  
  4. #include <iostream>
  5.  
  6.  
  7. /*
  8. Represents a number (or similarly behaving class) within the range [0,max_value)
  9.  
  10. A member of the T type must be:
  11.  1. convertable from int,
  12.  2. comparable to ints (via '<')
  13.  3. comparable to itself (via '=='), and
  14.  4. addable/subtractable from ints (or Ts - as they can be converted via 1.)
  15.  
  16. Parameters that fit that description include
  17.  * int, unsigned, long, char
  18.  * double, float
  19.  * enum
  20.  * or, even a Modulus instance itself
  21. */
  22. template<typename T, int _max_value>
  23. class Modulus {
  24. public:
  25. /*
  26. Typedefs/access to template parameters
  27. */
  28. typedef T value_type;
  29. static const int max_value = _max_value;
  30.  
  31. /*
  32. Conversion operators
  33. */
  34. Modulus(T val=0): val(val) {rectify();}
  35. Modulus &operator =(T rhs) {val=rhs; rectify(); return *this;}
  36. operator T () const {return val;}
  37. T value() const {return (T)*this;}
  38.  
  39. virtual void rectify() {
  40. while ( val < 0 ) val = val + max_value;
  41. //val = val % max_value; doesn't work for float/double,
  42. //so we do it the long way
  43. while(max_value < val) val = val - max_value;
  44. if(val == max_value ) val=0;
  45. }
  46.  
  47. /*
  48. Increment/Decrement
  49. */
  50. Modulus& operator ++() { val = val+1; rectify(); return *this; }
  51. Modulus operator ++(int) {T old=val; val = val+1; rectify(); return Modulus(old);}
  52.  
  53. Modulus& operator --() { val = val-1; rectify(); return *this; }
  54. Modulus operator --(int) {T old=val; val = val-1; rectify(); return Modulus(old);}
  55.  
  56. /*
  57. Input
  58. (output is handled by automatic conversion to value_type)
  59. */
  60. template <typename T_2, int max_2>
  61. friend std::istream& operator >> (std::istream& in, Modulus<T,max_value> &j);
  62.  
  63. /*
  64. OP= Operators
  65. #define OPERATOR(OP) \
  66. Modulus& operator OP(T x) { \
  67. val OP x; \
  68. rectify(); \
  69. return *this; \
  70. }
  71.  
  72. OPERATOR(+=)
  73. OPERATOR(-=)
  74. OPERATOR(*=)
  75. OPERATOR(/=)
  76. OPERATOR(%=)
  77. OPERATOR(&=)
  78. OPERATOR(|=)
  79. OPERATOR(^=)
  80. OPERATOR(<<=)
  81. OPERATOR(>>=)
  82.  
  83. #undef OPERATOR
  84. */
  85.  
  86. private:
  87. T val;
  88. };
  89.  
  90.  
  91. template <typename T, int max_value>
  92. std::istream& operator >> (std::istream& in, Modulus<T,max_value> &j){
  93. in >> j.val;
  94. j.rectify();
  95. return in;
  96. }
  97.  
  98. template<typename T_from, typename T_to>
  99. T_to convert(T_from from) {
  100. typedef typename T_to::value_type T_to_type;
  101. typename T_from::value_type max_from = T_from::max_value;
  102. T_to_type max_to = T_to::max_value;
  103.  
  104. T_to out = T_to_type( (from * max_to) / max_from);
  105. return out;
  106. }
  107.  
  108.  
  109. #endif
  110.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
stdout
Standard output is empty