Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. #define INPUT "input.txt"
  8. #define OUTPUT "output.txt"
  9.  
  10. struct Monomial
  11. {
  12.     int exponent, multiplier = 0;
  13.     Monomial* next = NULL;
  14.     Monomial* last = NULL;
  15. };
  16.  
  17. int split(string line, Monomial* pX)
  18. {
  19.     cout << line << "\n";
  20.  
  21.     int pos, sign, multiplier, exponent;
  22.     bool find = false;
  23.  
  24.     if ((pos = line.find("x") != (-1)))
  25.     {
  26.  
  27.         if (line[0] == '-')
  28.             sign = (-1);
  29.         else
  30.             sign = 1;
  31.  
  32.         if ((line[0] == '-') || (line[0] == '+'))
  33.             line.erase(0, 1);
  34.  
  35.         pos = line.find("x");
  36.         if (pos != (-1))
  37.         {
  38.             if (pos == 0)
  39.                 multiplier = sign;
  40.             else
  41.                 multiplier = sign * (stoi(line.substr(0, pos)));
  42.  
  43.             if (line != "x")
  44.             {
  45.                 pos = 0;
  46.                 pos = line.find("^");
  47.                 if (pos == (-1))
  48.                     exponent = 1;
  49.                 else
  50.                     exponent = stoi(line.substr(pos + 1));
  51.             }
  52.             else
  53.                 exponent = 1;
  54.         }
  55.         else
  56.         {
  57.             if ((line != ""))
  58.                 multiplier = sign * stoi(line);
  59.             else multiplier = sign;
  60.             exponent = 0;
  61.         }
  62.     }
  63.     else
  64.     {
  65.         exponent = 0;
  66.         multiplier = stoi(line);
  67.     }
  68.  
  69.  
  70.     cout << multiplier << "\n";
  71.     cout << exponent << "\n";
  72.  
  73.     while (find == false)
  74.     {
  75.         if (pX->exponent == exponent)
  76.         {
  77.             pX->multiplier += multiplier;
  78.             find = true;
  79.             cout << "^^^^^^^^^^^^^" << "\n";
  80.             cout << pX->multiplier << "\n";
  81.             cout << pX->exponent << "\n";
  82.             cout << "^^^^^^^^^^^^^" << "\n";
  83.         }
  84.         else
  85.         {
  86.             if (pX->next != NULL)
  87.             {
  88.                 pX = pX->next;
  89.             }
  90.             else
  91.             {
  92.                 pX->next = new Monomial;
  93.                 pX->next->exponent = exponent;
  94.                 pX->next->multiplier += multiplier;
  95.                 pX->next->last = pX;
  96.                 //pX = pX->next;
  97.                 find = true;
  98.             }
  99.         }
  100.     }
  101.  
  102.     return pX->exponent;
  103.  
  104. }
  105.  
  106.  
  107. void sort(Monomial* pX)
  108. {
  109.     int ex, mult;
  110.     Monomial* firstPX = pX;
  111.     Monomial* buf;
  112.     bool check = false;
  113.     while (check == false)
  114.     {
  115.         pX = firstPX;
  116.         while (pX->next != NULL)
  117.         {
  118.            
  119.             if (pX->exponent < pX->next->exponent)
  120.             {
  121.                 ex = pX->exponent;
  122.                 mult = pX->multiplier;
  123.                 pX->exponent = pX->next->exponent;
  124.                 pX->multiplier = pX->next->multiplier;
  125.                 buf = pX->next->last;
  126.                 pX->next->last = pX;
  127.                 pX->last = buf;
  128.                 pX->next->exponent = ex;
  129.                 pX->next->multiplier = mult;
  130.             }
  131.             pX = pX->next;
  132.  
  133.         }
  134.         pX = firstPX;
  135.         check = true;
  136.         while (pX->next != NULL)
  137.         {
  138.             if (pX->exponent < pX->next->exponent)
  139.                 check = false;
  140.             pX = pX->next;
  141.         }
  142.     }
  143.  
  144.     pX = firstPX;
  145.     while (pX != NULL)
  146.     {
  147.         cout << "0000000000000" << "\n";
  148.         cout << pX->multiplier << "\n";
  149.         cout << pX->exponent << "\n";
  150.         cout << "0000000000000" << "\n";
  151.         pX = pX->next;
  152.     }
  153. }
  154.  
  155.  
  156. void main()
  157. {
  158.     char ch;
  159.     string line;
  160.     int max = 0, pMax = 0;
  161.  
  162.     Monomial* pX = new Monomial;
  163.     pX->exponent = 0;
  164.     Monomial* lastPX = pX;
  165.  
  166.  
  167.     std::fstream file;
  168.     file.open(INPUT);
  169.  
  170.     if (file.is_open())
  171.     {
  172.         if (!file.eof())
  173.             ch = file.get();
  174.  
  175.         while (!file.eof())
  176.         {
  177.             line = line + ch;
  178.             ch = file.get();
  179.             if ((ch == '+') || (ch == '-') || file.eof())
  180.             {
  181.                 try {
  182.                     pMax = split(line, pX);
  183.                 }
  184.                 catch (const exception error)
  185.                 {
  186.                     cout << "ERROR";
  187.                     return;
  188.                 }
  189.                 if (pMax > max)
  190.                     max = pMax;
  191.                 cout << "===============" << "\n";
  192.                 line = "";
  193.             }
  194.         }
  195.     }
  196.     file.close();
  197.  
  198.     cout << "da"<<"\n";
  199.     sort(pX);
  200.     //while (lastPX->exponent < max)
  201.     lastPX = pX;
  202.  
  203.  
  204.  
  205.     file.open(OUTPUT, ios_base::out | ios_base::trunc);
  206.     if (file.is_open())
  207.     {
  208.         line = "";
  209.             while (lastPX->next != 0)
  210.             {
  211.                 if (lastPX->multiplier > 0)
  212.                     if (lastPX->multiplier > 1) line = line + "+" + to_string(lastPX->multiplier) + "x" + "^" + to_string(lastPX->exponent);
  213.                     else line = line + "+" + "x" + "^" + to_string(lastPX->exponent);
  214.                 if (lastPX->multiplier < 0)
  215.                     if (lastPX->multiplier < (-1)) line = line + to_string(lastPX->multiplier) + "x" + "^" + to_string(lastPX->exponent);
  216.                     else line = line + "-" + "x" + "^" + to_string(lastPX->exponent);
  217.                 lastPX = lastPX->next;
  218.             }
  219.  
  220.             if (lastPX->multiplier > 0)
  221.                 line = line + "+" + to_string(lastPX->multiplier);
  222.             if (lastPX->multiplier < 0)
  223.                 line = line + to_string(lastPX->multiplier);
  224.         if (line[0] == '+') line.erase(0, 1);
  225.         cout << line;
  226.         file << line;
  227.     }
  228.  
  229.     file.close();
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement