Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <string>
- #include <exception>
- #include <fstream>
- #include <algorithm>
- #include <sstream>
- using namespace std;
- vector<string> Split(const string& str, const string& delim)
- {
- vector<string> tokens;
- size_t prev = 0, pos = 0;
- do
- {
- pos = str.find(delim, prev);
- if (pos == string::npos) pos = str.length();
- string token = str.substr(prev, pos - prev);
- if (!token.empty()) tokens.push_back(token);
- prev = pos + delim.length();
- } while (pos < str.length() && prev < str.length());
- return tokens;
- }
- class Element {
- private:
- bool isEqualExp(Element& that) {
- return this->exponent == that.exponent;
- }
- public:
- int exponent;
- int prefix;
- string ToString(bool full = false) {
- string s;
- if (this->prefix < 0) { s += "-"; }
- else {
- if (full) {
- s += "+";
- }
- }
- s += to_string(abs(this->prefix));
- //s += RightVersion(this->exponent);
- if (this->exponent > 0) {
- s += "^" + to_string(this->exponent) + "";
- }
- return s;
- }
- // -4^5
- //[0] - "-4"
- //[1] - "5"
- //-2
- static Element Resolve(string s) {
- vector<string> in = Split(s, "^");
- if (in.size() == 1) {
- stringstream spre(s);
- int pre;
- if ((spre >> pre).fail() || !(spre >> std::ws).eof())
- {
- string out = "Not convertable to int at Element::Resolve at " + s;
- throw exception(out.c_str());
- }
- return Element(0, pre);
- }
- stringstream spre(in[0]);
- stringstream sexp(in[1]);
- int pre;
- int exp;
- //[0] - "-4"
- //[1] - "5"
- //cout << in[0] << " " << in[1] << endl;
- if ((spre >> pre).fail() || !(spre >> std::ws).eof())
- {
- string out = "Not convertable to int at Element::Resolve at " + in[0];
- throw exception(out.c_str());
- }
- if ((sexp >> exp).fail() || !(sexp >> std::ws).eof())
- {
- string out = "Not convertable to int at Element::Resolve at " + in[1];
- throw exception(out.c_str());
- }
- //cout << pre << " " << exp;
- Element elem(exp, pre);
- return elem;
- }
- Element(int exponent, double prefix) {
- this->exponent = exponent;
- this->prefix = prefix;
- }
- Element(string s) {
- Element elem = Element::Resolve(s);
- this->exponent = elem.exponent;
- this->prefix = elem.prefix;
- // delete &elem;
- }
- Element operator+(Element &that) {
- if (that.exponent == this->exponent) {
- return Element(this->exponent, this->prefix + that.prefix);
- }
- else {
- throw exception("Unaddable");
- }
- }
- Element operator*(Element& that) {
- return Element(this->exponent + that.exponent, this->prefix * that.prefix);
- }
- Element operator*(double pre) {
- return Element(this->exponent, this->prefix * pre);
- }
- };
- struct compareDESC {
- bool operator()(Element one, Element two) {
- return (one.exponent > two.exponent);
- }
- } comparatorDESC;
- struct compareASC {
- bool operator()(Element one, Element two) {
- return (one.exponent < two.exponent);
- }
- } comparatorASC;
- class Equation {
- public:
- vector<Element> elements;
- int getNumByExponent(int exponent) {
- for (int i = 0; i < this->elements.size(); i++) {
- Element elem = elements[i];
- if (elem.exponent == exponent) {
- return i;
- }
- }
- return -1;
- }
- void Add(Element elem) {
- this->elements.push_back(elem);
- }
- void Sanitize() {
- for (int i = 0; i < this->elements.size(); i++) {
- if (this->elements[i].prefix == 0) {
- this->elements.erase(elements.begin() + i);
- }
- }
- }
- void Normalize() {
- this->SortDESC();
- vector<Element> out;
- int ce = this->elements[0].exponent;
- int ci = 0;
- int exti = 0;
- for (int i = 1; i < this->elements.size(); i++) {
- if (ce != elements[i].exponent) {
- //cout << "Non Found at "<< i << endl;
- out.push_back(this->elements[ci]);
- ci = i;
- ce = elements[i].exponent;
- }
- else {
- // cout << "Found at " << i << " added to " << ci << endl;
- this->elements[ci].prefix += this->elements[i].prefix;
- }
- exti = i;
- }
- out.push_back(this->elements[ci]);
- this->elements = out;
- this->SortDESC();
- }
- void SortDESC() {
- sort(this->elements.begin(), this->elements.end(), comparatorDESC);
- }
- void SortASC() {
- sort(this->elements.begin(), this->elements.end(), comparatorASC);
- }
- string ToString() {
- string s;
- s += elements[0].ToString();
- for (int i = 1; i < this->elements.size(); i++) {
- s += elements[i].ToString(true);
- }
- return s;
- }
- Equation operator+(Equation& that) {
- Equation another = that;
- this->Normalize();
- another.Normalize();
- for (int i = 0; i < this->elements.size(); i++) {
- int ci = another.getNumByExponent(this->elements[i].exponent);
- if (ci == -1) {
- another.elements.push_back(this->elements[i]);
- }
- else {
- another.elements[ci].prefix += this->elements[i].prefix;
- }
- }
- another.Normalize();
- another.Sanitize();
- return another;
- }
- Equation operator*(Equation& that) {
- Equation an;
- for (int i = 0; i < this->elements.size(); i++) {
- for (int j = 0; j < that.elements.size(); j++) {
- an.Add(this->elements[i] * that.elements[j]);
- }
- }
- an.Normalize();
- an.Sanitize();
- return an;
- }
- //5^4 +93^2 -2^1
- static Equation Resolve(string s) {
- string current;
- vector<string> out;
- for (int i = 0; i < s.size(); i++) {
- if (s[i] == '+' || s[i] == '-') {
- if (!current.empty()) {
- out.push_back(current);
- }
- current = s[i];
- }
- else {
- current += s[i];
- }
- }
- if (!current.empty()) {
- out.push_back(current);
- }
- Equation eq;
- for (int i = 0; i < out.size(); i++) {
- eq.Add(Element(out[i]));
- }
- return eq;
- }
- };
- int main() {
- try {
- Element elem = Element::Resolve("-4^5");
- Equation eq = Equation::Resolve("5^4+3^2-2^1");
- Equation bq = Equation::Resolve("-2^4-3^2-2");
- Equation cq = eq + bq;
- cout << cq.ToString();
- }
- catch (exception e) {
- cout << e.what();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement