Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <vector>
  7.  
  8. class CIntN {
  9. protected:
  10. std::string filename;
  11. std::vector<int> nums;
  12. class Adder {
  13. int carry;
  14. public:
  15. Adder(): carry(0) {}
  16. int operator()(int x, int y) {
  17. int oldcarry = carry;
  18. carry = (x + y + oldcarry) / 10;
  19. return (x + y + oldcarry) % 10;
  20. }
  21. };
  22. class Invertor {
  23. public:
  24. int operator()(int x) {
  25. return 9 - x;
  26. }
  27. };
  28. public:
  29. virtual int output(const char* FileName = NULL) = 0;
  30. virtual CIntN& with(const char* str = NULL);
  31. virtual CIntN& operator=(const CIntN&);
  32. virtual CIntN* operator-() const = 0;
  33. virtual CIntN& operator+=(const CIntN&);
  34. virtual CIntN& operator-=(const CIntN&);
  35. virtual CIntN& negate();
  36. virtual ~CIntN() {
  37. };
  38. };
  39.  
  40. CIntN& CIntN::with(const char *str) {
  41. std::stringstream ss((std::string(str)));
  42. int num;
  43. ss >> num;
  44. ss >> filename;
  45. int n;
  46. ss >> n;
  47. nums = std::vector<int>(n + 1, 0);
  48. std::string number;
  49. ss >> number;
  50. std::string::reverse_iterator end = number.rend();
  51. std::vector<int>::reverse_iterator vit = nums.rbegin();
  52. if (number[0] == '-' || number[0] == '+') --end;
  53. for (std::string::reverse_iterator it = number.rbegin(); it != end
  54. && vit != nums.rend(); ++it, ++vit) {
  55. *vit = *it - '0';
  56. }
  57. if (number[0] == '-') {
  58. negate();
  59. }
  60. return *this;
  61. }
  62.  
  63. CIntN& CIntN::negate() {
  64. std::transform(nums.begin(), nums.end(), nums.begin(), Invertor());
  65. int carry = (*(nums.rbegin()) + 1) / 10;
  66. *(nums.rbegin()) = (*(nums.rbegin()) + 1) % 10;
  67. std::vector<int>::reverse_iterator it = nums.rbegin() + 1;
  68. for (; it < nums.rend(); ++it) {
  69. int oldcarry = carry;
  70. carry = (*it + oldcarry) / 10;
  71. *it = (*it + oldcarry) % 10;
  72. }
  73. return *this;
  74. }
  75.  
  76. CIntN& CIntN::operator=(const CIntN& other) {
  77. nums.clear();
  78. std::copy(other.nums.begin(), other.nums.end(), std::back_inserter(nums));
  79. return *this;
  80. }
  81.  
  82. CIntN& CIntN::operator+=(const CIntN& other) {
  83. if (nums.size() != other.nums.size()) {
  84. std::cerr << "Attempt to add CIntN's with different sizes!" << std::endl;
  85. }
  86. std::transform(nums.rbegin(), nums.rend(), other.nums.rbegin(), nums.rbegin(), Adder());
  87. return *this;
  88. }
  89.  
  90. CIntN& CIntN::operator-=(const CIntN& other) {
  91. CIntN *tmp = -other;
  92. *this += *tmp;
  93. delete tmp;
  94. return *this;
  95. }
  96.  
  97. class CIntN0: public CIntN {
  98. public:
  99. CIntN0() {
  100. nums = std::vector<int>();
  101. }
  102. CIntN0(const CIntN0& n0) {
  103. nums = std::vector<int>(n0.nums);
  104. }
  105. int output(const char* FileName = NULL) {
  106. std::ofstream of;
  107. if (FileName) {
  108. of.open(FileName);
  109. } else {
  110. of.open(filename.c_str());
  111. }
  112. of << nums.size() - 1 << " ";
  113. std::vector<int>::iterator it = nums.begin();
  114. if (nums[0] >= 5) {
  115. of << '-';
  116. negate();
  117. }
  118. while (*it == 0 && it != nums.end()) ++it;
  119. if (it == nums.end()) {
  120. of << 0;
  121. } else {
  122. std::copy(it, nums.end(), std::ostream_iterator<int>(of, ""));
  123. }
  124. of << std::endl;
  125. of.close();
  126. return 0;
  127. }
  128. CIntN* operator-() const {
  129. CIntN0 *ci = new CIntN0(*this);
  130. ci->negate();
  131. return ci;
  132. }
  133. ~CIntN0() {
  134. }
  135. };
  136.  
  137. CIntN0 operator+(const CIntN0 &first, const CIntN0 &second) {
  138. CIntN0 res(first);
  139. res += second;
  140. return res;
  141. }
  142.  
  143. CIntN0 operator-(const CIntN0 &first, const CIntN0 &second) {
  144. CIntN0 res(first);
  145. res -= second;
  146. return res;
  147. }
  148.  
  149. class CIntN1: public CIntN {
  150. public:
  151. CIntN1() {
  152. nums = std::vector<int>();
  153. }
  154. CIntN1(const CIntN1& n1) {
  155. nums = std::vector<int>(n1.nums);
  156. }
  157. int output(const char* FileName = NULL) {
  158. std::ofstream of;
  159. if (FileName) {
  160. of.open(FileName);
  161. } else {
  162. of.open(filename.c_str());
  163. }
  164. of << nums.size() - 1 << std::endl;
  165. std::vector<int>::iterator it = nums.begin();
  166. if (nums[0] >= 5) {
  167. of << '-';
  168. negate();
  169. }
  170. while (*it == 0 && it != nums.end()) ++it;
  171. if (it == nums.end()) {
  172. of << 0;
  173. } else {
  174. std::copy(it, nums.end(), std::ostream_iterator<int>(of, ""));
  175. }
  176. of << std::endl;
  177. of.close();
  178. return 0;
  179. }
  180. CIntN* operator-() const {
  181. CIntN1 *ci = new CIntN1(*this);
  182. ci->negate();
  183. return ci;
  184. }
  185. ~CIntN1() {
  186. }
  187. };
  188.  
  189. CIntN1 operator+(const CIntN1 &first, const CIntN1 &second) {
  190. CIntN1 res(first);
  191. res += second;
  192. return res;
  193. }
  194.  
  195. CIntN1 operator-(const CIntN1 &first, const CIntN1 &second) {
  196. CIntN1 res(first);
  197. res -= second;
  198. return res;
  199. }
  200.  
  201. class CIntNFactory {
  202. int i;
  203. public:
  204. CIntNFactory(int i): i(i) {};
  205. CIntN *createCIntN(const char* str);
  206. ~CIntNFactory() {
  207. }
  208. };
  209.  
  210. CIntN *CIntNFactory::createCIntN(const char *str) {
  211. CIntN *ci;
  212. if (i == 0) {
  213. ci = new CIntN0();
  214. } else if (i == 1) {
  215. ci = new CIntN1();
  216. } else return NULL;
  217. ci->with(str);
  218. return ci;
  219. }
  220.  
  221. CIntN *CreateCIntN(const char *str, CIntNFactory** factories) {
  222. return factories[str[0] - '0']->createCIntN(str);
  223. }
  224.  
  225. int main() {
  226. std::string str;
  227. CIntNFactory **factories = new CIntNFactory*[2];
  228. factories[0] = new CIntNFactory(0);
  229. factories[1] = new CIntNFactory(1);
  230. std::ifstream in("input.txt");
  231. std::vector<CIntN*> arr;
  232. while (std::getline(in, str)) {
  233. arr.push_back(CreateCIntN(str.c_str(), factories));
  234. }
  235. CIntN *tmp = -(*(dynamic_cast<CIntN0*>(arr[0])));
  236. tmp->output("output6.txt");
  237. delete tmp;
  238. (*(dynamic_cast<CIntN1*>(arr[1])) + *(dynamic_cast<CIntN1*>(arr[3]))).output("output5.txt");
  239. for (std::vector<CIntN*>::iterator it = arr.begin(); it != arr.end(); ++it) {
  240. (*it)->output();
  241. delete *it;
  242. }
  243. delete factories[0];
  244. delete factories[1];
  245. delete[] factories;
  246. return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement