Guest User

Untitled

a guest
May 4th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. #include "Factor.h"
  2.  
  3.  
  4. void Send(Factor a, bool b){
  5.     if(a.inf){
  6.         *o << "inf\n";
  7.         cout << "inf\n";
  8.         return;
  9.     }
  10.     *o << a.n << "*10^" << a.pot << "\n";
  11.     cout << a.n << "*10^" << a.pot << "\n";
  12. }
  13.  
  14. void Send(Factor a){
  15.     if(a.inf){
  16.         *o << "inf";
  17.         cout << "inf";
  18.         return;
  19.     }
  20.     *o << a.n << "*10^" << a.pot;
  21.     cout << a.n << "*10^" << a.pot;
  22. }
  23.  
  24. void Send(Factor* a, int n, char c, bool d){
  25.     *o << a[0].n << "*10^" << a[0].pot;
  26.     cout << a[0].n << "*10^" << a[0].pot;
  27.     for(int x = 1; x<n; x++){
  28.         *o << c << a[x].n << "*10^" << a[x].pot;
  29.         cout << c << a[x].n << "*10^" << a[x].pot;
  30.     }
  31.     *o << "\n";
  32.     cout << "\n";
  33. }
  34.  
  35. void Send(Factor* a, int n, char c){
  36.     *o << a[0].n << "*10^" << a[0].pot;
  37.     cout << a[0].n << "*10^" << a[0].pot;
  38.     for(int x = 1; x<n; x++){
  39.         *o << c << a[x].n << "*10^" << a[x].pot;
  40.         cout << c << a[x].n << "*10^" << a[x].pot;
  41.     }
  42. }
  43.  
  44. Factor fixFactor(Factor a){
  45.     if(a.n==INFINITY||a.inf){
  46.         a.pot = 0;
  47.         a.inf = true;
  48.         a.n = INFINITY;
  49.         return a;
  50.     }
  51.     while(a.n*a.n>=100&&a.n!=INFINITY){
  52.         a.pot++;
  53.         a.n/=10;
  54.     }
  55.     while(a.n*a.n<1&&a.n!=0){
  56.         a.pot--;
  57.         a.n*=10;
  58.     }
  59.     if(digits>0){
  60.         for(int x = 0; x<digits; x++){
  61.             a.n*=10;
  62.         }
  63.         a.n = floor(a.n+0.5f);
  64.         for(int x = 0; x<digits; x++){
  65.             a.n/=10;
  66.         }
  67.     }
  68.     return a;
  69. }
  70.  
  71. bool operator==(Factor a, Factor b){
  72.     if(a.inf||b.inf){
  73.         return !(a.inf xor b.inf);
  74.     }
  75.     Factor c, d;
  76.     c = fixFactor(a);
  77.     d = fixFactor(b);
  78.     return c.n==d.n&&c.pot==d.pot;
  79. }
  80.  
  81. bool operator<(Factor a, Factor b){
  82.     if(a.inf&&!b.inf){
  83.         return true;
  84.     }
  85.     if(b.inf){
  86.         return false;
  87.     }
  88.     Factor c, d;
  89.     c = fixFactor(a);
  90.     d = fixFactor(b);
  91.     if(c.pot<d.pot){
  92.         return true;
  93.     }
  94.     if(c.pot==d.pot&&c.n<d.n){
  95.         return true;
  96.     }
  97.     return false;
  98. }
  99.  
  100. bool operator>(Factor a, Factor b){
  101.     return b<a;
  102. }
  103.  
  104. bool operator>=(Factor a, Factor b){
  105.     return b<a||b==a;
  106. }
  107.  
  108. bool operator<=(Factor a, Factor b){
  109.     return a<b||a==b;
  110. }
  111.  
  112. bool operator!=(Factor a, Factor b){
  113.     return !(a==b);
  114. }
  115.  
  116. Factor operator+(Factor a, Factor b){
  117.     if(a.inf||b.inf){
  118.         Factor c;
  119.         c.inf = true;
  120.         c.n = INFINITY;
  121.         c.pot = 0;
  122.         c.name = a.name;
  123.         return c;
  124.     }
  125.     Factor c,d;
  126.     c = fixFactor(a); d = fixFactor(b);
  127.     for(int x = c.pot; x<d.pot; x++){
  128.         c.n /= 10;
  129.         c.pot++;
  130.     }
  131.     for(int x = d.pot; x<c.pot; x++){
  132.         d.n /= 10;
  133.         d.pot++;
  134.     }
  135.     c.n += d.n;
  136.     return fixFactor(c);
  137. }
  138.  
  139. Factor operator-(Factor a, Factor b){
  140.     if(a.inf){
  141.         Factor c;
  142.         c.inf = true;
  143.         c.n = INFINITY;
  144.         c.pot = 0;
  145.         c.name = a.name;
  146.         return c;
  147.     }
  148.     else if(b.inf){
  149.         Factor c;
  150.         c.inf = true;
  151.         c.n = -INFINITY;
  152.         c.pot = 0;
  153.         c.name = a.name;
  154.         return c;
  155.     }
  156.  
  157.     Factor c,d;
  158.     c = fixFactor(a); d = fixFactor(b);
  159.     for(int x = c.pot; x<d.pot; x++){
  160.         c.n /= 10;
  161.         c.pot++;
  162.     }
  163.     for(int x = d.pot; x<c.pot; x++){
  164.         c.n *= 10;
  165.         c.pot--;
  166.     }
  167.     c.n -= d.n;
  168.     return fixFactor(c);
  169. }
  170.  
  171. Factor operator-(Factor a){
  172.     Factor b = fixFactor(a);
  173.     b.n = -b.n;
  174.     return b;
  175. }
  176.  
  177. Factor operator*(Factor a, Factor b){
  178.     if(a.inf||b.inf){
  179.         Factor c;
  180.         c.inf = true;
  181.         c.n = INFINITY;
  182.         c.pot = 0;
  183.         c.name = a.name + "*" + b.name;
  184.         return c;
  185.     }
  186.     Factor c, d;
  187.     c = fixFactor(a);
  188.     d = fixFactor(b);
  189.     c.n = c.n*d.n;
  190.     c.pot += d.pot;
  191.     c.name = c.name + "*" + d.name;
  192.     return fixFactor(c);
  193. }
  194.  
  195. Factor operator/(Factor a, Factor b){
  196.     if(a.inf||b==nullFactor){
  197.         Factor c;
  198.         c.inf = true;
  199.         c.n = INFINITY;
  200.         c.pot = 0;
  201.         c.name = a.name + "/(" + b.name + ")";
  202.         return c;
  203.     }
  204.     else if(b.inf||a==nullFactor){
  205.         Factor c = nullFactor;
  206.         c.name = a.name + "/(" + b.name + ")";
  207.         return c;
  208.     }
  209.     Factor c, d;
  210.     c = fixFactor(a);
  211.     d = fixFactor(b);
  212.     c.n = c.n/d.n;
  213.     c.pot -= d.pot;
  214.     c.name = c.name + "/(" + d.name + ")";
  215.     Factor e = fixFactor(c);
  216.     return e;
  217. }
  218.  
  219. Factor F(double b){
  220.     Factor a;
  221.     a.n = b;
  222.     a.name = "";
  223.     a.pot = 0;
  224.     return fixFactor(a);
  225. }
  226.  
  227. Factor sqrt(Factor a){
  228.     if(a.inf)
  229.         return a;
  230.     Factor b = fixFactor(a);
  231.     if(b.pot%2==1){
  232.         b.n *=10;
  233.         b.pot--;
  234.     }
  235.     b.n = std::sqrt(b.n);
  236.     b.pot = b.pot/2;
  237.     return fixFactor(b);
  238. }
  239.  
  240. Factor cbrt(Factor a){
  241.     if(a.inf)
  242.         return a;
  243.     Factor b = fixFactor(a);
  244.     if(b.pot%2==1){
  245.         b.n *=10;
  246.         b.pot--;
  247.     }
  248.     b.n = std::cbrt(b.n);
  249.     b.pot = b.pot/2;
  250.     return fixFactor(b);
  251. }
  252.  
  253. Factor corruptFactor(Factor a){
  254.     if(a.inf)
  255.         return a;
  256.     for(int x = 0; x<a.pot; x++){
  257.         a.n *= 10;
  258.     }
  259.     for(int x = 0; x>a.pot; x--){
  260.         a.n /= 10;
  261.     }
  262.     a.pot = 0;
  263.     return a;
  264. }
  265.  
  266. Factor tan(Factor a){
  267.     if(a.inf)
  268.         return a;
  269.     if(degrees){
  270.         a.n *= 2*PI;
  271.         a.n /= 360;
  272.     }
  273.     Factor b = corruptFactor(a);
  274.     b.n = std::tan(b.n);
  275.     return fixFactor(b);
  276. }
  277.  
  278. Factor atan(Factor a){
  279.     if(a.inf){
  280.         if(degrees){
  281.             return F(90);
  282.         }
  283.         return F(PI/2);
  284.     }
  285.     Factor b = corruptFactor(a);
  286.     b.n = std::atan(b.n);
  287.     if(degrees){
  288.         a.n *= 360;
  289.         a.n /= 2*PI;
  290.     }
  291.     return fixFactor(b);
  292. }
  293.  
  294. Factor sin(Factor a){
  295.     if(a.inf)
  296.         return a;
  297.     if(degrees){
  298.         a.n *= 2*PI;
  299.         a.n /= 360;
  300.     }
  301.     Factor b = corruptFactor(a);
  302.     b.n = std::sin(b.n);
  303.     return fixFactor(b);
  304. }
  305.  
  306. Factor asin(Factor a){
  307.     if(a.inf)
  308.         return a;
  309.     Factor b = corruptFactor(a);
  310.     b.n = std::asin(b.n);
  311.     if(degrees){
  312.         a.n *= 360;
  313.         a.n /= 2*PI;
  314.     }
  315.     return fixFactor(b);
  316. }
  317.  
  318. Factor cos(Factor a){
  319.     if(a.inf)
  320.         return a;
  321.     if(degrees){
  322.         a.n *= 2*PI;
  323.         a.n /= 360;
  324.     }
  325.     Factor b = corruptFactor(a);
  326.     b.n = std::cos(b.n);
  327.     return fixFactor(b);
  328. }
  329.  
  330. Factor acos(Factor a){
  331.     if(a.inf)
  332.         return a;
  333.     Factor b = corruptFactor(a);
  334.     b.n = std::acos(b.n);
  335.     if(degrees){
  336.         a.n *= 360;
  337.         a.n /= 2*PI;
  338.     }
  339.     return fixFactor(b);
  340. }
  341.  
  342.  
  343. Factor::Factor()
  344. {
  345.     //ctor
  346. }
  347.  
  348. Factor::~Factor()
  349. {
  350.     //dtor
  351. }
Advertisement
Add Comment
Please, Sign In to add comment