Advertisement
Guest User

Untitled

a guest
May 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. //============================================================================
  2. // Name : BigInteger.cpp
  3. // Author : Oskar Pawica
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <cmath>
  11. #include <algorithm>
  12. #include <cstdlib>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. using namespace std;
  17.  
  18. class BigInteger
  19. {
  20. private:
  21. char* e;
  22. long size;
  23.  
  24. public:
  25. BigInteger() : e(NULL), size(0) {}
  26. BigInteger(long a){
  27. size = 1;
  28. if(a == 0)
  29. {
  30. e = new char[size];
  31. *e = '0';
  32. }
  33. else
  34. {
  35. size += numberOfDigits(abs(a));
  36. e = new char [size];
  37. if(a > 0)
  38. *e = '+';
  39. else {
  40. *e = '-';
  41. a = -a;
  42. }
  43. int i = 1;
  44. while(a >= 1)
  45. {
  46. sprintf(e+i, "%ld", a%10);
  47. a/=10;
  48. i++;
  49. }
  50. }
  51. }
  52. BigInteger(long ssize, bool): size(ssize), e(NULL) {}
  53.  
  54. BigInteger(const char* a){
  55. size = 1;
  56. if(*a == '0'){
  57. e = new char [size];
  58. *e = '0';
  59. }
  60. else
  61. {
  62. char j;
  63. int i = 0;
  64. if(*a == '+' or *a == '-') {
  65. j = *a;
  66. i++;
  67. }
  68. else {
  69. j = '+';
  70. }
  71. while(*(a+i)>=48 and *(a+i)<=57) {
  72. size++;
  73. i++;
  74. }
  75. e = new char [size];
  76. *e = j;
  77. i--;
  78. int k = 1;
  79. while(*(a+i)>=48 and *(a+i)<=57) {
  80. *(e+k) = *(a+i);
  81. i--;
  82. k++;
  83. }
  84. }
  85.  
  86. }
  87.  
  88. virtual ~BigInteger()
  89. {
  90. delete [] e;
  91. }
  92.  
  93. BigInteger (const BigInteger &b):size(b.size)
  94. {
  95. e = new char[b.size];
  96. strcpy(e, b.e);
  97. }
  98.  
  99. BigInteger operator +(const BigInteger &b) const
  100. {
  101. if(e[0] == '0')
  102. return b;
  103. if(b.e[0] == '0')
  104. return *this;
  105. //if(e[0] == '-')
  106. //return b-(*this);
  107. //if(b.e[0] == '-')
  108. //return *this-b;
  109. if(b.e[0] == '-' and e[0] == '-')
  110. return -((*this)+b);
  111.  
  112. BigInteger temp(this->size, true);
  113. temp.e = new char [this->size];
  114. temp.e[0] = '+';
  115. unsigned int i = 1;
  116. char temp_c;
  117. bool t = false;
  118. while (i <strlen(this->e) and i <strlen(b.e)) {
  119. temp_c = this->e[i]+b.e[i]-48;
  120. if(t) {
  121. temp_c+=1;
  122. t = false;
  123. }
  124. if(temp_c-48 > 10) {
  125. temp_c = (temp_c-48)%10;
  126. t = true;
  127. }
  128. temp.e[i] = temp_c;
  129. ++i;
  130. }
  131. if (i<strlen(this->e)) {
  132. memcpy(temp.e+i, e+i, strlen(this->e)-i);
  133.  
  134. }
  135. else if (i<strlen(this->e)) {
  136. memcpy(temp.e+i, b.e+i, strlen(b.e)-i);
  137. }
  138. if(t) {
  139. BigInteger temp2(this->size+1, true);
  140. strcpy(temp2.e, temp.e);
  141. temp2.e[size] = '1';
  142. return temp2;
  143. }
  144. else
  145. return temp;
  146. }
  147.  
  148. BigInteger operator +(const long &a) const {
  149. BigInteger temp(a);
  150. return *this+a;
  151. }
  152.  
  153. BigInteger operator +=(const BigInteger &b) {
  154. return *this+b;
  155. }
  156.  
  157. BigInteger operator +=(const long &a) {
  158. return *this+a;
  159. }
  160.  
  161. /*
  162. BigInteger &operator *(const BigInteger &b)
  163. {
  164. BigInteger temp;
  165. temp = *this;
  166. delete [] e;
  167. e = new char [b.size+temp.size];
  168.  
  169. }*/
  170.  
  171. BigInteger operator -()
  172. {
  173. BigInteger temp(*this);
  174. if (*(this->e) == '-')
  175. temp.e[0] = '+';
  176. else if (*(this->e) == '+')
  177. temp.e[0] = '-';
  178. return temp;
  179. }
  180.  
  181. BigInteger &operator =(const BigInteger &b)
  182. {
  183. size = b.size;
  184. delete [] e;
  185. e = new char [size];
  186. memcpy(e,b.e,size*sizeof(char));
  187. return *this;
  188. }
  189.  
  190. char &operator [](const int b) {
  191. return e[size-1-b];
  192. }
  193.  
  194. long numberOfDigits(long a) {
  195.  
  196. if(a<=9){
  197. return 1;
  198. }
  199. return 1 + numberOfDigits(a/10);
  200. }
  201.  
  202. friend ostream& operator <<(ostream& os, BigInteger& b);
  203. friend istream& operator >>(istream& is, BigInteger& b);
  204. };
  205.  
  206. ostream& operator<<(ostream& os, BigInteger& b)
  207. {
  208. if (*b.e == '0')
  209. os << '0';
  210. else{
  211. if (*b.e == '-')
  212. os << '-';
  213. for (int i = b.size-1; i > 0; i--)
  214. os << *(b.e+i);
  215. }
  216. return os;
  217. }
  218.  
  219. /*istream& operator >>(istream& is, BigInteger& b)
  220. {
  221. }*/
  222.  
  223. int main() {
  224. long a = 150;
  225. char * b = "120";
  226.  
  227. BigInteger bigint(a);
  228. BigInteger bigint2(b);
  229. BigInteger bigint3;
  230. bigint3 = (bigint+bigint2);
  231. cout<<"BI1: "<<bigint<<endl;
  232. cout<<"BI2: "<<bigint2<<endl;
  233. cout<<"BI3: "<<bigint3<<endl;
  234. cout<<"BI1 <- BI2"<<endl<<endl;
  235.  
  236. bigint = bigint2;
  237. cout<<"BI1: "<<bigint<<endl;
  238. cout<<"BI2: "<<bigint2<<endl;
  239. bigint2 = -bigint2;
  240. cout<<"BI2: "<<bigint2<<endl;
  241. cout<<bigint2[1]<<endl;
  242. cout<<"KONIEC"<<endl;
  243. return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement