Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct node
- {
- int koef;
- int power;
- node* next;
- };
- bool empty(node* top)
- {
- return top == NULL;
- }
- node* find_end(node *top)
- {
- node *p = top;
- while (p->next)
- p = p->next;
- return p;
- }
- void pop(node* &top)
- {
- node* p;
- if (!empty(top))
- {
- p = top->next;
- delete top;
- top = p;
- }
- }
- void del_info(node *&top, int x)
- {
- node *p = top;
- if (top->koef == x)
- top = p->next;
- else
- {
- while ((p->next)->koef != x)
- p = p->next;
- p->next = (p->next)->next;
- }
- }
- void push(node* &top, int k, int po)
- {
- node* p=new node;
- p->koef = k;
- p->power = po;
- p->next = top;
- top = p;
- }
- node* create(ifstream&f, int n)
- {
- int i;
- node *top = NULL;
- int k, p;
- for (i = 0; i < n; i++)
- {
- f >> k;
- f >> p;
- push(top, k, p);
- }
- return top;
- }
- node* previous(node* top, node* p)
- {
- node* pr, *q;
- q = top;
- pr = NULL;
- while (q != p && q)
- {
- pr = q;
- q = q->next;
- }
- if (q == p) return pr;
- return NULL;
- }
- node* previous_info(node *top, int x)
- {
- node *p = top;
- if (top->koef == x)
- return NULL;
- else
- {
- while ((p->next)->koef != x && p)
- p = p->next;
- return p;
- }
- }
- void sort(node* top)
- {
- int koef, power;
- node* p = top, *q;
- while (p&&p->next)
- {
- q = p->next;
- while (q)
- {
- if (p->power < q->power)
- {
- power = p->power;
- p->power = q->power;
- q->power = power;
- koef = p->koef;
- p->koef = q->koef;
- q->koef = koef;
- }
- q = q->next;
- }
- p = p->next;
- }
- }
- node** create_mas(ifstream&f,int n)
- {
- node **mas = new node*[n];
- for (int i = 0; i < n; i++)
- {
- int N;
- f >> N;
- mas[i]= create(f, N);
- }
- return mas;
- }
- bool prime(int n)
- {
- int i;
- bool k = true;
- for (i = 2; i <= int(pow(n, 1.0 / 2)); i++)
- if (n%i == 0)
- return false;
- if (k == true)
- return 1;
- else
- return 0;
- }
- bool prime_polinom(node *top)
- {
- bool t = 1;
- node *p = top;
- while (p && t)
- {
- if (!prime(p->power))
- t = 0;
- p = p->next;
- }
- return t;
- }
- bool func9(node **mas, int n)
- {
- bool t = 0;
- for (int i = 0; i < n && !t; i++)
- if (prime_polinom(mas[i]))
- t = 1;
- return t;
- }
- bool otr(node* top)
- {
- node *p = top;
- bool t = 1;
- while (p && t)
- {
- if (p->koef > 0)
- t = 0;
- p = p->next;
- }
- return t;
- }
- bool func11(node **mas, int n)
- {
- bool t = 0;
- for (int i = 0; i < n && !t; i++)
- if (otr(mas[i]))
- t = 1;
- return t;
- }
- node* copy(node* top)
- {
- node *top2 = NULL;
- while (top)
- {
- push(top2, top->koef, top->power);
- top = top->next;
- }
- return top2;
- }
- void collecting_terms(node* &top)
- {
- sort(top);
- node* p, *q, *pr;
- p = top;
- q = p->next;
- while (q)
- {
- if (p->power == q->power)
- {
- q->koef = q->koef + p->koef;
- if (p == top)
- {
- pop(top);
- p = top;
- }
- else
- {
- pr = previous(top, p);
- pr->next = q;
- delete p;
- p = q;
- }
- q = q->next;
- }
- else
- {
- p = q;
- q = q->next;
- }
- }
- }
- node* mult(node* top1, node* top2)
- {
- node *top3 = NULL;
- while (top1)
- {
- node* p = top2;
- while (p)
- {
- push(top3, top1->koef*p->koef,
- top1->power + p->power);
- p = p->next;
- }
- top1 = top1->next;
- }
- collecting_terms(top3);
- return top3;
- }
- void mult_k(node* top, int k)
- {
- node* p;
- p = top;
- while (p)
- {
- p->koef = k * p->koef;
- p = p->next;
- }
- }
- node* sum(node* top1, node* top2)
- {
- node *top3,*top4;
- top3 = copy(top1);
- top4 = copy(top2);
- find_end(top3)->next = top4;
- collecting_terms(top3);
- return top3;
- }
- node* sub(node* top1, node* top2)
- {
- node *top3, *top4;
- top3 = copy(top1);
- top4 = copy(top2);
- mult_k(top4, -1);
- find_end(top3)->next = top4;
- collecting_terms(top3);
- return top3;
- }
- void show(node* top)
- {
- sort(top);
- node* p;
- p = top;
- while (p->next)
- {
- cout << p->koef << "*x^" << p->power << " + ";
- p = p->next;
- }
- cout << p->koef << "*x^" << p->power << endl;
- }
- void show_mas(node **mas,int n)
- {
- for (int i = 0; i < n; i++)
- {
- collecting_terms(mas[i]);
- show(mas[i]);
- }
- cout << endl;
- }
- bool otr_3(node *top)
- {
- node *p = top;
- int k = 0;
- bool t = 0;
- while (p &&k<4)
- {
- if (p->koef < 0)
- k++;
- p = p->next;
- }
- if (k > 3)
- t = 1;
- return t;
- }
- int main()
- {
- ifstream in("input.txt");
- int n;
- in >> n;
- node **mas;
- mas=create_mas(in, n);
- node *n1, *n2=NULL;
- if (func11(mas, n) || func9(mas, n))
- {
- int i = 0;
- while (!(otr(mas[i]) || prime_polinom(mas[i])))
- i++;
- n1 = mas[i];
- for (i; i < n; i++)
- {
- if (otr(mas[i]) || prime_polinom(mas[i]))
- n1 = mult(n1, mas[i]);
- }
- }
- else
- n1 = mult(mas[0], mas[1]);
- if (func11(mas, n))
- {
- for (int i = 0; i < n; i++)
- {
- if (otr_3(mas[i]))
- n2 = sum(n1, mas[i]);
- }
- }
- else
- n2 = sub(mas[0], mas[1]);
- show(n1);
- show(n2);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement