Guest User

Untitled

a guest
Jun 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. // long numbers v 1.0
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. string addition(const string& a, const string& b);
  6. string substraction(const string& a, const string& b);
  7. class BigInt {
  8. public:
  9. BigInt()
  10. {
  11. number = "0";
  12. }
  13. BigInt(string N)
  14. {
  15. number = N;
  16. }
  17.  
  18. BigInt& operator=(const BigInt& rhs) {
  19. //проверка на самоприсваивание
  20. if (this == &rhs) {
  21. return *this;
  22. }
  23. number = rhs.number;
  24. return *this;
  25. }
  26.  
  27. friend const BigInt operator-(const BigInt& lhs, const BigInt& rhs)
  28. {
  29. return BigInt(substraction(lhs.number, rhs.number));
  30. }
  31.  
  32.  
  33. friend const BigInt operator+(const BigInt& lhs, const BigInt& rhs)
  34. {
  35. return BigInt(addition(lhs.number, rhs.number));
  36. }
  37.  
  38.  
  39. friend ostream& operator<<(ostream& os, const BigInt& rhs)
  40. {
  41. os << rhs.number;
  42. return os;
  43. }
  44.  
  45. friend bool operator <(BigInt const & lhs, BigInt const & rhs)
  46. {
  47.  
  48.  
  49. return 1;
  50. }
  51.  
  52.  
  53. private:
  54. string number;
  55. };
  56.  
  57. string addition(const string& a, const string& b)
  58. {
  59. string res;
  60. int carry = 0, c;
  61. int a_l = a.length();
  62. int b_l = b.length();
  63.  
  64. if (a_l >= b_l)
  65. {
  66. for (int i = 0; i < a_l; i++)
  67. {
  68. if ((b_l - 1 - i) >= 0)
  69. {
  70. c = (a.at(a_l - 1 - i) - '0') + (b.at(b_l - 1 - i) - '0') + carry;
  71. }
  72. else
  73. {
  74. c = (a.at(a_l - 1 - i) - '0') + carry;
  75. }
  76. carry = c / 10;
  77. res += (c % 10) + '0';
  78. }
  79. }
  80. else
  81. {
  82. for (int i = 0; i < b_l; i++)
  83. {
  84. if ((a_l - 1 - i) >= 0)
  85. {
  86. c = (a.at(a_l - 1 - i) - '0') + (b.at(b_l - 1 - i) - '0') + carry;
  87. }
  88. else
  89. {
  90. c = (b.at(b_l - 1 - i) - '0') + carry;
  91. }
  92. carry = c / 10;
  93. res += (c % 10) + '0';
  94. }
  95. }
  96. if (carry > 0)
  97. {
  98. res += (c / 10) + '0';
  99. }
  100. int l = res.length() >> 1;
  101. char temp;
  102. for (int i = 0; i < l; i++)
  103. {
  104. temp = res[i];
  105. res[i] = res[res.length() - 1 - i];
  106. res[res.length() - 1 - i] = temp;
  107. }
  108. return res;
  109. }
  110.  
  111. string substraction(const string& a, const string& b)
  112. {
  113. string res;
  114.  
  115. int carry = 0, c;
  116. int a_l = a.length();
  117. int b_l = b.length();
  118.  
  119. if (a_l >= b_l)
  120. {
  121. for (int i = 0; i < a_l; i++)
  122. {
  123. if ((b_l - 1 - i) >= 0)
  124. {
  125. c = (a.at(a_l - 1 - i) - '0') - (b.at(b_l - 1 - i) - '0') - carry;
  126. }
  127. else
  128. {
  129. c = (a.at(a_l - 1 - i) - '0') - carry;
  130. }
  131. if (c < 0)
  132. {
  133. c += 10;
  134. carry = 1;
  135. }
  136. else
  137. {
  138. carry = 0;
  139. }
  140. res += (c % 10) + '0';
  141. }
  142. }
  143. else
  144. {
  145. for (int i = 0; i < b_l; i++)
  146. {
  147. if ((a_l - 1 - i) >= 0)
  148. {
  149. c = (a.at(a_l - 1 - i) - '0') + (b.at(b_l - 1 - i) - '0') - carry;
  150. }
  151. else
  152. {
  153. c = (b.at(b_l - 1 - i) - '0') - carry;
  154. }
  155. if (c < 0)
  156. {
  157. c += 10;
  158. carry = 1;
  159. }
  160. else
  161. {
  162. carry = 0;
  163. }
  164. res += (c % 10) + '0';
  165. }
  166. }
  167. int l = res.length() >> 1;
  168. char temp;
  169. for (int i = 0; i < l; i++)
  170. {
  171. temp = res[i];
  172. res[i] = res[res.length() - 1 - i];
  173. res[res.length() - 1 - i] = temp;
  174. }
  175.  
  176. return res;
  177. }
  178.  
  179. // light version
  180. string multiplication(string a, const string& b)
  181. {
  182. string result = "0", t_res;
  183. for (int i = 0; i < b.length(); i++)
  184. {
  185. int l = b[b.length() - 1 - i] - '0';
  186. t_res = "0";
  187. for (int i1 = 0; i1 < l; i1++)
  188. {
  189. t_res = addition(a, t_res);
  190. }
  191. result = addition(result, t_res);
  192. a = a + "0";
  193. }
  194. return result;
  195. }
  196.  
  197. string division(const string& a, const string& b)
  198. {
  199. string result = "0", t_res;
  200.  
  201. return result;
  202. }
  203.  
  204. bool modulo(const string& a, const string& b)
  205. {
  206. bool result;
  207.  
  208. return result;
  209.  
  210. }
  211.  
  212. BigInt fact(const string& N)
  213. {
  214. BigInt a = "0";
  215. for (BigInt i = "0"; i < BigInt("100") ; i = i + BigInt("1"))
  216. {
  217. cout << i << endl;
  218. }
  219.  
  220. return a;
  221. }
  222.  
  223.  
  224. string fibonacci(string N)
  225. {
  226. string index = 0;
  227. string a, b, c;
  228. do {
  229. if (index == "0")
  230. {
  231. a = "0"; b = "0";
  232. }
  233. if (index == "1")
  234. {
  235. a = "1";
  236. }
  237. c = addition(a, b);
  238. a = b;
  239. b = c;
  240. index = addition(index, "1");
  241. } while (c != N);
  242. return c;
  243. }
  244.  
  245. int main()
  246. {
  247. cout << fact("0");
  248.  
  249. return 0;
  250. }
Add Comment
Please, Sign In to add comment