Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. long int rem;
  6.  
  7. //using namespace std;
  8.  
  9. class BigInt {
  10. public:
  11. char *big;
  12. int big_size;
  13. BigInt();
  14. BigInt(long);
  15. BigInt(std::string);
  16.  
  17. //~BigInt();
  18. int which_is_bigger(BigInt B)
  19. {
  20. if(big_size>B.big_size)
  21. return 1;
  22. else if (big_size<B.big_size)
  23. return 2;
  24. else
  25. for(int i=1; i<big_size; i++)
  26. {
  27. if(big[i]>B.big[i])
  28. return 1;
  29. else if (big[i]<B.big[i])
  30. return 2;
  31. }
  32. return 0;
  33. }
  34.  
  35.  
  36.  
  37. BigInt& operator+(BigInt B)
  38. {
  39.  
  40. if((big[0]=='-' && B.big[0]=='+') && -*this==B)
  41. {
  42. *this = -*this;
  43. BigInt zeros("+0");
  44. return zeros;
  45. }
  46. if((big[0]=='+' && B.big[0]=='-') && *this==-B)
  47. {
  48. B=-B;
  49. BigInt zeros("+0");
  50. return zeros;
  51. }
  52.  
  53. if(big[1]=='0')
  54. return B;
  55. if(B.big[1]=='0')
  56. return *this;
  57.  
  58. int temp_size;
  59. char *tmp_ar;
  60. char *tmp_ar2;
  61. if(big_size>B.big_size)
  62. {
  63. temp_size = big_size+1;
  64. int war = B.big_size;
  65. int roz = big_size - B.big_size;
  66. tmp_ar= new char[big_size];
  67.  
  68.  
  69. for(int i=1; i<B.big_size; i++)
  70. {
  71. tmp_ar[i+roz] = B.big[i];
  72. war = war-1;
  73. if(war==1)
  74. break;
  75. }
  76. for(int i=0; i<=roz; i++)
  77. tmp_ar[i]='0';
  78.  
  79. tmp_ar[0]='+';
  80.  
  81. tmp_ar2=new char[big_size];
  82. for(int i=0; i<big_size; i++)
  83. tmp_ar2[i] = big[i];
  84. }
  85. else
  86. {
  87. temp_size = B.big_size+1;
  88. int war = big_size;
  89. int roz = B.big_size - big_size;
  90. tmp_ar= new char[B.big_size];
  91. for(int i=1; i<big_size; i++)
  92. {
  93. tmp_ar[i+roz] = big[i];
  94. war = war-1;
  95. if(war==1)
  96. break;
  97. }
  98. for(int i=0; i<=roz; i++)
  99. tmp_ar[i]='0';
  100.  
  101. tmp_ar[0]='+';
  102. tmp_ar2=new char[B.big_size];
  103. for(int i=0; i<B.big_size; i++)
  104. tmp_ar2[i] = B.big[i];
  105. }
  106.  
  107. BigInt suma(temp_size, 1);
  108. /*for (int i = 1; i < temp_size; i++){
  109. suma.big[i] = '0';
  110. std::cout <<"S"<< suma.big[i];}*/
  111.  
  112. int flag = 0;
  113. char temp;
  114. //std::cout << "temp_size: " << temp_size << std::endl;
  115. //if(big[0]=='+' && B.big[0] == '+')
  116. //{
  117.  
  118. if (big[0]=='+' && B.big[0] == '+') suma.big[0] = '+';
  119. else if (big[0]=='-' && B.big[0] == '-') suma.big[0] = '-';
  120.  
  121. int b = B.big_size > big_size ? B.big_size -1 : big_size -1 ;
  122. //std::cout << b;
  123.  
  124. for(int i=temp_size-1; i>1; i--, b--)
  125. { //std::cout << "flag" << flag <<std::endl;
  126. suma.big[i] = flag;
  127. flag = 0;
  128.  
  129. temp = (tmp_ar2[b]-'0') + (tmp_ar[b] - '0') + 48;
  130. //std::cout << "temp: " << temp << "!"<< std::endl;
  131. if(temp>57)
  132. {
  133. temp=temp-10;
  134. flag = 1;
  135. suma.big[i] += temp;
  136. }
  137. else if (temp == 57 && suma.big[i] != '0') //kiedy 9
  138. {
  139. temp = '0';
  140. flag = 1;
  141. suma.big[i] = temp;
  142. }
  143.  
  144. else suma.big[i] += temp;
  145.  
  146. //std::cout<< "a "<<suma.big[i] << "c " << std::endl;
  147. if (i == 2)
  148. {
  149. //std::cout << "flag" << flag <<std::endl;
  150. suma.big[i-1] = flag+48;
  151. //std::cout<< "a "<<suma.big[i-1] << " c " << std::endl;
  152. break;
  153. }
  154. }
  155.  
  156.  
  157.  
  158.  
  159. if (suma.big[1] == '0')
  160. {
  161. BigInt sum1(suma.big_size-1,1); //zmiejszamy rozmiar o jedno 0
  162. sum1.big[0] = suma.big[0] == '+' ? '+' : '-';
  163. for (int i = sum1.big_size-1; i > 0; i--)
  164. sum1.big[i] = suma.big[i+1];
  165. sum1.remove_zeros();
  166. return sum1;
  167. }
  168. suma.remove_zeros();
  169. return suma;
  170. }
  171.  
  172. BigInt& operator-(BigInt B)
  173. {
  174. if((big[0]=='+' && B.big[0]=='-') && *this==-B)
  175. {
  176. B=-B;
  177. BigInt zeros("+0");
  178. return zeros;
  179. }
  180. if((big[0]=='-' && B.big[0]=='-') && *this==B)
  181. {
  182. BigInt zeros("+0");
  183. return zeros;
  184. }
  185. int temp_size, small_size;
  186. if(big_size>B.big_size){
  187. temp_size = big_size;
  188. small_size = B.big_size;
  189. }
  190. else{
  191. temp_size = B.big_size;
  192. small_size = big_size;
  193. }
  194. BigInt roznica(temp_size, 1);
  195. roznica.big[0]='+';
  196. int flag=0;
  197. int i=0;
  198. if(big[0]=='+' && B.big[0]=='+'){
  199. if(big_size>B.big_size){
  200. for(i=temp_size-1; i>0; i--)
  201. {
  202. if(flag>0)
  203. if(roznica[i]<49)
  204. flag++;
  205. else
  206. roznica[i]=roznica[i]-flag;
  207.  
  208. roznica.big[i]=big[i]-(B.big[small_size-1]-48);
  209. if(roznica[i]<48){
  210. roznica[i]+=10;
  211. flag = 1;
  212. }
  213.  
  214. if(small_size==1)
  215. break;
  216. small_size--;
  217. }
  218. for(i; i>0; i--)
  219. roznica.big[i]=big[i];
  220.  
  221. roznica.big[0]='+';
  222. }
  223. else if(big_size<B.big_size){
  224. roznica=B-*this;
  225. roznica.big[0]='-';
  226. }else {
  227. if(*this>B)
  228. {
  229.  
  230. for(i=temp_size-1; i>0; i--)
  231. {
  232. if(flag>0)
  233. if(roznica[i]<49)
  234. flag++;
  235. else
  236. roznica[i]=roznica[i]-flag;
  237.  
  238. roznica.big[i]=big[i]-(B.big[small_size-1]-48);
  239. if(roznica[i]<48){
  240. roznica[i]+=10;
  241. flag = 1;
  242. }
  243.  
  244. if(small_size==1)
  245. break;
  246. small_size--;
  247. }
  248. for(i; i>0; i--)
  249. roznica.big[i]=big[i];
  250.  
  251. roznica.big[0]='+';
  252. }
  253. else if(B>*this)
  254. {
  255.  
  256. roznica=B-*this;
  257.  
  258. roznica.big[0]='-';
  259. }
  260. else
  261. {
  262. BigInt zero('0');
  263. return zero;
  264. }
  265.  
  266. }
  267. }
  268. if(big[0]=='+' && B.big[0]=='-'){
  269. B=-B;
  270. roznica=*this+B;
  271. B=-B;
  272. }
  273. if(big[0]=='-' && B.big[0]=='+'){
  274. *this=-*this;
  275. roznica=*this+B;
  276. *this=-*this;
  277. }
  278. roznica.remove_zeros();
  279. return roznica;
  280. }
  281.  
  282. const BigInt operator*(const BigInt& B) const
  283. {
  284. if(big[1]=='0' || B.big[1]=='0')
  285. {
  286. BigInt zeros("+0");
  287. return zeros;
  288. }
  289. BigInt iloczyn(big_size+B.big_size-1, 1); //-1 bo jeden z dwĂłch znakĂłw jest niepotrzebny
  290. //std::cout << iloczyn.big_size;
  291.  
  292. if(big[0]=='-' && B.big[0]=='-')
  293. iloczyn[0] = '+';
  294. else if(big[0]=='-' || B.big[0]=='-')
  295. iloczyn[0] = '-';
  296. else
  297. iloczyn[0] = '+';
  298.  
  299.  
  300. int bigger, smaller;
  301. if(big_size>B.big_size){bigger = big_size; smaller = B.big_size;}
  302. else {bigger = B.big_size; smaller = big_size;}
  303.  
  304. BigInt rows[smaller-1]; //-1 bo znak
  305.  
  306. for (int i = 0; i < smaller-1; i++) //-1
  307. {
  308. rows[i].big_size = bigger + i+1; //+1 bo liczba może byc o 1 większa, +i na zera
  309. //std::cout << "b" <<rows[i].big_size;
  310. rows[i].big = new char[rows[i].big_size];
  311. for (int j = 0; j < rows[i].big_size; j++)
  312. rows[i].big[j] = '0';
  313. rows[i].big[0] = '+';
  314.  
  315. }
  316.  
  317. //for (int i = 0; i < smaller-1; i++){
  318. // std::cout << rows[i].big;
  319. // std::cout << std::endl;}
  320.  
  321. int rest;
  322. int s;
  323. int ct=0;
  324.  
  325. for (int i = smaller; i > 1; i--,ct++) //wymnażamy rzędy do dodania
  326. {
  327. rest = 0;
  328. s = rows[smaller-i].big_size-1;
  329. for (int j = bigger; j > 1; j--,s--)
  330. {
  331. //std::cout << "ct: " << ct << std::endl;
  332. //std::cout << i <<" " << big[i-1] << " " << B.big[j-1] << std::endl;
  333. if (big_size < B.big_size)
  334. {
  335. rows[smaller-i].big[s-ct] = (((big[i-1]-48) * (B.big[j-1]-48) + rest) % 10)+48; //char
  336. rest = (((big[i-1]-48) * (B.big[j-1]-48) + rest) / 10); //int
  337. //std::cout << "rest = " <<rest <<std::endl;
  338. if (j == 2)
  339. rows[smaller-i].big[s-ct-1] = rest + 48;
  340. //std::cout << "\trows[smaller-i].big[s]" << smaller-i << " "<< s << " "<< rows[smaller-i].big[s-ct] <<std::endl;
  341. }
  342. else
  343. {
  344. rows[smaller-i].big[s-ct] = (((B.big[i-1]-48) * (big[j-1]-48) + rest) % 10)+48; //char
  345. rest = (((B.big[i-1]-48) * (big[j-1]-48) + rest) / 10); //int
  346. //std::cout << "rest = " <<rest <<std::endl;
  347. if (j == 2)
  348. rows[smaller-i].big[s-ct-1] = rest + 48;
  349. //std::cout << "\trows[smaller-i].big[s]" << smaller-i << " "<< s-ct << " " << s-ct-1 << " " << rows[smaller-i].big[s-ct] <<std::endl;
  350. }
  351. }
  352.  
  353. }
  354.  
  355. /*for (int i = 0; i < smaller-1; i++){
  356. std::cout << rows[i].big;
  357. std::cout << std::endl;}*/
  358.  
  359.  
  360.  
  361. for (int i = 1; i < iloczyn.big_size; i++) //zerujemy zeby mozna było dodawać
  362. iloczyn.big[i] = '0';
  363. //std::cout << iloczyn.big <<std::endl;
  364.  
  365. for (int i = 0; i < smaller-1; i++)
  366. {
  367. //std::cout << "A"<< rows[i].big;
  368. iloczyn = iloczyn + rows[i];
  369. //std::cout << "p " << iloczyn.big <<std::endl;
  370. }
  371.  
  372.  
  373. for (int i = 0; i <= bigger; i++) //żeby zera sie zgadzały
  374. {
  375. if(iloczyn.big[1] == '0' && iloczyn.big_size != 2)
  376. {
  377. BigInt iloczyn1(iloczyn.big_size - 1,1);
  378. //iloczyn1.big_size = iloczyn.big_size - 1; //zmiejszamy rozmiar o jedno 0
  379. //iloczyn1.big = new char[iloczyn1.big_size];
  380. iloczyn1.big[0] = iloczyn.big[0];
  381. for (int i = iloczyn1.big_size-1; i > 0; i--)
  382. iloczyn1.big[i] = iloczyn.big[i+1];
  383. iloczyn = iloczyn1; //trzeba przepisac bo wynik1 to obiekt tymczasowy
  384. }
  385. }
  386.  
  387. iloczyn.remove_zeros();
  388. if(big[0]=='-' && B.big[0]=='-')
  389. iloczyn[0] = '+';
  390. else if(big[0]=='-' || B.big[0]=='-')
  391. iloczyn[0] = '-';
  392. else
  393. iloczyn[0] = '+';
  394. return iloczyn;
  395. }
  396.  
  397. void remove_zeros(){
  398.  
  399. if(big[1] == '0' && big_size > 2) {
  400.  
  401. int i=1;
  402. while(big[i] == '0')
  403. {
  404. i++;
  405. }
  406. int temp_size = big_size-i+1;
  407.  
  408. BigInt tmp(temp_size, 1);
  409. tmp.big[0] = big[0];
  410. int k=1;
  411. for(int j=i; j<big_size; j++)
  412. {
  413. tmp.big[k] = big[j];
  414. k++;
  415. }
  416. delete[] big;
  417. big = new char[temp_size];
  418.  
  419. for(int z=0; z<temp_size; z++)
  420. big[z]=tmp.big[z];
  421. big_size=temp_size;
  422. }
  423. }
  424. BigInt operator+(long c)
  425. {
  426. BigInt B(c);
  427. return *this+B;
  428. }
  429.  
  430. bool operator== (BigInt B)
  431. {
  432. if(big_size!=B.big_size)
  433. return 0;
  434. else
  435. {
  436. int temp_size;
  437. char *tmp_ar;
  438. char *tmp_ar2;
  439. if(big_size>B.big_size)
  440. {
  441. temp_size = big_size+1;
  442. int war = B.big_size;
  443. int roz = big_size - B.big_size;
  444. tmp_ar= new char[big_size];
  445.  
  446.  
  447. for(int i=1; i<B.big_size; i++)
  448. {
  449. tmp_ar[i+roz] = B.big[i];
  450. war = war-1;
  451. if(war==1)
  452. break;
  453. }
  454. for(int i=0; i<=roz; i++)
  455. tmp_ar[i]='0';
  456.  
  457. tmp_ar[0]='+';
  458.  
  459.  
  460. tmp_ar2=new char[big_size];
  461. for(int i=0; i<big_size; i++)
  462. tmp_ar2[i] = big[i];
  463. }
  464. else
  465. {
  466. temp_size = B.big_size+1;
  467. int war = big_size;
  468. int roz = B.big_size - big_size;
  469. tmp_ar= new char[B.big_size];
  470. for(int i=1; i<big_size; i++)
  471. {
  472. tmp_ar[i+roz] = big[i];
  473. war = war-1;
  474. if(war==1)
  475. break;
  476. }
  477. for(int i=0; i<=roz; i++)
  478. tmp_ar[i]='0';
  479.  
  480. tmp_ar[0]='+';
  481. tmp_ar2=new char[B.big_size];
  482. for(int i=0; i<B.big_size; i++)
  483. tmp_ar2[i] = B.big[i];
  484. }
  485. for(int i=0; i<temp_size-2; i++)
  486. {
  487. if(tmp_ar[i]!=tmp_ar2[i])
  488. return 0;
  489. }
  490. }
  491. return 1;
  492. }
  493.  
  494. bool operator!= (BigInt B)
  495. {
  496. if(big_size!=B.big_size)
  497. return 1;
  498. else
  499. {
  500. int temp_size;
  501. char *tmp_ar;
  502. char *tmp_ar2;
  503. if(big_size>B.big_size)
  504. {
  505. temp_size = big_size+1;
  506. int war = B.big_size;
  507. int roz = big_size - B.big_size;
  508. tmp_ar= new char[big_size];
  509.  
  510.  
  511. for(int i=1; i<B.big_size; i++)
  512. {
  513. tmp_ar[i+roz] = B.big[i];
  514. war = war-1;
  515. if(war==1)
  516. break;
  517. }
  518. for(int i=0; i<=roz; i++)
  519. tmp_ar[i]='0';
  520.  
  521. tmp_ar[0]='+';
  522.  
  523.  
  524. tmp_ar2=new char[big_size];
  525. for(int i=0; i<big_size; i++)
  526. tmp_ar2[i] = big[i];
  527. }
  528. else
  529. {
  530. temp_size = B.big_size+1;
  531. int war = big_size;
  532. int roz = B.big_size - big_size;
  533. tmp_ar= new char[B.big_size];
  534. for(int i=1; i<big_size; i++)
  535. {
  536. tmp_ar[i+roz] = big[i];
  537. war = war-1;
  538. if(war==1)
  539. break;
  540. }
  541. for(int i=0; i<=roz; i++)
  542. tmp_ar[i]='0';
  543.  
  544. tmp_ar[0]='+';
  545. tmp_ar2=new char[B.big_size];
  546. for(int i=0; i<B.big_size; i++)
  547. tmp_ar2[i] = B.big[i];
  548. }
  549. for(int i=0; i<temp_size-2; i++)
  550. {
  551. if(tmp_ar[i]!=tmp_ar2[i])
  552. return 1;
  553. }
  554. }
  555. return 0;
  556. }
  557.  
  558. bool operator< (BigInt B)
  559. {
  560. if(big[0]=='-' && B.big[0]=='+')
  561. return 1;
  562. else if(big[0]=='+' && B.big[0]=='-')
  563. return 0;
  564. else if(big[0]=='+' && B.big[0]=='+')
  565. {
  566. if(big_size<B.big_size)
  567. return 1;
  568. else if(big_size>B.big_size)
  569. return 0;
  570. else
  571. {
  572. for(int i=1; i<big_size; i++)
  573. if(big[i]>B.big[i])
  574. return 0;
  575. }
  576. }
  577. else
  578. {
  579. if(big_size<B.big_size)
  580. return 0;
  581. else if(big_size>B.big_size)
  582. return 1;
  583. else
  584. {
  585. for(int i=1; i<big_size; i++)
  586. if(big[i]<B.big[i])
  587. return 0;
  588. }
  589. }
  590. return 1;
  591.  
  592.  
  593. }
  594.  
  595. bool operator> (BigInt B)
  596. {
  597. if(big[0]=='-' && B.big[0]=='+')
  598. return 0;
  599. else if(big[0]=='+' && B.big[0]=='-')
  600. return 1;
  601. else if(big[0]=='+' && B.big[0]=='+')
  602. {
  603. if(big_size<B.big_size)
  604. return 0;
  605. else if(big_size>B.big_size)
  606. return 1;
  607. else
  608. {
  609. for(int i=0; i<big_size; i++)
  610. if(big[i]<B.big[i])
  611. return 0;
  612. }
  613. }
  614. else
  615. {
  616. if(big_size<B.big_size)
  617. return 1;
  618. else if(big_size>B.big_size)
  619. return 0;
  620. else
  621. {
  622. for(int i=1; i<big_size; i++)
  623. if(big[i]<B.big[i])
  624. return 0;
  625. }
  626. }
  627. return 1;
  628.  
  629.  
  630. }
  631.  
  632. bool operator>= (BigInt B)
  633. {
  634. int flag;
  635. if(big_size == B.big_size)
  636. for(int i=0; i<big_size; i++)
  637. {
  638. if(big[i]==B.big[i])
  639. flag=1;
  640. else
  641. flag=0;
  642. }
  643. if(flag==1)
  644. return 1;
  645.  
  646. if(big[0]=='-' && B.big[0]=='+')
  647. return 0;
  648. else if(big[0]=='+' && B.big[0]=='-')
  649. return 1;
  650. else if(big[0]=='+' && B.big[0]=='+')
  651. {
  652. if(big_size<B.big_size)
  653. return 0;
  654. else if(big_size>B.big_size)
  655. return 1;
  656. else
  657. {
  658. for(int i=0; i<big_size; i++)
  659. if(big[i]<B.big[i])
  660. return 0;
  661. }
  662. }
  663. else
  664. {
  665. if(big_size<B.big_size)
  666. return 1;
  667. else if(big_size>B.big_size)
  668. return 0;
  669. else
  670. {
  671. for(int i=1; i<big_size; i++)
  672. if(big[i]<B.big[i])
  673. return 0;
  674. }
  675. }
  676. return 1;
  677. }
  678.  
  679. bool operator<= (BigInt B)
  680. {
  681. int flag;
  682. if(big_size == B.big_size)
  683. for(int i=0; i<big_size; i++)
  684. {
  685. if(big[i]==B.big[i])
  686. flag=1;
  687. else
  688. flag=0;
  689. }
  690. if(flag==1)
  691. return 1;
  692. if(big[0]=='-' && B.big[0]=='+')
  693. return 1;
  694. else if(big[0]=='+' && B.big[0]=='-')
  695. return 0;
  696. else if(big[0]=='+' && B.big[0]=='+')
  697. {
  698. if(big_size<B.big_size)
  699. return 1;
  700. else if(big_size>B.big_size)
  701. return 0;
  702. else
  703. {
  704. for(int i=1; i<big_size; i++)
  705. if(big[i]>B.big[i])
  706. return 0;
  707. }
  708. }
  709. else
  710. {
  711. if(big_size<B.big_size)
  712. return 0;
  713. else if(big_size>B.big_size)
  714. return 1;
  715. else
  716. {
  717. for(int i=1; i<big_size; i++)
  718. if(big[i]<B.big[i])
  719. return 0;
  720. }
  721. }
  722. return 1;
  723. }
  724.  
  725. const BigInt operator++(int x)
  726. {
  727. BigInt tmp("+1");
  728. return *this+tmp;
  729. }
  730. BigInt operator++()
  731. {
  732. BigInt tmp("+1");
  733. BigInt retval = *this;
  734. *this = *this+tmp;
  735. return retval;
  736. }
  737.  
  738. BigInt operator--(int x)
  739. {
  740. BigInt tmp("+1");
  741. return *this-tmp;
  742. }
  743. BigInt operator--()
  744. {
  745. BigInt tmp("+1");
  746. BigInt retval = *this;
  747. *this = *this-tmp;
  748. return retval;
  749. }
  750.  
  751. BigInt& operator-()
  752. {
  753. if(big[0]=='+')
  754. big[0]='-';
  755. else
  756. big[0]='+';
  757. return *this;
  758. }
  759.  
  760. BigInt& operator=(const BigInt B)
  761. {
  762. if (this == &B)
  763. return *this;
  764. delete [] big;
  765.  
  766. big_size = B.big_size;
  767. big = new char[big_size];
  768.  
  769. for(int i=0; i<B.big_size; i++)
  770. {
  771. big[i]=B.big[i];
  772. }
  773. return *this;
  774. }
  775.  
  776. char& operator[](int i)
  777. {
  778. int j=1;
  779. int flag=0;
  780. for(j=1; j<big_size; j++)
  781. if(big[i]>48 && big[j]<57)
  782. flag=i;
  783. else
  784. break;
  785.  
  786. return big[flag];
  787. }
  788.  
  789. BigInt operator/(BigInt B)
  790. {
  791.  
  792. if(big_size<B.big_size || *this<B){
  793. BigInt zeros("+0");
  794. return zeros;
  795. }
  796.  
  797. char counter = 0;
  798. int full_counter = B.big_size;
  799.  
  800. BigInt iloraz(big_size, 1);
  801. BigInt tmp(B.big_size+1, 1);
  802.  
  803. for(int i=0; i<big_size; i++)
  804. tmp[i] = big[i];
  805. if(tmp<B) {
  806. tmp[big_size+1]=big[big_size+1];
  807. full_counter++;
  808. }
  809.  
  810. while(tmp>B)
  811. {
  812. counter++;
  813. tmp=tmp-B;
  814. }
  815. iloraz[1] = counter+48;
  816. full_counter++;
  817. if(full_counter==big_size)
  818. return iloraz;
  819.  
  820. }
  821.  
  822. BigInt(int, int);
  823.  
  824. };
  825.  
  826. std::ostream& operator<< (std::ostream& os, const BigInt& B)
  827. {
  828. for(int i=0; i<B.big_size; i++)
  829. os<< B.big[i];
  830. return os;
  831. }
  832.  
  833.  
  834.  
  835. std::istream& operator>> (std::istream& is, BigInt& B)
  836. {
  837. for(int i=1; i<B.big_size; i++)
  838. is>>B.big[i];
  839. return is;
  840. }
  841.  
  842. int NumDigits(int x)
  843. {
  844. x = abs(x);
  845. return (x < 10 ? 1 :
  846. (x < 100 ? 2 :
  847. (x < 1000 ? 3 :
  848. (x < 10000 ? 4 :
  849. (x < 100000 ? 5 :
  850. (x < 1000000 ? 6 :
  851. (x < 10000000 ? 7 :
  852. (x < 100000000 ? 8 :
  853. (x < 1000000000 ? 9 :
  854. 10)))))))));
  855. }
  856.  
  857. BigInt::BigInt(int size, int flag)
  858. {
  859. big = new char[size];
  860. big_size = size;
  861. }
  862.  
  863. BigInt::BigInt(long a)
  864. {
  865. int numberOfDigits = NumDigits(a)+1;
  866. big = new char[numberOfDigits];
  867. big_size = numberOfDigits;
  868.  
  869. if(a<0)
  870. big[0] = '-';
  871. else
  872. big[0] = '+';
  873.  
  874. int temp = a;
  875. int rest;
  876. a=abs(a);
  877. for(int i=numberOfDigits-1; i>0; i--)
  878. {
  879. rest = temp%10;
  880. temp = (temp-rest)/10;
  881. rest = rest + 48;
  882. big[i] = rest;
  883. }
  884. }
  885.  
  886. BigInt::BigInt(std::string str)
  887. {
  888. int size;
  889. if(str.at(0) == '+' || str.at(0) == '-')
  890. size = str.length();
  891. else
  892. size = str.length()+1;
  893. big = new char[size];
  894.  
  895. if(str.at(0) == '+' || str.at(0) == '-')
  896. {
  897. big[0] = str.at(0);
  898. for(int i=size-1; i>0; i--)
  899. {
  900. big[i] = str.at(i);
  901. }
  902. }
  903.  
  904. else
  905. {
  906. big[0] = '+';
  907. for(int i=size-1; i>0; i--)
  908. {
  909. big[i] = str.at(i-1);
  910. }
  911. }
  912. big_size = size;
  913. }
  914.  
  915. BigInt::BigInt()
  916. {
  917. // big_size = 0;
  918. // big = new char[2];
  919. // big[0] = '+';
  920. // big[1] = '0';
  921. }
  922.  
  923.  
  924.  
  925. int main(void)
  926. {
  927. BigInt bi("+1");
  928. BigInt bi2("-112");
  929.  
  930. BigInt s1("22");
  931. BigInt s2("32");
  932. BigInt s3("42");
  933. BigInt s4("52");
  934. BigInt s5("62");
  935.  
  936. BigInt b_silnia = s1*s2*s3*s4*s5;
  937. std::cout<<"Mnozenie: " << b_silnia << std::endl;
  938.  
  939. std::cout<<"test: "<<bi<<std::endl;
  940. std::cout<<"test: "<<bi2<<std::endl;
  941. std::cout<<"Dodawanie longa: " <<bi+999999998<<std::endl;
  942.  
  943. BigInt b8 = bi-bi2;
  944.  
  945. std::cout<<"Odejmowanie: "<<b8<<std::endl;
  946. -b8;
  947. std::cout<<"Odejmowanie: "<<b8<<std::endl;
  948.  
  949. BigInt bi3("+122");
  950. BigInt bi4("+144");
  951. BigInt bi5 = bi3/bi4;
  952. std::cout<<"dzielenie: "<<bi5<<std::endl;
  953. std::cout<<"dzielenie: "<<bi5<<std::endl;
  954.  
  955.  
  956. BigInt size_test1("+125");
  957. BigInt size_test2("-15");
  958.  
  959. size_test1=size_test1+size_test2;
  960. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  961. size_test1=size_test1+size_test2;
  962. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  963. size_test1=size_test1+size_test2;
  964. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  965. size_test1=size_test1-size_test2;
  966. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  967. size_test1=size_test1-size_test2;
  968. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  969. size_test1=size_test1-size_test2;
  970. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  971. size_test1=size_test1-size_test2;
  972. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  973. size_test1=size_test1-size_test2;
  974. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  975. size_test1=size_test1-size_test2;
  976. std::cout<<size_test1<<", size: "<<size_test1.big_size<<std::endl;
  977.  
  978. return 0;
  979. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement