fork download
  1. #include <iostream>
  2.  
  3. class reverser
  4. {
  5. public:
  6. reverser(int orig) : orig(orig), done(false) {}
  7. int result() const { return done ? res : reverse(); }
  8. private:
  9. int reverse() const
  10. {
  11. res = 0;
  12. int n = orig;
  13. while (n)
  14. {
  15. int last = get_last_digit(n);
  16. add_last_digit(res, last);
  17. remove_last_digit(n);
  18. }
  19. done = true;
  20. return res;
  21. }
  22.  
  23. static void remove_last_digit(int& n) {
  24. n /= 10;
  25. }
  26.  
  27. static int get_last_digit(int& n) {
  28. return n % 10;
  29. }
  30.  
  31. static void add_last_digit(int& n, int digit) {
  32. n = n * 10 + digit;
  33. }
  34.  
  35. mutable int res;
  36. const int orig;
  37. mutable bool done;
  38. };
  39.  
  40. int main()
  41. {
  42. int n = 1023456;
  43. reverser* rev = new reverser( n );
  44. std::cout << rev->result() << std::endl;
  45. delete rev;
  46. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
6543201