Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. // workspace.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <deque>
  7. #include <functional>
  8. #include <string>
  9. #include <list>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13.  
  14. union Integer;
  15.  
  16. union Integer
  17. {
  18. public:
  19. const Integer& fred;
  20.  
  21. // recurse on everything below fred
  22. const Integer& recurse(const function<const Integer&(const Integer&,const Integer&)> fred2,const Integer& fred3) const
  23. {
  24. if (data)
  25. {
  26. return [&](const Integer& fred) mutable throw() -> const Integer& { return fred.recurse(fred2,fred2(fred3,fred));} (fred);
  27. } else return fred3;
  28. }
  29.  
  30. bool isZero() const
  31. {
  32. return data == 0;
  33. }
  34.  
  35. private:
  36. Integer() : fred(*this){};
  37. Integer(const Integer& fred2) : fred(fred2){};
  38. int *data;
  39. friend class EnterpriseIntegerFactory;
  40. friend ostream& operator<<(ostream&,const Integer&);
  41. };
  42.  
  43. ostream& operator<<(ostream& fred2,const Integer& fred3)
  44. {
  45. fred2 << "{";
  46. fred3.recurse([&](const Integer& fred,const Integer& fred4) mutable throw() -> const Integer& { fred2 << fred4 << ","; return fred;},fred3);
  47. return fred2 << "}";
  48. }
  49.  
  50. class EnterpriseIntegerFactory
  51. {
  52. list<Integer*> m_allocPool;
  53. public:
  54. Integer Zero;
  55. EnterpriseIntegerFactory()
  56. {
  57. memset(&Zero.data,0,sizeof(int*));
  58. }
  59. Integer& CreateSucc(const Integer& fred)
  60. {
  61. Integer* ret = new Integer(fred);
  62. m_allocPool.push_back(ret);
  63. return *ret;
  64. }
  65.  
  66. ~EnterpriseIntegerFactory()
  67. {
  68. for_each(m_allocPool.begin(),m_allocPool.end(),[](Integer* val) -> void { delete val;});
  69. }
  70. };
  71.  
  72. class EnterpriseOperationFactory;
  73.  
  74. class CurriedOperation
  75. {
  76. public:
  77. CurriedOperation(EnterpriseOperationFactory *p_Factory,const Integer& a) : fred(a)
  78. {
  79. m_Factory = p_Factory;
  80. }
  81. protected:
  82. const Integer& fred;
  83. EnterpriseOperationFactory *m_Factory;
  84. virtual const Integer& doOp(const Integer& b)=0;
  85. friend class EnterpriseOperationFactory;
  86. };
  87.  
  88. class EnterpriseOperationFactory
  89. {
  90. EnterpriseIntegerFactory* m_Factory;
  91. public:
  92. EnterpriseOperationFactory(EnterpriseIntegerFactory *p_Factory) : m_Factory(p_Factory) {}
  93. class Adder : public CurriedOperation
  94. {
  95. public:
  96. Adder(EnterpriseOperationFactory *p_Factory, const Integer& a) : CurriedOperation(p_Factory,a) {};
  97. const Integer& doOp(const Integer& num)
  98. {
  99. return fred.recurse([&](const Integer& fred3,const Integer& fred2) -> const Integer& { return m_Factory->m_Factory->CreateSucc(fred3);}, num);
  100. }
  101. };
  102.  
  103. class Multiplier : public CurriedOperation
  104. {
  105. public:
  106. Multiplier(EnterpriseOperationFactory *p_Factory, const Integer& a) : CurriedOperation(p_Factory,a) {};
  107. const Integer& doOp(const Integer& num)
  108. {
  109. return fred.recurse([&](const Integer& fred,const Integer& fred2) -> const Integer& { return m_Factory->add(num,fred);}, m_Factory->m_Factory->Zero);
  110. }
  111. };
  112.  
  113. class Factorial : public CurriedOperation
  114. {
  115. public:
  116. Factorial(EnterpriseOperationFactory *p_Factory, const Integer& a) : CurriedOperation(p_Factory,a) {};
  117. const Integer& doOp(const Integer& dummy)
  118. {
  119. return fred.recurse([&](const Integer& fred,const Integer& fred2) -> const Integer& {
  120. return fred.isZero() ? m_Factory->m_Factory->CreateSucc(m_Factory->m_Factory->Zero) : // Return 1 instead of 0.
  121. m_Factory->mult(fred,m_Factory->m_Factory->CreateSucc(fred2)); // Otherwise multiply by iterator.
  122. },m_Factory->m_Factory->CreateSucc(m_Factory->m_Factory->Zero));
  123. }
  124. };
  125.  
  126. const Integer& add(const Integer& a,const Integer& b)
  127. {
  128. Adder adder(this,a);
  129. return adder.doOp(b);
  130. }
  131.  
  132. const Integer& mult(const Integer& a,const Integer& b)
  133. {
  134. Multiplier mult(this,a);
  135. return mult.doOp(b);
  136. }
  137. const Integer& fact(const Integer& a)
  138. {
  139. Factorial fact(this,a);
  140. return fact.doOp(fact.m_Factory->m_Factory->Zero);
  141. }
  142.  
  143.  
  144. };
  145.  
  146.  
  147. int IntegerToInt(const Integer& fred)
  148. {
  149. int i = 0;
  150. fred.recurse([&](const Integer& a,const Integer& b)-> const Integer& {i++; return a;},fred);
  151. return i;
  152. }
  153.  
  154. int _tmain(int argc, _TCHAR* argv[])
  155. {
  156. EnterpriseIntegerFactory factory;
  157. EnterpriseOperationFactory fred(&factory);
  158.  
  159. Integer& one = factory.CreateSucc(factory.Zero);
  160.  
  161. const Integer& onePlusOne = fred.add(one,one);
  162. cout << "1+1: " << IntegerToInt(onePlusOne) << " - " << onePlusOne << endl;
  163.  
  164. system("pause");
  165.  
  166. Integer& five = factory.CreateSucc(factory.CreateSucc(factory.CreateSucc(factory.CreateSucc(factory.CreateSucc(factory.Zero)))));
  167. Integer& three = factory.CreateSucc(factory.CreateSucc(factory.CreateSucc(factory.Zero)));
  168. cout << "5: " << IntegerToInt(five) << " - "<< five << endl;
  169. cout << "3: " << IntegerToInt(three) << " - " << three << endl;
  170.  
  171. const Integer& fivePlusThree = fred.add(five,three);
  172. cout << "5+3: " << IntegerToInt(fivePlusThree) << " - " << fivePlusThree << endl;
  173. Integer& num = factory.CreateSucc(factory.CreateSucc(factory.CreateSucc(factory.CreateSucc(factory.Zero))));
  174. cout << IntegerToInt(num) << " - " << num << endl;
  175. system("pause");
  176.  
  177. const Integer& fiveTimesThree = fred.mult(five,three);
  178. cout << "5*3: " << IntegerToInt(fiveTimesThree) << " - <Set representation too large>" << endl;
  179. system("pause");
  180.  
  181. const Integer& threeFactorial = fred.fact(factory.CreateSucc(three));
  182. cout << "3!: " << IntegerToInt(threeFactorial) << " - " << endl;
  183.  
  184. system("pause");
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement