Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- #define INPUT "input.txt"
- #define OUTPUT "output.txt"
- struct Monomial
- {
- int exponent, multiplier = 0;
- Monomial* next = NULL;
- Monomial* last = NULL;
- };
- int split(string line, Monomial* pX)
- {
- cout << line << "\n";
- int pos, sign, multiplier, exponent;
- bool find = false;
- if ((pos = line.find("x") != (-1)))
- {
- if (line[0] == '-')
- sign = (-1);
- else
- sign = 1;
- if ((line[0] == '-') || (line[0] == '+'))
- line.erase(0, 1);
- pos = line.find("x");
- if (pos != (-1))
- {
- if (pos == 0)
- multiplier = sign;
- else
- multiplier = sign * (stoi(line.substr(0, pos)));
- if (line != "x")
- {
- pos = 0;
- pos = line.find("^");
- if (pos == (-1))
- exponent = 1;
- else
- exponent = stoi(line.substr(pos + 1));
- }
- else
- exponent = 1;
- }
- else
- {
- if ((line != ""))
- multiplier = sign * stoi(line);
- else multiplier = sign;
- exponent = 0;
- }
- }
- else
- {
- exponent = 0;
- multiplier = stoi(line);
- }
- cout << multiplier << "\n";
- cout << exponent << "\n";
- while (find == false)
- {
- if (pX->exponent == exponent)
- {
- pX->multiplier += multiplier;
- find = true;
- cout << "^^^^^^^^^^^^^" << "\n";
- cout << pX->multiplier << "\n";
- cout << pX->exponent << "\n";
- cout << "^^^^^^^^^^^^^" << "\n";
- }
- else
- {
- if (pX->next != NULL)
- {
- pX = pX->next;
- }
- else
- {
- pX->next = new Monomial;
- pX->next->exponent = exponent;
- pX->next->multiplier += multiplier;
- pX->next->last = pX;
- //pX = pX->next;
- find = true;
- }
- }
- }
- return pX->exponent;
- }
- void sort(Monomial* pX)
- {
- int ex, mult;
- Monomial* firstPX = pX;
- Monomial* buf;
- bool check = false;
- while (check == false)
- {
- pX = firstPX;
- while (pX->next != NULL)
- {
- if (pX->exponent < pX->next->exponent)
- {
- ex = pX->exponent;
- mult = pX->multiplier;
- pX->exponent = pX->next->exponent;
- pX->multiplier = pX->next->multiplier;
- buf = pX->next->last;
- pX->next->last = pX;
- pX->last = buf;
- pX->next->exponent = ex;
- pX->next->multiplier = mult;
- }
- pX = pX->next;
- }
- pX = firstPX;
- check = true;
- while (pX->next != NULL)
- {
- if (pX->exponent < pX->next->exponent)
- check = false;
- pX = pX->next;
- }
- }
- pX = firstPX;
- while (pX != NULL)
- {
- cout << "0000000000000" << "\n";
- cout << pX->multiplier << "\n";
- cout << pX->exponent << "\n";
- cout << "0000000000000" << "\n";
- pX = pX->next;
- }
- }
- void main()
- {
- char ch;
- string line;
- int max = 0, pMax = 0;
- Monomial* pX = new Monomial;
- pX->exponent = 0;
- Monomial* lastPX = pX;
- std::fstream file;
- file.open(INPUT);
- if (file.is_open())
- {
- if (!file.eof())
- ch = file.get();
- while (!file.eof())
- {
- line = line + ch;
- ch = file.get();
- if ((ch == '+') || (ch == '-') || file.eof())
- {
- try {
- pMax = split(line, pX);
- }
- catch (const exception error)
- {
- cout << "ERROR";
- return;
- }
- if (pMax > max)
- max = pMax;
- cout << "===============" << "\n";
- line = "";
- }
- }
- }
- file.close();
- cout << "da"<<"\n";
- sort(pX);
- //while (lastPX->exponent < max)
- lastPX = pX;
- file.open(OUTPUT, ios_base::out | ios_base::trunc);
- if (file.is_open())
- {
- line = "";
- while (lastPX->next != 0)
- {
- if (lastPX->multiplier > 0)
- if (lastPX->multiplier > 1) line = line + "+" + to_string(lastPX->multiplier) + "x" + "^" + to_string(lastPX->exponent);
- else line = line + "+" + "x" + "^" + to_string(lastPX->exponent);
- if (lastPX->multiplier < 0)
- if (lastPX->multiplier < (-1)) line = line + to_string(lastPX->multiplier) + "x" + "^" + to_string(lastPX->exponent);
- else line = line + "-" + "x" + "^" + to_string(lastPX->exponent);
- lastPX = lastPX->next;
- }
- if (lastPX->multiplier > 0)
- line = line + "+" + to_string(lastPX->multiplier);
- if (lastPX->multiplier < 0)
- line = line + to_string(lastPX->multiplier);
- if (line[0] == '+') line.erase(0, 1);
- cout << line;
- file << line;
- }
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement