Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.     int deg = 0;
  7.     double koef = 0;
  8.     node* next = nullptr;
  9.     node* prev = nullptr;
  10. };
  11.  
  12. struct polynomial {
  13.     node* poly;
  14.     polynomial* next = nullptr;
  15. };
  16.  
  17.  
  18. polynomial *HEAD = new polynomial, *END = HEAD;
  19.  
  20. pair<int, int> read_koef(int i, string &main_str) {
  21.     char c = main_str[i];
  22.     int koef = 0;
  23.  
  24.     while('0' <= c && c <= '9') {
  25.         koef *= 10;
  26.         koef += (int) (c - '0');
  27.         c = main_str[++i];
  28.     }
  29.  
  30.     if (koef == 0 && main_str[i - 1] != '0') return {1, i};
  31.  
  32.     return {koef, i};
  33. }
  34.  
  35. pair<string, int> read_name(int i, string &main_str) {
  36.     char c = main_str[i];
  37.     string dname = "";
  38.  
  39.     while( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ) {
  40.         dname += c;
  41.         ++i;
  42.         c = main_str[i];
  43.     }
  44.  
  45.     return {dname, i};
  46. }
  47.  
  48. pair<int, int> read_deg(int i, string &main_str) {
  49.     char c = main_str[i];
  50.  
  51.     if (c != '^') return {1, i};
  52.  
  53.     if (c == '^') {
  54.         c = main_str[++i];
  55.     }
  56.  
  57.     if (!('0' <= c && c <= '9') ) return {-1, i};
  58.  
  59.     int deg = 0;
  60.     while('0' <= c && c <= '9') {
  61.         deg *= 10;
  62.         deg += (int) (c - '0');
  63.         c = main_str[++i];
  64.     }
  65.  
  66.     return {deg, i};
  67. }
  68.  
  69. pair<string, int> read_sign(int i, string &main_str) {
  70.     if (main_str[i] != '+' && main_str[i] != '-' && i == 0) {
  71.         return {"+", i};
  72.     }
  73.  
  74.     char c = main_str[i];
  75.     string sg;
  76.  
  77.     sg = c;
  78.  
  79.     return {sg, i + 1};
  80. }
  81.  
  82. bool sign_bool(char c) {
  83.     if (c == '-') return 1;
  84.     if (c == '+') return 1;
  85.     if (c == '^') return 1;
  86.     if (c == '*') return 1;
  87.     return 0;
  88. }
  89.  
  90. void create_node(node* &h, int koef, int deg) {
  91.     h->deg = deg;
  92.     h->koef = koef;
  93.     h->next = nullptr;
  94.     h->prev = nullptr;
  95.     return;
  96. }
  97.  
  98. void add_node(node* &head, int koef, int deg){
  99.     if (koef == 0) return;
  100.     if (head->prev == nullptr) {
  101.         node* h = new (node);
  102.         create_node(h, koef, deg);
  103.         head->prev = h;
  104.         h->next = head;
  105.         return;
  106.     }
  107.  
  108.     node* p = head->prev;
  109.     node* h = new (node);
  110.     create_node(h, koef, deg);
  111.  
  112.     if (p->deg < h->deg) {
  113.         p->next = h;
  114.         h->prev = p;
  115.         h->next = head;
  116.         head->prev = h;
  117.         return;
  118.     }
  119.  
  120.     while (p->prev != nullptr && p->deg > h->deg) {
  121.         p = p->prev;
  122.     }
  123.  
  124.     if (h->deg > p->deg) {
  125.         p->next->prev = h;
  126.         h->next = p->next;
  127.         h->prev = p;
  128.         p->next = h;
  129.         return;
  130.     }
  131.     if (p->deg == h->deg) {
  132.         if (p->koef + h->koef == 0) {
  133.             if (p->prev == nullptr) {
  134.                 p->next->prev = nullptr;
  135.                 delete p;
  136.             }
  137.             else {
  138.                 p->next->prev = p->prev;
  139.                 p->prev->next = p->next;
  140.                 delete p;
  141.             }
  142.         }
  143.         else p->koef += h->koef;
  144.         delete h;
  145.         return;
  146.     }
  147.     if (h->deg < p->deg) {
  148.         p->prev = h;
  149.         h->next = p;
  150.         return;
  151.     }
  152. }
  153.  
  154. void print_node(node* head) {
  155.     node *p = head->prev;
  156.  
  157.     //if (name == "") name = "x";
  158.     string name = "x";
  159.  
  160.     if (p == nullptr) {
  161.         cout << 0 << endl;
  162.         return;
  163.     }
  164.  
  165.  
  166.     if (p->deg == 0) {
  167.         cout << p->koef;
  168.         p = p->prev;
  169.     }
  170.     else {
  171.         if (p->deg == 1) {
  172.             if (p->koef == -1) {
  173.                 cout << "-";
  174.             }
  175.             else cout << p->koef;
  176.             cout << name;
  177.         }
  178.         else {
  179.             if (p->koef == -1) {
  180.                 cout << "-";
  181.             }
  182.             else cout << p->koef;
  183.             cout << name << "^" << p->deg;
  184.         }
  185.  
  186.         p = p->prev;
  187.     }
  188.  
  189.     while(p != nullptr) {
  190.         if (p->koef == 1) {
  191.             if (p->deg == 0) {
  192.                 cout << "+" << p->koef;
  193.             }
  194.             else cout << "+" << name << "^" << p->deg;
  195.             p = p->prev;
  196.             continue;
  197.         }
  198.         if (p->koef == -1) {
  199.             if (p->deg == 0) {
  200.                 cout << "-" << p->koef;
  201.             }
  202.             else cout << "-" << name << "^" << p->deg;
  203.             p = p->prev;
  204.             continue;
  205.         }
  206.  
  207.  
  208.         if (p->koef > 0) cout << "+";
  209.         if (p->deg == 0) {
  210.             cout << p->koef;
  211.         }
  212.         else if (p->deg == 1) {
  213.             cout << p->koef << name;
  214.         }
  215.         else {
  216.             cout << p->koef << name << "^" << p->deg;
  217.         }
  218.         p = p->prev;
  219.     }
  220.         cout << endl;
  221. }
  222.  
  223. node* pol(string &main_str) {
  224.     bool turn;
  225.     string name = "";
  226.     for (int i = 0; i < main_str.size(); i++) {
  227.         if (!( ('0'<=main_str[i]&&main_str[i]<='9') || ('a'<=main_str[i]&&main_str[i]<='z') ||
  228.                ('A'<=main_str[i]&&main_str[i]<='Z') || main_str[i] == '^' ||
  229.                  main_str[i] == '+' || main_str[i] == '-' || main_str[i] == ' ')) {
  230.                 cout << "ERROR1!!!" << endl;
  231.                 main_str = "";
  232.                 name = "";
  233.                 return nullptr;
  234.              }
  235.     }
  236.  
  237.     for(int i = 0; i < main_str.size(); i++) {
  238.         if(main_str[i] == '*') {
  239.             main_str.erase(i, 1);
  240.             i--;
  241.         }
  242.     }
  243.  
  244.     for (int i = 0; i < main_str.size(); i++) {
  245.         if (i == 0) {
  246.             if (('a'<=main_str[i]&&main_str[i]<='z') || ('A'<=main_str[i]&&main_str[i]<='Z')) turn = 1;
  247.             else turn = 0;
  248.         }
  249.         else {
  250.             if ( ('a'<=main_str[i]&&main_str[i]<='z' || 'A'<=main_str[i]&&main_str[i]<='Z') &&
  251.                 !('a'<=main_str[i - 1]&&main_str[i - 1]<='z' || 'A'<=main_str[i - 1]&&main_str[i- 1]<='Z') ) {
  252.                 if (turn == 1) {
  253.                     cout << "ERROR2!!!" << endl;
  254.                     main_str = "";
  255.                     name = "";
  256.                     return nullptr;
  257.                 }
  258.                 turn = 0;
  259.             }
  260.             else if (main_str[i] != ' ') {
  261.                 turn = 0;
  262.             }
  263.         }
  264.     }
  265.  
  266.     for(int i = 0; i < main_str.size(); i++) {
  267.             if(main_str[i] == ' ') {
  268.                     main_str.erase(i, 1);
  269.                     i--;
  270.             }
  271.         }
  272.  
  273.     if (main_str.size() == 0) {
  274.         cout << "ERROR3!!!" << endl;
  275.         main_str = "";
  276.         name = "";
  277.         return nullptr;
  278.     }
  279.  
  280.  
  281.     for (int i = 0; i < main_str.size(); i++) {
  282.  
  283.         if (i != 0 && sign_bool(main_str[i]) && sign_bool(main_str[i - 1])) {
  284.             cout << "ERROR4!!!" << endl;
  285.             main_str = "";
  286.             name = "";
  287.             return nullptr;
  288.         }
  289.  
  290.         if (i != 0 && ('0'<=main_str[i]&&main_str[i]<='9') && (('a'<=main_str[i - 1]&&main_str[i - 1]<='z') ||
  291.                ('A'<=main_str[i - 1]&&main_str[i - 1]<='Z')) ) {
  292.             cout << "ERROR4!!!" << endl;
  293.             main_str = "";
  294.             name = "";
  295.             return nullptr;
  296.         }
  297.  
  298.         if (i != 1 && ('0'<=main_str[i - 2]&&main_str[i - 2]<='9') && main_str[i - 1] == '^' && ('0'<=main_str[i]&&main_str[i]<='9') ) {
  299.             cout << "ERROR4!!!" << endl;
  300.             main_str = "";
  301.             name = "";
  302.             return nullptr;
  303.         }
  304.  
  305.     }
  306.  
  307.     if (main_str.size() == 0 || (main_str.size() == 1 && sign_bool(main_str[0]))) {
  308.         cout << "ERROR5!!!" << endl;
  309.         main_str = "";
  310.         name = "";
  311.         return nullptr;
  312.     }
  313.  
  314.     //cout << main_str << endl << endl;
  315.  
  316.     node *head = new(node);
  317.  
  318.  
  319.     for (int i = 0; i < main_str.size();) {
  320.         int koef, deg;
  321.         string dname, sg = " ";
  322.         auto d1 = read_sign(i, main_str);
  323.         sg = d1.first;
  324.         i = d1.second;
  325.         auto d2 = read_koef(i, main_str);
  326.         koef = d2.first;
  327.         i = d2.second;
  328.         auto d3 = read_name(i, main_str);
  329.         dname = d3.first;
  330.         i = d3.second;
  331.         auto d4 = read_deg(i, main_str);
  332.         deg = d4.first;
  333.         i = d4.second;
  334.         if (dname == "") {
  335.             deg = 0;
  336.         }
  337.         if (deg < 0) {
  338.             cout << "ERROR4!!!" << endl;
  339.             main_str = "";
  340.             name = "";
  341.             return nullptr;
  342.         }
  343.         if (sg != "-" && sg != "+") {
  344.             cout << "ERROR5!!!" << endl;
  345.             main_str = "";
  346.             name = "";
  347.             return nullptr;
  348.         }
  349.         //cout << sg << " " << koef << dname << "^" << deg << endl;
  350.         if (sg == "-") koef = -koef;
  351.         if (name == "") {
  352.             name = dname;
  353.         }
  354.         else if (dname != name && dname != "") {
  355.             cout << "ERROR6!!!" << endl;
  356.             main_str = "";
  357.             name = "";
  358.             return nullptr;
  359.         }
  360.         add_node(head, koef, deg);
  361.     }
  362.  
  363.     print_node(head);
  364.     name = "";
  365.  
  366.  
  367.     return head;
  368. }
  369.  
  370. //#########################
  371.  
  372. void ins_after(int d,double k,node* &p,bool x) {
  373.     node *q = new node;
  374.     q->deg = d;
  375.     q->koef = k;
  376.     if (x) {
  377.         p->prev = q;
  378.         q->next = p;
  379.     } else
  380.         q->next = nullptr;
  381.     p = q;
  382. }
  383.  
  384. node* poly_num(polynomial* HEAD, int k) {
  385.     polynomial *h = HEAD;
  386.     node *L;
  387.     for (int i = 0; i < k; i++)
  388.         h = h->next;
  389.     L = h->poly;
  390.     return L;
  391. }
  392.  
  393. node* sum(node* L1,node* L2) {
  394.     node *head = new node;
  395.  
  396.     if (L1->prev != nullptr) {
  397.         node *p = L1->prev;
  398.         while (p != nullptr) {
  399.             add_node(head, p->koef, p->deg);
  400.             p = p->prev;
  401.         }
  402.         delete p;
  403.     }
  404.  
  405.     if (L2->prev != nullptr) {
  406.         node *p = L2->prev;
  407.         while (p != nullptr) {
  408.             add_node(head, p->koef, p->deg);
  409.             p = p->prev;
  410.         }
  411.         delete p;
  412.     }
  413.  
  414.     return head->prev;
  415. }
  416.  
  417. node* derivative(node* L1, int n) {
  418.     node *L, *p, *p1 = L1->prev;
  419.  
  420.     if (L1 != nullptr && L1->deg > 0) {
  421.         ins_after(p1->deg - 1, p1->koef * p1->deg, L, 0);
  422.         p1 = p1->prev;
  423.  
  424.         p = L;
  425.         while (p1 != nullptr && p1->deg > 0) {
  426.             ins_after(p1->deg - 1, p1->koef * p1->deg, p, 1);
  427.             p1 = p1->prev;
  428.         }
  429.         p->prev = nullptr;
  430.     } else
  431.         L = nullptr;
  432.  
  433.     if (n > 1)
  434.         derivative(L, n - 1);
  435.     else
  436.         return L;
  437. }
  438.  
  439. int check(node* L,int i) {
  440.     node *p = L;
  441.     int sum = 0, x;
  442.     while (p != nullptr) {
  443.         x = 1;
  444.         for (int j = 0; j < p->deg; j++)
  445.             x *= i;
  446.         sum += (x * (int) p->koef);
  447.         p = p->prev;
  448.     }
  449.     return sum;
  450. }
  451.  
  452. void roots(node *L) {
  453.     node *p = L->prev;
  454.     int free, x = 0;
  455.  
  456.     set<int> st;
  457.  
  458.     while (p->prev != nullptr)
  459.         p = p->prev;
  460.     if (p->deg > 0) {
  461.         st.insert(0);
  462.         x = 1;
  463.     }
  464.     free = (int) p->koef;
  465.     for (int i = 1; i * 1.0 <= sqrt(abs(free * 1.0)); i++) {
  466.         if (free % i == 0) {
  467.             if (check(L, i) == 0) {
  468.                 //cout << i << " ";
  469.                 st.insert(i);
  470.                 x = 1;
  471.             }
  472.             if (check(L, -i) == 0) {
  473.                 //cout << -i << " ";
  474.                 st.insert(-i);
  475.                 x = 1;
  476.             }
  477.             if (i * i != free && check(L, free / i) == 0) {
  478.                 //cout << free / i << " ";
  479.                 st.insert(free/i);
  480.                 x = 1;
  481.             }
  482.             if (i * i != free && check(L, -free / i) == 0) {
  483.                 //cout << -free / i << " ";
  484.                 st.insert(-free/i);
  485.                 x = 1;
  486.             }
  487.         }
  488.     }
  489.     if (st.size() == 0) cout << "No roots";
  490.     else {
  491.         cout << "Roots: ";
  492.         for (auto i: st) cout << i << " ";
  493.     }
  494.     cout << endl << endl;
  495. }
  496.  
  497. node* division(node* L1,node* L2,node* L0,node* p0)//L0=p0=NULL
  498. {
  499.     node *L, *head = new node;
  500.     int a;
  501.  
  502.     if (L1!=nullptr&&L1->deg >= L2->deg) {
  503.         a = L1->koef / L2->koef;
  504.         if (L0 == nullptr) {
  505.             ins_after(L1->deg - L2->deg, a, L0, 0);
  506.             p0 = L0;
  507.         } else
  508.             ins_after(L1->deg - L2->deg, a, p0, 1);
  509.  
  510.         if (L1->prev != nullptr) {
  511.             node *p = L1/*->prev*/;
  512.             while (p != nullptr) {
  513.                 add_node(head, p->koef, p->deg);
  514.                 p = p->prev;
  515.             }
  516.             delete p;
  517.         }
  518.  
  519.         if (L2->prev != nullptr) {
  520.             node *p = L2/*->prev*/;
  521.             while (p != nullptr) {
  522.                 add_node(head, -p->koef * a, p->deg + L1->deg - L2->deg);
  523.                 p = p->prev;
  524.             }
  525.             delete p;
  526.         }
  527.         L = head->prev;
  528.  
  529.         division(L, L2, L0, p0);
  530.         return L0;
  531.     } else
  532.         return L0;
  533. }
  534.  
  535. node* multiplication(node* L1,node* L2) {
  536.     node *head = new node;
  537.  
  538.     if (L1->prev != nullptr) {
  539.         node *p = L1->prev;
  540.         while (p != nullptr) {
  541.             node *p1 = L2->prev;
  542.             while (p1 != nullptr) {
  543.                 add_node(head, p->koef * p1->koef, p1->deg + p->deg);
  544.                 p1 = p1->prev;
  545.             }
  546.             delete p1;
  547.             p = p->prev;
  548.         }
  549.         delete p;
  550.     }
  551.  
  552.     return head->prev;
  553. }
  554.  
  555. void add_to_poly(polynomial* &E, node* L)
  556. {
  557.     polynomial* h=new polynomial;
  558.     node* head = new node;
  559.     h->poly=head;
  560.     head->prev=L;
  561.     L->next=head;
  562.     E->next=h;
  563.     E=h;
  564.     h->next= nullptr;
  565. }
  566.  
  567. void print_list(polynomial* HEAD){
  568.     polynomial* h=HEAD->next;
  569.     while(h!=nullptr) {
  570.         print_node(h->poly);
  571.         h = h->next;
  572.     }
  573. }
  574.  
  575. void del_node(node* L) {
  576.     node *p = L, *p1 = L->prev;
  577.     while (p1 != nullptr) {
  578.         delete (p);
  579.         p = p1;
  580.         p1 = p1->prev;
  581.     }
  582.     delete (p);
  583. }
  584.  
  585. bool del_H_node(polynomial* HEAD,polynomial*& E, int n) {
  586.     polynomial *h = HEAD, *h1;
  587.     if (HEAD->next != nullptr)
  588.         h1 = HEAD->next;
  589.     for (int i = 0; i < n - 1&&h1->next!= nullptr; i++) {
  590.         h = h1;
  591.         h1 = h1->next;
  592.     }
  593.     if(h1->next!= nullptr) {
  594.         if(h1==E)
  595.             E=h;
  596.         h->next = h1->next;
  597.         del_node(h1->poly->prev);
  598.         return 1;
  599.     }
  600.     return 0;
  601. }
  602.  
  603.  
  604. //#########################
  605.  
  606. void funcs_display(int x) {
  607.     if (x == 5) {
  608.         cout << "Enter number of function: ";
  609.         return ;
  610.     }
  611.     system("cls");
  612.     cout << endl << "LIST:" << endl;
  613.     if (HEAD->next != nullptr) print_list(HEAD);
  614.     cout << endl;
  615.  
  616.     cout << "Functions: " << endl;
  617.     cout << "1: Enter polynomial" << endl;
  618.     cout << "2: Delete polynomial" << endl;
  619.     cout << "3: Integer roots of polynomial" << endl;
  620.     cout << "4: Take the derivative" << endl;
  621.     cout << "5: Summarize polynomial_1 and polynomial_2" << endl;
  622.     cout << "6: Multiply polynomial_1 and polynomial_2" << endl;
  623.     cout << "7: Divide polynomial_1 and polynomial_2" << endl;
  624.     cout << endl;
  625.     //cout << "Enter number of function: ";
  626.     if (x == 1) cout << "Enter number of function: ";
  627.     else if (x == 2) {
  628.         cout << "ERROR IN INPUT" << endl;
  629.         cout << "Enter number of function: ";
  630.     }
  631.     else if (x == 3) {
  632.         cout << "POLYNOMIAL ADDED" << endl;
  633.         cout << "Enter number of function: ";
  634.     }
  635.     else if (x == 4) {
  636.         cout << "POLYNOMIAL DELETED" << endl;
  637.         cout << "Enter number of function: ";
  638.     }
  639. }
  640.  
  641. void start(int flag) {
  642.     funcs_display(flag);
  643.  
  644.     string type;
  645.     getline(cin, type);
  646.     node *head;
  647.  
  648.  
  649.     if (type == "1") {
  650.         int x = 3;
  651.         cout << endl << "Enter polynomial: ";
  652.         string main_str;
  653.         while(getline(cin, main_str)) {
  654.             if (main_str == "") continue;
  655.             head = pol(main_str);
  656.             break;
  657.         }
  658.  
  659.         if (head == nullptr) {
  660.             x = 2;
  661.             cout << "ERROR IN INPUT" << endl;
  662.         }
  663.         else add_to_poly(END, head->prev);
  664.         start(x);
  665.     }
  666.     else if (type == "2") {
  667.         cout << endl << "Enter number of the polynomial: ";
  668.         string NUMV;
  669.         getline(cin, NUMV);
  670.  
  671.         bool YES_NO = del_H_node(HEAD, END, stoi(NUMV));
  672.         if (YES_NO) start(4);
  673.         else start(2);
  674.     }
  675.     else if (type == "3") {
  676.         cout << endl << "Enter number of the polynomial: ";
  677.         string NUMV;
  678.         getline(cin, NUMV);
  679.  
  680.         roots(poly_num(HEAD, stoi(NUMV))->prev);
  681.         start(5);
  682.     }
  683.     else if (type == "4") {
  684.  
  685.     }
  686.     else if (type == "5") {
  687.  
  688.     }
  689.     else if (type == "6") {
  690.  
  691.     }
  692.     else if (type == "7") {
  693.  
  694.     }
  695.     else if (type == "8") {
  696.  
  697.     }
  698.     //start(1);
  699. }
  700.  
  701.  
  702. signed main() {
  703.     start(1);
  704.  
  705.     return 0;
  706. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement