Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef FACTOR_H
- #define FACTOR_H
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #define PI 3.14
- using namespace std;
- int digits = 0;
- bool degrees = true;
- ofstream* o;
- struct SQ{
- string name;
- string a;
- string b;
- string c;
- };
- class Factor
- {
- public:
- double n = 0;
- long long int pot = 0;
- bool inf = false;
- string name = "";
- Factor();
- virtual ~Factor();
- protected:
- private:
- };
- Factor nullFactor;
- template<typename T>
- void Send(T a){
- *o << a;
- cout << a;
- }
- template<typename T>
- void Send(T a, bool b){
- *o << a << "\n";
- cout << a << "\n";
- }
- void Send(Factor a, bool b){
- if(a.inf){
- *o << "inf\n";
- cout << "inf\n";
- return;
- }
- *o << a.n << "*10^" << a.pot << "\n";
- cout << a.n << "*10^" << a.pot << "\n";
- }
- void Send(Factor a){
- if(a.inf){
- *o << "inf";
- cout << "inf";
- return;
- }
- *o << a.n << "*10^" << a.pot;
- cout << a.n << "*10^" << a.pot;
- }
- void Send(Factor* a, int n, char c, bool d){
- *o << a[0].n << "*10^" << a[0].pot;
- cout << a[0].n << "*10^" << a[0].pot;
- for(int x = 1; x<n; x++){
- *o << c << a[x].n << "*10^" << a[x].pot;
- cout << c << a[x].n << "*10^" << a[x].pot;
- }
- *o << "\n";
- cout << "\n";
- }
- void Send(Factor* a, int n, char c){
- *o << a[0].n << "*10^" << a[0].pot;
- cout << a[0].n << "*10^" << a[0].pot;
- for(int x = 1; x<n; x++){
- *o << c << a[x].n << "*10^" << a[x].pot;
- cout << c << a[x].n << "*10^" << a[x].pot;
- }
- }
- Factor fixFactor(Factor a){
- if(a.n==INFINITY||a.inf){
- a.pot = 0;
- a.inf = true;
- a.n = INFINITY;
- return a;
- }
- while(a.n*a.n>=100&&a.n!=INFINITY){
- a.pot++;
- a.n/=10;
- }
- while(a.n*a.n<1&&a.n!=0){
- a.pot--;
- a.n*=10;
- }
- if(digits>0){
- for(int x = 0; x<digits; x++){
- a.n*=10;
- }
- a.n = floor(a.n+0.5f);
- for(int x = 0; x<digits; x++){
- a.n/=10;
- }
- }
- return a;
- }
- bool operator==(Factor a, Factor b){
- if(a.inf||b.inf){
- return !(a.inf xor b.inf);
- }
- Factor c, d;
- c = fixFactor(a);
- d = fixFactor(b);
- return c.n==d.n&&c.pot==d.pot;
- }
- bool operator<(Factor a, Factor b){
- if(a.inf&&!b.inf){
- return true;
- }
- if(b.inf){
- return false;
- }
- Factor c, d;
- c = fixFactor(a);
- d = fixFactor(b);
- if(c.pot<d.pot){
- return true;
- }
- if(c.pot==d.pot&&c.n<d.n){
- return true;
- }
- return false;
- }
- bool operator>(Factor a, Factor b){
- return b<a;
- }
- bool operator>=(Factor a, Factor b){
- return b<a||b==a;
- }
- bool operator<=(Factor a, Factor b){
- return a<b||a==b;
- }
- bool operator!=(Factor a, Factor b){
- return !(a==b);
- }
- Factor operator+(Factor a, Factor b){
- if(a.inf||b.inf){
- Factor c;
- c.inf = true;
- c.n = INFINITY;
- c.pot = 0;
- c.name = a.name;
- return c;
- }
- Factor c,d;
- c = fixFactor(a); d = fixFactor(b);
- for(int x = c.pot; x<d.pot; x++){
- c.n /= 10;
- c.pot++;
- }
- for(int x = d.pot; x<c.pot; x++){
- d.n /= 10;
- d.pot++;
- }
- c.n += d.n;
- return fixFactor(c);
- }
- Factor operator-(Factor a, Factor b){
- if(a.inf){
- Factor c;
- c.inf = true;
- c.n = INFINITY;
- c.pot = 0;
- c.name = a.name;
- return c;
- }
- else if(b.inf){
- Factor c;
- c.inf = true;
- c.n = -INFINITY;
- c.pot = 0;
- c.name = a.name;
- return c;
- }
- Factor c,d;
- c = fixFactor(a); d = fixFactor(b);
- for(int x = c.pot; x<d.pot; x++){
- c.n /= 10;
- c.pot++;
- }
- for(int x = d.pot; x<c.pot; x++){
- c.n *= 10;
- c.pot--;
- }
- c.n -= d.n;
- return fixFactor(c);
- }
- Factor operator-(Factor a){
- Factor b = fixFactor(a);
- b.n = -b.n;
- return b;
- }
- Factor operator*(Factor a, Factor b){
- if(a.inf||b.inf){
- Factor c;
- c.inf = true;
- c.n = INFINITY;
- c.pot = 0;
- c.name = a.name + "*" + b.name;
- return c;
- }
- Factor c, d;
- c = fixFactor(a);
- d = fixFactor(b);
- c.n = c.n*d.n;
- c.pot += d.pot;
- c.name = c.name + "*" + d.name;
- return fixFactor(c);
- }
- Factor operator/(Factor a, Factor b){
- if(a.inf||b==nullFactor){
- Factor c;
- c.inf = true;
- c.n = INFINITY;
- c.pot = 0;
- c.name = a.name + "/(" + b.name + ")";
- return c;
- }
- else if(b.inf||a==nullFactor){
- Factor c = nullFactor;
- c.name = a.name + "/(" + b.name + ")";
- return c;
- }
- Factor c, d;
- c = fixFactor(a);
- d = fixFactor(b);
- c.n = c.n/d.n;
- c.pot -= d.pot;
- c.name = c.name + "/(" + d.name + ")";
- Factor e = fixFactor(c);
- return e;
- }
- Factor F(double b){
- Factor a;
- a.n = b;
- a.name = "";
- a.pot = 0;
- return fixFactor(a);
- }
- Factor sqrt(Factor a){
- if(a.inf)
- return a;
- Factor b = fixFactor(a);
- if(b.pot%2==1){
- b.n *=10;
- b.pot--;
- }
- b.n = std::sqrt(b.n);
- b.pot = b.pot/2;
- return fixFactor(b);
- }
- Factor cbrt(Factor a){
- if(a.inf)
- return a;
- Factor b = fixFactor(a);
- if(b.pot%2==1){
- b.n *=10;
- b.pot--;
- }
- b.n = std::cbrt(b.n);
- b.pot = b.pot/2;
- return fixFactor(b);
- }
- Factor corruptFactor(Factor a){
- if(a.inf)
- return a;
- for(int x = 0; x<a.pot; x++){
- a.n *= 10;
- }
- for(int x = 0; x>a.pot; x--){
- a.n /= 10;
- }
- a.pot = 0;
- return a;
- }
- Factor tan(Factor a){
- if(a.inf)
- return a;
- if(degrees){
- a.n *= 2*PI;
- a.n /= 360;
- }
- Factor b = corruptFactor(a);
- b.n = std::tan(b.n);
- return fixFactor(b);
- }
- Factor atan(Factor a){
- if(a.inf){
- if(degrees){
- return F(90);
- }
- return F(PI/2);
- }
- Factor b = corruptFactor(a);
- b.n = std::atan(b.n);
- if(degrees){
- a.n *= 360;
- a.n /= 2*PI;
- }
- return fixFactor(b);
- }
- Factor sin(Factor a){
- if(a.inf)
- return a;
- if(degrees){
- a.n *= 2*PI;
- a.n /= 360;
- }
- Factor b = corruptFactor(a);
- b.n = std::sin(b.n);
- return fixFactor(b);
- }
- Factor asin(Factor a){
- if(a.inf)
- return a;
- Factor b = corruptFactor(a);
- b.n = std::asin(b.n);
- if(degrees){
- a.n *= 360;
- a.n /= 2*PI;
- }
- return fixFactor(b);
- }
- Factor cos(Factor a){
- if(a.inf)
- return a;
- if(degrees){
- a.n *= 2*PI;
- a.n /= 360;
- }
- Factor b = corruptFactor(a);
- b.n = std::cos(b.n);
- return fixFactor(b);
- }
- Factor acos(Factor a){
- if(a.inf)
- return a;
- Factor b = corruptFactor(a);
- b.n = std::acos(b.n);
- if(degrees){
- a.n *= 360;
- a.n /= 2*PI;
- }
- return fixFactor(b);
- }
- #endif // FACTOR_H
Advertisement
Add Comment
Please, Sign In to add comment