fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename Num>
  5. class Val {
  6. public:
  7. Val(Num value): firstvalue(value), value(value), state(0) {}
  8. operator Num() const { return value; }
  9. const Val operator - () const {
  10. switch(state){
  11. case 0: return Val(firstvalue, fkt(firstvalue), 1);
  12. case 1: return Val(firstvalue, 1 + firstvalue, 2);
  13. case 2: return Val(firstvalue, firstvalue > 0 ? firstvalue : -firstvalue, 3);
  14. case 3: return Val(firstvalue, - firstvalue, 4);
  15. case 4: return Val(firstvalue, firstvalue, 0);
  16. }
  17. }
  18. private:
  19. Val(Num firstvalue, Num value, int state):
  20. firstvalue(firstvalue), value(value), state(state) {}
  21.  
  22. static Num fkt(Num num) { return num > 0 ? num * fkt(num-1) : 1; }
  23.  
  24. const Num firstvalue;
  25. Num value;
  26. int state;
  27. };
  28.  
  29. int main() {
  30. Val<int> v(4);
  31.  
  32. cout << v << " "
  33. << -v << " "
  34. << -(-v) << " "
  35. << -(-(-v)) << " "
  36. << -(-(-(-v))) << " "
  37. << -(-(-(-(-v))));
  38. return 0;
  39. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
4 24 5 4 -4 4