Guest User

Untitled

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