Advertisement
Guest User

BigNum.cpp

a guest
May 21st, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <unistd.h>
  5. #include <windows.h>
  6. #include "stdafx.h"
  7. /*
  8. * bigNum v0.2. - operacje na DUZYCH liczbach
  9. *
  10. * author: Jakub Ziółkowski (jakub.ziolkowski@hotmail.com)
  11. */
  12. class bigNum {
  13. public:
  14. std::string z; /* Wynik */
  15. unsigned char r; /* Reszta */
  16. double time; /* Licznik czasu obliczen */
  17.  
  18. void clean(){ this->z = ""; this->r = 0; }; /* Czysci pamiec */
  19.  
  20. /* czas CPU w usec */
  21. double timer(){
  22. timeval t; gettimeofday(&t, NULL);
  23. return (double)t.tv_sec+(t.tv_usec/1000000.0);
  24. }
  25.  
  26. /* dodawanie 2ch liczb calkowitych dodatnich */
  27. std::string plus(const std::string& a, const std::string& b) {
  28. this->clean();
  29. double start = timer();
  30.  
  31. int a_s = a.size(), b_s = b.size(),
  32. limit = a_s > b_s ? a_s : b_s,
  33. mov_a = a_s-1, mov_b = b_s-1;
  34.  
  35. this->z.resize(limit + 1);
  36. for (int i = limit; i >= 0; i--){
  37. if (mov_a >= 0)this->r += a[mov_a--] - '0';
  38. if (mov_b >= 0)this->r += b[mov_b--] - '0';
  39. this->z[i] = this->r % 10 + '0';
  40. this->r /= 10;
  41. }
  42. this->time += this->timer()-start;
  43. return (this->z[0] == '0')?this->z.substr(1):this->z;
  44. }
  45.  
  46. /* mnozenie 2ch liczb calkowitych dodatnich */
  47. std::string mul(const std::string& a, const std::string& b)
  48. {
  49. this->clean();
  50. double start = timer();
  51. int a_s = a.size(), b_s = b.size(),
  52. o_l = a_s + b_s;//,
  53. // mov_a = a_s - 1, mov_b = b_s - 1;
  54.  
  55. this->z.resize(o_l);
  56. for (int i = 0; i < o_l; i++) this->z[i] = '0';
  57. for (int i = a_s - 1; i >=0; i--) {
  58. this->r = 0;
  59. for (int j = b_s - 1; j >= 0; j--){
  60. this->r += this->z[i + j + 1] - '0';
  61. this->r += (a[i] - '0') * (b[j] - '0');
  62. this->z[i + j + 1] = this->r % 10 + '0';
  63. this->r /= 10;
  64. }
  65. this->r += this->z[i] - '0';
  66. this->z[i] = this->r % 10 + '0';
  67. }
  68. this->time += this->timer()-start;
  69. return (this->z[0] == '0')?this->z.substr(1):this->z;
  70. }
  71.  
  72. /* Potega */
  73. std::string pow(const std::string a, long long int x){
  74. std::string out = "1";
  75. while(x--) out = this->mul(out,a);
  76. return out;
  77. }
  78.  
  79. /* Silnia z liczby calkowitej */
  80. std::string silnia(int n){
  81. std::string _r = "1", _m = "2";
  82. for (int i = 2; i <= n; i++) {
  83. _r = this->mul(_r, _m);
  84. _m = this->plus(_m, "1");
  85. if(i%100 == 0) std::cout << "n~=" << i << "\r"; // DEBUG
  86. }
  87. return _r;
  88. }
  89. };
  90.  
  91. int main(){
  92.  
  93. int x;
  94. bigNum* handler = new bigNum();
  95.  
  96. std::string WYNIK;
  97. printf("* Silnia * (N! = x),\nn=");
  98. //std::cout << "n! = x podaj n\r";
  99. std::cin >> x;
  100. WYNIK = handler->silnia(x);
  101. std::cout << WYNIK << std::endl;
  102. handler->clean();
  103. printf("\n %d cyfr\nCzas procesora): %.6lf\n",WYNIK.size(), handler->time);
  104. WYNIK = "";
  105. std::string a,b;
  106. printf("* Iloczyn (AxB = x),\nA=");
  107. std::cin >> a;
  108. printf("B=");
  109. std::cin >> b;
  110. WYNIK = handler->mul(a,b);
  111. std::cout << WYNIK << std::endl;
  112.  
  113. delete handler;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement