Advertisement
RadioNurshat

Полином

Mar 1st, 2021
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.28 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. #include <exception>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include <sstream>
  8. using namespace std;
  9.  
  10. vector<string> Split(const string& str, const string& delim)
  11. {
  12.     vector<string> tokens;
  13.     size_t prev = 0, pos = 0;
  14.     do
  15.     {
  16.         pos = str.find(delim, prev);
  17.         if (pos == string::npos) pos = str.length();
  18.         string token = str.substr(prev, pos - prev);
  19.         if (!token.empty()) tokens.push_back(token);                              
  20.         prev = pos + delim.length();
  21.     } while (pos < str.length() && prev < str.length());
  22.     return tokens;
  23. }
  24.  
  25.  
  26.  
  27. class Element {
  28. private:
  29.     bool isEqualExp(Element& that) {
  30.         return this->exponent == that.exponent;
  31.     }
  32.    
  33. public:
  34.    
  35.  
  36.     int exponent;
  37.     int prefix;
  38.     string ToString(bool full = false) {
  39.         string s;
  40.         if (this->prefix < 0) { s += "-"; }
  41.         else {
  42.             if (full) {
  43.                 s += "+";
  44.             }
  45.         }
  46.         s += to_string(abs(this->prefix));
  47.         //s += RightVersion(this->exponent);
  48.         if (this->exponent > 0) {
  49.             s += "^" + to_string(this->exponent) + "";
  50.         }
  51.        
  52.         return s;
  53.  
  54.     }
  55.     //      -4^5
  56.  
  57.     //[0] - "-4"
  58.     //[1] - "5"
  59.     //-2
  60.     static Element Resolve(string s) {
  61.         vector<string> in = Split(s, "^");
  62.  
  63.  
  64.  
  65.  
  66.  
  67.         if (in.size() == 1) {
  68.             stringstream spre(s);
  69.             int pre;
  70.             if ((spre >> pre).fail() || !(spre >> std::ws).eof())
  71.             {
  72.                 string out = "Not convertable to int at Element::Resolve at " + s;
  73.                 throw exception(out.c_str());
  74.             }
  75.             return Element(0, pre);
  76.  
  77.         }
  78.  
  79.  
  80.         stringstream spre(in[0]);
  81.         stringstream sexp(in[1]);
  82.         int pre;
  83.         int exp;
  84.         //[0] - "-4"
  85.     //[1] - "5"
  86.         //cout << in[0] << " " << in[1] << endl;
  87.         if ((spre >> pre).fail() || !(spre >> std::ws).eof())
  88.         {
  89.             string out = "Not convertable to int at Element::Resolve at " + in[0];
  90.             throw exception(out.c_str());
  91.         }
  92.         if ((sexp >> exp).fail() || !(sexp >> std::ws).eof())
  93.         {
  94.             string out = "Not convertable to int at Element::Resolve at " + in[1];
  95.             throw exception(out.c_str());
  96.         }
  97.         //cout << pre << " " << exp;
  98.  
  99.         Element elem(exp, pre);
  100.         return elem;
  101.     }
  102.     Element(int exponent, double prefix) {
  103.         this->exponent = exponent;
  104.         this->prefix = prefix;
  105.     }
  106.     Element(string s) {
  107.         Element elem = Element::Resolve(s);
  108.         this->exponent = elem.exponent;
  109.         this->prefix = elem.prefix;
  110. //        delete &elem;
  111.     }
  112.  
  113.  
  114.     Element operator+(Element &that) {
  115.         if (that.exponent == this->exponent) {
  116.             return Element(this->exponent, this->prefix + that.prefix);
  117.         }
  118.         else {
  119.             throw exception("Unaddable");
  120.         }
  121.     }
  122.     Element operator*(Element& that) {
  123.         return Element(this->exponent + that.exponent, this->prefix * that.prefix);
  124.     }
  125.     Element operator*(double pre) {
  126.         return Element(this->exponent, this->prefix * pre);
  127.     }
  128. };
  129.  
  130. struct compareDESC {
  131.     bool operator()(Element one, Element two) {
  132.         return (one.exponent > two.exponent);
  133.     }
  134. } comparatorDESC;
  135. struct compareASC {
  136.     bool operator()(Element one, Element two) {
  137.         return (one.exponent < two.exponent);
  138.     }
  139. } comparatorASC;
  140.  
  141. class Equation {
  142. public:
  143.     vector<Element> elements;
  144.     int getNumByExponent(int exponent) {
  145.        
  146.         for (int i = 0; i < this->elements.size(); i++) {
  147.             Element elem = elements[i];
  148.             if (elem.exponent == exponent) {
  149.                 return i;
  150.             }
  151.         }
  152.         return -1;
  153.     }
  154.  
  155.     void Add(Element elem) {
  156.         this->elements.push_back(elem);
  157.     }
  158.     void Sanitize() {
  159.         for (int i = 0; i < this->elements.size(); i++) {
  160.             if (this->elements[i].prefix == 0) {
  161.                 this->elements.erase(elements.begin() + i);
  162.             }
  163.         }
  164.     }
  165.     void Normalize() {
  166.         this->SortDESC();
  167.         vector<Element> out;
  168.         int ce = this->elements[0].exponent;
  169.         int ci = 0;
  170.         int exti = 0;
  171.         for (int i = 1; i < this->elements.size(); i++) {
  172.             if (ce != elements[i].exponent) {
  173.                 //cout << "Non Found at "<< i << endl;
  174.                 out.push_back(this->elements[ci]);
  175.                 ci = i;
  176.                 ce = elements[i].exponent;
  177.                
  178.             }
  179.             else {
  180.                // cout << "Found at " << i << " added to " << ci << endl;
  181.                 this->elements[ci].prefix += this->elements[i].prefix;
  182.             }
  183.             exti = i;
  184.         }
  185.         out.push_back(this->elements[ci]);
  186.         this->elements = out;
  187.         this->SortDESC();
  188.     }
  189.     void SortDESC() {
  190.         sort(this->elements.begin(), this->elements.end(), comparatorDESC);
  191.     }
  192.     void SortASC() {
  193.         sort(this->elements.begin(), this->elements.end(), comparatorASC);
  194.     }
  195.     string ToString() {
  196.         string s;
  197.         s += elements[0].ToString();
  198.         for (int i = 1; i < this->elements.size(); i++) {
  199.             s += elements[i].ToString(true);
  200.         }
  201.         return s;
  202.     }
  203.     Equation operator+(Equation& that) {
  204.         Equation another = that;
  205.         this->Normalize();
  206.         another.Normalize();
  207.         for (int i = 0; i < this->elements.size(); i++) {
  208.             int ci = another.getNumByExponent(this->elements[i].exponent);
  209.             if (ci == -1) {
  210.                 another.elements.push_back(this->elements[i]);
  211.             }
  212.             else {
  213.                 another.elements[ci].prefix += this->elements[i].prefix;
  214.             }
  215.         }
  216.         another.Normalize();
  217.         another.Sanitize();
  218.         return another;
  219.     }
  220.     Equation operator*(Equation& that) {
  221.         Equation an;
  222.         for (int i = 0; i < this->elements.size(); i++) {
  223.             for (int j = 0; j < that.elements.size(); j++) {
  224.                 an.Add(this->elements[i] * that.elements[j]);
  225.             }
  226.         }
  227.         an.Normalize();
  228.         an.Sanitize();
  229.         return an;
  230.     }
  231.     //5^4    +93^2    -2^1    
  232.     static Equation Resolve(string s) {
  233.         string current;
  234.         vector<string> out;
  235.         for (int i = 0; i < s.size(); i++) {
  236.             if (s[i] == '+' || s[i] == '-') {
  237.                 if (!current.empty()) {
  238.                     out.push_back(current);
  239.                 }
  240.                 current = s[i];
  241.             }
  242.             else {
  243.                 current += s[i];
  244.             }
  245.         }
  246.         if (!current.empty()) {
  247.             out.push_back(current);
  248.         }
  249.         Equation eq;
  250.         for (int i = 0; i < out.size(); i++) {
  251.             eq.Add(Element(out[i]));
  252.         }
  253.         return eq;
  254.     }
  255. };
  256.  
  257. int main() {
  258.     try {
  259.         Element elem = Element::Resolve("-4^5");
  260.  
  261.  
  262.         Equation eq = Equation::Resolve("5^4+3^2-2^1");
  263.         Equation bq = Equation::Resolve("-2^4-3^2-2");
  264.         Equation cq = eq + bq;
  265.         cout << cq.ToString();
  266.     }
  267.     catch (exception e) {
  268.         cout << e.what();
  269.     }
  270.  
  271.  
  272.    
  273. }
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement