Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdlib>
- #include<cstring>
- #include <iostream>
- #include <string>
- using namespace std;
- struct polynom
- {
- int deg;
- int coef;
- polynom* next;
- };
- typedef polynom* Pointer;
- Pointer Head = NULL;
- int M = 0;
- void input()
- {
- Pointer NewEl = NULL;
- int i, k = 0;
- char pol[100], p[100];
- cout << "vvedite polynom: ";
- cin >> pol;
- int N = strlen(pol);
- for (i = 0; i <N; i++)
- {
- if ((i != N - 1) && (pol[i] != 'x') && (pol[i] != '^') && (pol[i] != '+') && (pol[i] != '-'))
- {
- p[k] = pol[i];
- k++;
- }
- else if (pol[i] == 'x')
- {
- p[k] = '\0';
- k = 0;
- NewEl = new polynom;
- NewEl->coef = atof(p);
- NewEl->next = Head;
- Head = NewEl;
- M++;
- }
- else if ((i == N - 1) || (pol[i] == '+') || (pol[i] == '-'))
- {
- if (i == N - 1)
- {
- p[k] = pol[i];
- k++;
- }
- p[k] = '\0';
- k = 1;
- if (NewEl)
- NewEl->deg = atof(p);
- p[0] = pol[i];
- }
- }
- }
- void Gornir(Pointer q)
- {
- string s = to_string(q->coef);
- if (q->next != NULL) {
- if (q->next->deg + 1 == q->deg) {
- q = q->next;
- s = "("+ s + "x + " + to_string(q->coef) + ")x";
- }
- q = q->next;
- while (q->next != NULL) {
- for (int i = q->next->deg; i <=q->deg; i++) {
- if (q->deg == i) {
- s = "(" + s + " + " + to_string(q->coef) + ")x";
- q = q->next;
- continue;
- }
- s = "(" + s + ")x";
- }
- q = q->next;
- }
- if (q->deg == 0) {
- s = s + " + " + to_string(q->coef);
- }
- }
- cout << s;
- }
- int main()
- {
- input();
- cout << "\nPolynom: ";
- Gornir(Head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement