Guest User

Untitled

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