Advertisement
Mbxvim

Untitled

Jun 2nd, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include<cstdlib>
  2. #include<cstring>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. struct polynom
  7. {
  8.     int deg;
  9.     int coef;
  10.     polynom* next;
  11. };
  12. typedef polynom* Pointer;
  13. Pointer Head = NULL;
  14. int M = 0;
  15. void input()
  16. {
  17.     Pointer NewEl = NULL;
  18.     int i, k = 0;
  19.     char pol[100], p[100];
  20.     cout << "vvedite polynom: ";
  21.     cin >> pol;
  22.     int N = strlen(pol);
  23.     for (i = 0; i <N; i++)
  24.     {
  25.         if ((i != N - 1) && (pol[i] != 'x') && (pol[i] != '^') && (pol[i] != '+') && (pol[i] != '-'))
  26.         {
  27.             p[k] = pol[i];
  28.             k++;
  29.         }
  30.         else if (pol[i] == 'x')
  31.         {
  32.             p[k] = '\0';
  33.             k = 0;
  34.             NewEl = new polynom;
  35.             NewEl->coef = atof(p);
  36.             NewEl->next = Head;
  37.             Head = NewEl;
  38.             M++;
  39.         }
  40.         else if ((i == N - 1) || (pol[i] == '+') || (pol[i] == '-'))
  41.         {
  42.             if (i == N - 1)
  43.             {
  44.                 p[k] = pol[i];
  45.                 k++;
  46.             }
  47.             p[k] = '\0';
  48.             k = 1;
  49.             if (NewEl)
  50.                 NewEl->deg = atof(p);
  51.             p[0] = pol[i];
  52.         }
  53.     }
  54. }
  55. void Gornir(Pointer q)
  56. {
  57.     string s = to_string(q->coef);
  58.     if (q->next != NULL) {
  59.       if (q->next->deg + 1 == q->deg) {
  60.            q = q->next;
  61.         s =  "("+ s + "x + " + to_string(q->coef) + ")x";
  62.         }
  63.       q = q->next;
  64.         while (q->next != NULL) {
  65.             for (int i =  q->next->deg; i <=q->deg; i++) {
  66.                 if (q->deg == i) {
  67.                     s = "(" + s + " + " + to_string(q->coef) + ")x";
  68.                     q = q->next;
  69.                     continue;
  70.                 }
  71.                 s = "(" + s + ")x";
  72.             }
  73.             q = q->next;
  74.         }
  75.    
  76.     if (q->deg == 0) {
  77.         s = s + " + " + to_string(q->coef);
  78.     }
  79.     }
  80.     cout << s;
  81. }
  82.  
  83. int main()
  84. {
  85.     input();
  86.     cout << "\nPolynom: ";
  87.     Gornir(Head);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement