Advertisement
Glenpl

Bignum

Mar 10th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. typedef unsigned int UI;
  6.  
  7. // Bignum lib
  8. class Bignum;
  9. string adding(string, string);
  10. string add(string, string);
  11. string multiplique(string, string);
  12. string multi(string, string);
  13. string multiplique_small(string, string);
  14. string multi_small(string, string);
  15. string subtracting(string, string);
  16. string subtr(string, string);
  17. string remove_zeroes(string);
  18. string str_reverse(string);
  19. string divide(string, string);
  20. string div(string, string);
  21. string modulo(string, string);
  22. string mod(string, string);
  23. string power(string, string);
  24. string powe(string, string);
  25. string bigger(string, string);
  26. bool compare(string, string, string);
  27. string intToStr(int);
  28. int strToInt(string);
  29. string remove1stchar(string);
  30. string dodajzera(string, string);
  31.  
  32. class Bignum
  33. {
  34. // values
  35. public:
  36.     string value;
  37.     string value_abs;
  38. private:
  39.     string value_bin;
  40.     string value_oct;
  41.     string value_hex;
  42. // flags
  43. public:
  44.     bool zero; // value = 0
  45.     bool positive; // value > 0
  46.     bool negative; // value < 0
  47. //constructor
  48.     Bignum();
  49.     Bignum(const Bignum&);
  50.     Bignum(const char[]);
  51.     Bignum(string);
  52.     Bignum(int);
  53. // methodes
  54. private:
  55.     void clear(); // clear all values and flags
  56. public:
  57.     void set_new_value(string);
  58.     void update_flags();
  59.  
  60.     // overloaded math operators
  61.     //bignum & bignum
  62.     //1 char
  63.     string operator+(const Bignum& number) const
  64.     {
  65.         return adding(this->value, number.value);
  66.     }
  67.     string operator-(const Bignum& number) const
  68.     {
  69.         return subtracting(this->value, number.value);
  70.     }
  71.     string operator*(const Bignum& number) const
  72.     {
  73.         return multiplique(this->value, number.value);
  74.     }
  75.     string operator/(const Bignum& number) const
  76.     {
  77.         return divide(this->value, number.value);
  78.     }
  79.     string operator%(const Bignum& number) const
  80.     {
  81.         return modulo(this->value, number.value);
  82.     }
  83.     //2 chars
  84.     string operator+=(const Bignum& number)
  85.     {
  86.         this->set_new_value(adding(this->value, number.value));
  87.         return this->value;
  88.     }
  89.     string operator-=(const Bignum& number)
  90.     {
  91.         this->set_new_value(subtracting(this->value, number.value));
  92.         return this->value;
  93.     }
  94.     string operator*=(const Bignum& number)
  95.     {
  96.         this->set_new_value(multiplique(this->value, number.value));
  97.         return this->value;
  98.     }
  99.     string operator/=(const Bignum& number)
  100.     {
  101.         this->set_new_value(divide(this->value, number.value));
  102.         return this->value;
  103.     }
  104.     string operator%=(const Bignum& number)
  105.     {
  106.         this->set_new_value(modulo(this->value, number.value));
  107.         return this->value;
  108.     }
  109.     // bignum & int
  110.     string operator+(const int& number) const
  111.     {
  112.         return adding(this->value, intToStr(number));
  113.     }
  114.     string operator-(const int& number) const
  115.     {
  116.         return subtracting(this->value, intToStr(number));
  117.     }
  118.     string operator*(const int& number) const
  119.     {
  120.         return multiplique(this->value, intToStr(number));
  121.     }
  122.     string operator/(const int& number) const
  123.     {
  124.         return divide(this->value, intToStr(number));
  125.     }
  126.     string operator%(const int& number) const
  127.     {
  128.         return modulo(this->value, intToStr(number));
  129.     }
  130.     //2 chars
  131.     string operator+=(const int& number)
  132.     {
  133.         this->set_new_value(adding(this->value, intToStr(number)));
  134.         return this->value;
  135.     }
  136.     string operator-=(const int& number)
  137.     {
  138.         this->set_new_value(subtracting(this->value, intToStr(number)));
  139.         return this->value;
  140.     }
  141.     string operator*=(const int& number)
  142.     {
  143.         this->set_new_value(multiplique(this->value, intToStr(number)));
  144.         return this->value;
  145.     }
  146.     string operator/=(const int& number)
  147.     {
  148.         this->set_new_value(divide(this->value, intToStr(number)));
  149.         return this->value;
  150.     }
  151.     string operator%=(const int& number)
  152.     {
  153.         this->set_new_value(modulo(this->value, intToStr(number)));
  154.         return this->value;
  155.     }
  156.     // bignum & string
  157.     string operator+(const string& number) const
  158.     {
  159.         return adding(this->value, number);
  160.     }
  161.     string operator-(const string& number) const
  162.     {
  163.         return subtracting(this->value, number);
  164.     }
  165.     string operator*(const string& number) const
  166.     {
  167.         return multiplique(this->value, number);
  168.     }
  169.     string operator/(const string& number) const
  170.     {
  171.         return divide(this->value, number);
  172.     }
  173.     string operator%(const string& number) const
  174.     {
  175.         return modulo(this->value, number);
  176.     }
  177.     //2 chars
  178.     string operator+=(const string& number)
  179.     {
  180.         this->set_new_value(adding(this->value, number));
  181.         return this->value;
  182.     }
  183.     string operator-=(const string& number)
  184.     {
  185.         this->set_new_value(subtracting(this->value, number));
  186.         return this->value;
  187.     }
  188.     string operator*=(const string& number)
  189.     {
  190.         this->set_new_value(multiplique(this->value, number));
  191.         return this->value;
  192.     }
  193.     string operator/=(const string& number)
  194.     {
  195.         this->set_new_value(divide(this->value, number));
  196.         return this->value;
  197.     }
  198.     string operator%=(const string& number)
  199.     {
  200.         this->set_new_value(modulo(this->value, number));
  201.         return this->value;
  202.     }
  203.  
  204.     //other operators
  205.     string operator=(const string& number)
  206.     {
  207.         this->set_new_value(number);
  208.         return this->value;
  209.     }
  210.     string operator=(const int& number)
  211.     {
  212.         this->set_new_value(intToStr(number));
  213.         return this->value;
  214.     }
  215.     string &operator++()
  216.     {
  217.         this->set_new_value(adding(this->value, "1"));
  218.         return this->value;
  219.     }
  220.     string operator++(int)
  221.     {
  222.         Bignum copy1 = (*this);
  223.         this->set_new_value(adding(this->value, "1"));
  224.         return copy1.value;
  225.     }
  226.     string &operator--()
  227.     {
  228.         this->set_new_value(subtracting(this->value, "1"));
  229.         return this->value;
  230.     }
  231.     string operator--(int)
  232.     {
  233.         Bignum copy1 = (*this);
  234.         this->set_new_value(subtracting(this->value, "1"));
  235.         return copy1.value;
  236.     }
  237.     string &operator-()
  238.     {
  239.         this->set_new_value(((this->value[0] == '-') ? (remove1stchar(this->value)) : ('-' + this->value)));
  240.         return this->value;
  241.     }
  242.     //bignum
  243.     bool operator==(const Bignum& number)
  244.     {
  245.         return compare(this->value, "==", number.value);
  246.     }
  247.     bool operator!=(const Bignum& number)
  248.     {
  249.         return compare(this->value, "!=", number.value);
  250.     }
  251.     bool operator>(const Bignum& number)
  252.     {
  253.         return compare(this->value, ">", number.value);
  254.     }
  255.     bool operator<(const Bignum& number)
  256.     {
  257.         return compare(this->value, "<", number.value);
  258.     }
  259.     bool operator>=(const Bignum& number)
  260.     {
  261.         return compare(this->value, "=>", number.value);
  262.     }
  263.     bool operator<=(const Bignum& number)
  264.     {
  265.         return compare(this->value, "<=", number.value);
  266.     }
  267.     //string
  268.     bool operator==(const string& number)
  269.     {
  270.         return compare(this->value, "==", number);
  271.     }
  272.     bool operator!=(const string& number)
  273.     {
  274.         return compare(this->value, "!=", number);
  275.     }
  276.     bool operator>(const string& number)
  277.     {
  278.         return compare(this->value, ">", number);
  279.     }
  280.     bool operator<(const string& number)
  281.     {
  282.         return compare(this->value, "<", number);
  283.     }
  284.     bool operator>=(const string& number)
  285.     {
  286.         return compare(this->value, "=>", number);
  287.     }
  288.     bool operator<=(const string& number)
  289.     {
  290.         return compare(this->value, "<=", number);
  291.     }
  292.     //int
  293.     bool operator==(const int& number)
  294.     {
  295.         return compare(this->value, "==", intToStr(number));
  296.     }
  297.     bool operator!=(const int& number)
  298.     {
  299.         return compare(this->value, "!=", intToStr(number));
  300.     }
  301.     bool operator>(const int& number)
  302.     {
  303.         return compare(this->value, ">", intToStr(number));
  304.     }
  305.     bool operator<(const int& number)
  306.     {
  307.         return compare(this->value, "<", intToStr(number));
  308.     }
  309.     bool operator>=(const int& number)
  310.     {
  311.         return compare(this->value, "=>", intToStr(number));
  312.     }
  313.     bool operator<=(const int& number)
  314.     {
  315.         return compare(this->value, "<=", intToStr(number));
  316.     }
  317.     //unsigned int
  318.     bool operator==(const UI& number)
  319.     {
  320.         return compare(this->value, "==", intToStr(number));
  321.     }
  322.     bool operator!=(const UI& number)
  323.     {
  324.         return compare(this->value, "!=", intToStr(number));
  325.     }
  326.     bool operator>(const UI& number)
  327.     {
  328.         return compare(this->value, ">", intToStr(number));
  329.     }
  330.     bool operator<(const UI& number)
  331.     {
  332.         return compare(this->value, "<", intToStr(number));
  333.     }
  334.     bool operator>=(const UI& number)
  335.     {
  336.         return compare(this->value, "=>", intToStr(number));
  337.     }
  338.     bool operator<=(const UI& number)
  339.     {
  340.         return compare(this->value, "<=", intToStr(number));
  341.     }
  342.     operator string()
  343.     {
  344.         return this->value;
  345.     }
  346.     operator int()
  347.     {
  348.         return strToInt(this->value);
  349.     }
  350.     operator UI()
  351.     {
  352.         return (UI)strToInt(this->value);
  353.     }
  354. };
  355.  
  356. ostream& operator<<(ostream &out, const Bignum &num)
  357. {
  358.     return out <<num.value;
  359. }
  360. istream& operator>>(istream &in, Bignum &num)
  361. {
  362.     string a;
  363.     in >> a;
  364.     num.set_new_value(a);
  365.     return in;
  366. }
  367.  
  368. //int and string & Bignum global overloads
  369. // int & bignum
  370. string operator+(const int& number, const Bignum& number2)
  371. {
  372.     return adding(intToStr(number), number2.value);
  373. }
  374. string operator-(const int& number, const Bignum& number2)
  375. {
  376.     return subtracting(intToStr(number), number2.value);
  377. }
  378. string operator*(const int& number, const Bignum& number2)
  379. {
  380.     return multiplique(intToStr(number), number2.value);
  381. }
  382. string operator/(const int& number, const Bignum& number2)
  383. {
  384.     return divide(intToStr(number), number2.value);
  385. }
  386. string operator%(const int& number, const Bignum& number2)
  387. {
  388.     return modulo(intToStr(number), number2.value);
  389. }
  390. // bignum & string
  391. string operator+(const string& number, const Bignum& number2)
  392. {
  393.     return adding(number, number2.value);
  394. }
  395. string operator-(const string& number, const Bignum& number2)
  396. {
  397.     return subtracting(number, number2.value);
  398. }
  399. string operator*(const string& number, const Bignum& number2)
  400. {
  401.     return multiplique(number, number2.value);
  402. }
  403. string operator/(const string& number, const Bignum& number2)
  404. {
  405.     return divide(number, number2.value);
  406. }
  407. string operator%(const string& number, const Bignum& number2)
  408. {
  409.     return modulo(number, number2.value);
  410. }
  411. //2 chars
  412. string operator+=(string& number, const Bignum& number2)
  413. {
  414.     number = adding(number, number2.value);
  415.     return number;
  416. }
  417. string operator-=(string& number, const Bignum& number2)
  418. {
  419.     number = subtracting(number, number2.value);
  420.     return number;
  421. }
  422. string operator*=(string& number, const Bignum& number2)
  423. {
  424.     number = multiplique(number, number2.value);
  425.     return number;
  426. }
  427. string operator/=(string& number, const Bignum& number2)
  428. {
  429.     number = divide(number, number2.value);
  430.     return number;
  431. }
  432. string operator%=(string& number, const Bignum& number2)
  433. {
  434.     number = modulo(number, number2.value);
  435.     return number;
  436. }
  437. //int
  438. bool operator==(const int& number, const Bignum& number2)
  439. {
  440.     return compare(intToStr(number), "==", number2.value);
  441. }
  442. bool operator!=(const int& number, const Bignum& number2)
  443. {
  444.     return compare(intToStr(number), "!=", number2.value);
  445. }
  446. bool operator>(const int& number, const Bignum& number2)
  447. {
  448.     return compare(intToStr(number), ">", number2.value);
  449. }
  450. bool operator<(const int& number, const Bignum& number2)
  451. {
  452.     return compare(intToStr(number), "<", number2.value);
  453. }
  454. bool operator>=(const int& number, const Bignum& number2)
  455. {
  456.     return compare(intToStr(number), "=>", number2.value);
  457. }
  458. bool operator<=(const int& number, const Bignum& number2)
  459. {
  460.     return compare(intToStr(number), "<=", number2.value);
  461. }
  462. //string
  463. bool operator==(const string& number, const Bignum& number2)
  464. {
  465.     return compare(number, "==", number2.value);
  466. }
  467. bool operator!=(const string& number, const Bignum& number2)
  468. {
  469.     return compare(number, "!=", number2.value);
  470. }
  471. bool operator>(const string& number, const Bignum& number2)
  472. {
  473.     return compare(number, ">", number2.value);
  474. }
  475. bool operator<(const string& number, const Bignum& number2)
  476. {
  477.     return compare(number, "<", number2.value);
  478. }
  479. bool operator>=(const string& number, const Bignum& number2)
  480. {
  481.     return compare(number, "=>", number2.value);
  482. }
  483. bool operator<=(const string& number, const Bignum& number2)
  484. {
  485.     return compare(number, "<=", number2.value);
  486. }
  487.  
  488. Bignum::Bignum()
  489. {
  490.     this->clear();
  491. }
  492. Bignum::Bignum(const Bignum& a)
  493. {
  494.     this->clear();
  495.     this->set_new_value(a.value);
  496. }
  497. Bignum::Bignum(string a)
  498. {
  499.     this->clear();
  500.     this->set_new_value(a);
  501. }
  502. Bignum::Bignum(const char a[])
  503. {
  504.     string b = a;
  505.     this->clear();
  506.     this->set_new_value(b);
  507. }
  508. Bignum::Bignum(int a)
  509. {
  510.     this->clear();
  511.     this->set_new_value(intToStr(a));
  512. }
  513.  
  514. string powe(string a, string b)
  515. {
  516.     //interface
  517.     return power(a, b);
  518. }
  519. string power(string a, string b)
  520. {
  521.     Bignum c;
  522.     c = a;
  523.     for(int i = 1; i < strToInt(b); i++)
  524.         c *= a;
  525.     return c.value;
  526. }
  527. void Bignum::clear()
  528. {
  529.     this->value = "0";
  530.     this->value_abs = "0";
  531.     this->value_bin = "0";
  532.     this->value_oct = "0";
  533.     this->value_hex = "0";
  534.     this->update_flags();
  535.     return;
  536. }
  537. void Bignum::set_new_value(string a)
  538. {
  539.     this->value = a;
  540.     this->value_abs = remove1stchar(a);
  541.     this->update_flags();
  542. }
  543.  
  544. void Bignum::update_flags()
  545. {
  546.     if(this->value == "0")
  547.     {
  548.         this->zero = true;
  549.         this->positive = false;
  550.         this->negative = false;
  551.     }
  552.     if(this->value[0] == '-')
  553.     {
  554.         this->zero = false;
  555.         this->positive = false;
  556.         this->negative = true;
  557.     }
  558.     if(this->value[0] != '-')
  559.     {
  560.         this->zero = false;
  561.         this->positive = true;
  562.         this->negative = false;
  563.     }
  564. }
  565.  
  566. string sumofdigits(Bignum a)
  567. {
  568.     Bignum b;
  569.     b = 0;
  570.     for(unsigned i = 0; i < a.value.length(); i++)
  571.         b += a.value[i] - '0';
  572.     return b.value;
  573. }
  574.  
  575. string remove1stchar(string a)
  576. {
  577.     if(a[0] == '-')
  578.         a.erase(0, 1);
  579.     return a;
  580. }
  581.  
  582. string str_reverse(string a)
  583. {
  584.     string b;
  585.     for(int i = a.length()-1; i >= 0; i--)
  586.         b += a[i];
  587.     return  b;
  588. }
  589.  
  590. string remove_zeroes(string a)
  591. {
  592.     string b;
  593.     int length = a.length();
  594.     int n_zero_pos = length;
  595.     for(int i = 0; i < length; i++)
  596.         if(a[i] != '0')
  597.         {
  598.             n_zero_pos = i;
  599.             break;
  600.         }
  601.     if(n_zero_pos == length) return "0";
  602.  
  603.     for(int i = n_zero_pos; i < length; i++)
  604.         b += a[i];
  605.     return b;
  606. }
  607.  
  608. string adding(string a, string b)
  609. {
  610.     if(a[0] != '-' && b[0] != '-') return add(a, b);
  611.     if(a[0] == '-' && b[0] == '-') return '-' + add(remove1stchar(a), remove1stchar(b));
  612.     if(a[0] != '-' && b[0] == '-') return subtracting(a, remove1stchar(b));
  613.     if(a[0] == '-' && b[0] != '-') return subtracting(b, remove1stchar(a));
  614.     return "";
  615. }
  616.  
  617. string add(string a, string b)
  618. {
  619.     int length = max(a.length(), b.length());
  620.     a = str_reverse(a);
  621.     b = str_reverse(b);
  622.     if(a.length() != b.length())
  623.         ((a.length() > b.length()) ? (b) : (a)).resize(length, '0');
  624.     string c;
  625.     c.resize(length + 1);
  626.  
  627.     for(int i = 0; i < length; i++)
  628.         c[i] = a[i] - '0' + b[i];
  629.  
  630.     for(int i = 0; i < length; i++)
  631.         if(c[i] > '9')
  632.         {
  633.             c[i] -= 10;
  634.             c[i+1]++;
  635.         }
  636.     c[length] += '0';
  637.     return remove_zeroes(str_reverse(c));
  638. }
  639.  
  640. string subtracting(string a, string b)
  641. {
  642.     if(a[0] != '-' && b[0] != '-')
  643.     {
  644.         if(compare(a, ">=", b)) return subtr(a, b);
  645.         if(compare(a, "<=", b)) return '-' + subtr(b, a);
  646.     }
  647.     if(a[0] == '-' && b[0] == '-') return adding(a, remove1stchar(b));
  648.     if(a[0] != '-' && b[0] == '-') return adding(a, remove1stchar(b));
  649.     if(a[0] == '-' && b[0] != '-') return '-' + adding(remove1stchar(a), b);
  650.     return "";
  651. }
  652.  
  653. string subtr(string a, string b)
  654. {
  655.     int length = max(a.length(), b.length());
  656.     a = str_reverse(a);
  657.     b = str_reverse(b);
  658.     if(a.length() != b.length())
  659.         ((a.length() > b.length()) ? (b) : (a)).resize(length, '0');
  660.     string c;
  661.     c.resize(length);
  662.  
  663.     for(int i = 0; i < length; i++)
  664.         c[i] = a[i] + '0' - b[i];
  665.  
  666.     for(int i = 0; i < length; i++)
  667.         if(c[i] < '0')
  668.         {
  669.             c[i] += 10;
  670.             c[i+1]--;
  671.         }
  672.     return remove_zeroes(str_reverse(c));
  673. }
  674.  
  675. string bigger(string a, string b)
  676. {
  677.     if(a == b) return a;
  678.     if(a.length() != b.length())
  679.         return ((a.length() > b.length()) ? (a) : (b));
  680.     int i = 0;
  681.     while(a[i] == b[i])
  682.         i++;
  683.     return ((a[i] > b[i]) ? (a) : (b));
  684. }
  685.  
  686. string smaller(string a, string b)
  687. {
  688.     return ((a == bigger(a, b)) ? (b) : (a));
  689. }
  690.  
  691. Bignum bigger(Bignum a, Bignum b)
  692. {
  693.     return ((a.value == bigger(a.value, b.value)) ? (a) : (b));
  694. }
  695.  
  696. Bignum smaller(Bignum a, Bignum b)
  697. {
  698.     return ((a.value == bigger(a.value, b.value)) ? (b) : (a));
  699. }
  700.  
  701. string multiplique(string a, string b)
  702. {
  703.     Bignum c = smaller(a, b);
  704.     if(c < 501) return multiplique_small(a, b);
  705.     if(a[0] != '-' && b[0] != '-') return multi(a, b);
  706.     if(a[0] == '-' && b[0] == '-') return multi(remove1stchar(a), remove1stchar(b));
  707.     if(a[0] != '-' && b[0] == '-') return '-' + multi(a, remove1stchar(b));
  708.     if(a[0] == '-' && b[0] != '-') return '-' + multi(remove1stchar(a), b);
  709.     return "";
  710. }
  711.  
  712. int mnozeniecharow(char a, char b)
  713. {
  714.     return (a-'0')*(b-'0');
  715. }
  716.  
  717. string multi(string a, string b)
  718. {
  719.     if(a == "0" || b == "0") return "0";
  720.     string pom = bigger(a, b);
  721.     b = smaller(a, b);
  722.     a = pom;
  723.     Bignum w = 0;
  724.     a = str_reverse(a);
  725.     b = str_reverse(b);
  726.     for(int c = 0; c < a.length(); c++)
  727.         for(int d = 0; d < b.length(); d++)
  728.             w += dodajzera(intToStr(mnozeniecharow(a[c], b[d])), intToStr(c+d));
  729.     return w.value;
  730. }
  731.  
  732. bool compare(string a, string comp, string b)
  733. {
  734.     if(comp == "==" || comp == ">=" || comp == "<=")
  735.         if(a == b) return true;
  736.     if(comp == "==") return false;
  737.     if(comp == "!=") return true;
  738.     string bigger_num = bigger(a, b);
  739.     if((comp == ">" || comp == ">=") && bigger_num == a) return true;
  740.     if((comp == "<" || comp == "<=") && bigger_num == b) return true;
  741.     return false;
  742. }
  743.  
  744. string divide(string a, string b)
  745. {
  746.     if(a[0] != '-' && b[0] != '-') return div(a, b);
  747.     if(a[0] == '-' && b[0] == '-') return div(remove1stchar(a), remove1stchar(b));
  748.     if(a[0] != '-' && b[0] == '-') return '-' + div(a, remove1stchar(b));
  749.     if(a[0] == '-' && b[0] != '-') return '-' + div(remove1stchar(a), b);
  750.     return "";
  751. }
  752.  
  753. string div(string a, string b)
  754. {
  755.     string xtimes = "0";
  756.     while(compare(a, ">=", b))
  757.     {
  758.         a = subtracting(a, b);
  759.         xtimes = adding(xtimes, "1");
  760.     }
  761.     return xtimes;
  762. }
  763.  
  764. string modulo(string a, string b)
  765. {
  766.     if(a[0] != '-' && b[0] != '-') return mod(a, b);
  767.     if(a[0] == '-' && b[0] == '-') return remove1stchar(adding(b, mod(remove1stchar(a), remove1stchar(b))));
  768.     if(a[0] != '-' && b[0] == '-') return mod(a, remove1stchar(b));
  769.     if(a[0] == '-' && b[0] != '-') return subtracting(b, mod(remove1stchar(a), b));
  770.     return "";
  771. }
  772.  
  773. string mod(string a, string b)
  774. {
  775.     while(compare(a, ">=", b))
  776.         a = subtracting(a, b);
  777.     return a;
  778. }
  779.  
  780. string dodajzera(string a, string b)
  781. {
  782.     string c;
  783.     for(int i = strToInt(b); i > 0; i--)
  784.         c+='0';
  785.     c = a + c;
  786.     return c;
  787. }
  788.  
  789. string intToStr(int n)
  790. {
  791.     string tmp;
  792.     if(n < 0)
  793.     {
  794.         tmp = "-";
  795.         n = -n;
  796.     }
  797.     if(n > 9)
  798.         tmp += intToStr(n / 10);
  799.     tmp += n % 10 + 48;
  800.     return tmp;
  801. }
  802.  
  803. int strToInt(string s)
  804. {
  805.     int tmp = 0;
  806.     unsigned i = 0;
  807.     bool m = false;
  808.     if(s[0] == '-')
  809.     {
  810.         m = true;
  811.         i++;
  812.     }
  813.     for(; i < s.size(); i++)
  814.         tmp = 10 * tmp + s[i] - 48;
  815.     return m ? -tmp : tmp;
  816. }
  817.  
  818. Bignum nwd(Bignum a, Bignum b)
  819. {
  820.     if(a > b) a -= b;
  821.     else if (b > a) b -= a;
  822.     return ((a == b) ? (a) : (nwd(a, b)));
  823. }
  824.  
  825. /*
  826. Bignum nwd(Bignum a, Bignum b)
  827. {
  828.     Bignum c;
  829.     while(b != 0)
  830.     {
  831.         c = a % b;
  832.         a = b;
  833.         b = c;
  834.     }
  835.     return a;
  836. }
  837. */
  838.  
  839. Bignum nww(Bignum a, Bignum b)
  840. {
  841.     //return (a/nwd(a, b)*b);
  842.     return a;
  843. }
  844.  
  845. string multiplique_small(string a, string b)
  846. {
  847.     if(a[0] != '-' && b[0] != '-') return multi_small(a, b);
  848.     if(a[0] == '-' && b[0] == '-') return multi_small(remove1stchar(a), remove1stchar(b));
  849.     if(a[0] != '-' && b[0] == '-') return '-' + multi_small(a, remove1stchar(b));
  850.     if(a[0] == '-' && b[0] != '-') return '-' + multi_small(remove1stchar(a), b);
  851. }
  852.  
  853. string multi_small(string a, string b)
  854. {
  855.     string w = a;
  856.     while(subtracting(b, "1") != "0")
  857.     {
  858.         b = subtracting(b, "1");
  859.         w = adding(w, a);
  860.     }
  861.     return w;
  862. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement