Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <clocale>
- using namespace std;
- class TSummand
- {
- private:
- float _a;
- int _pow;
- TSummand* next;
- public:
- TSummand()
- {
- _a = 0;
- _pow = 0;
- next = NULL;
- }
- TSummand(int a, int pow)
- {
- _a = a;
- _pow = pow;
- next = NULL;
- }
- int isEmpty()
- {
- return (next==NULL)&&(_pow==0);
- }
- int oneValue(int xx)
- {
- return pow(xx,_pow) * _a;
- }
- void addElem(int addA, int addPow)
- {
- if(isEmpty())
- {
- _a = addA;
- _pow = addPow;
- }
- else
- {
- TSummand* t;
- t = this;
- int tA, tPow;
- tA = _a;
- tPow = _pow;
- _a = addA;
- _pow = addPow;
- TSummand* ad = new TSummand(tA, tPow);
- while(t->next!=NULL)
- {
- t = t->next;
- ad->_a = t->_a;
- ad->_pow = t->_pow;
- t->_a = tA;
- t->_pow = tPow;
- tA = ad->_a;
- tPow = ad->_pow;
- }
- t->next = ad;
- }
- }
- void printSummand()
- {
- TSummand* t;
- t = this;
- while(t!=NULL)
- {
- cout<<"Коэффициент "<<t->_a<<" степень "<<t->_pow<<"| ";
- t = t->next;
- }
- }
- int summandValue(float xx)
- {
- if(isEmpty())
- return 0;
- else
- {
- TSummand* t;
- float result = 0;
- t = this;
- while(t!=NULL)
- {
- result += t->oneValue(xx);
- t = t->next;
- }
- return result;
- }
- }
- };
- int main()
- {
- setlocale(0, "RUSSIAN");
- int n;
- cout << "Введите степень многочлена: ";
- cin >> n;
- TSummand *Summand = new TSummand;
- cout << "Введите коэффициенты при икс в степени" << endl;
- for (int i = n; i >= 0; i--)
- {
- cout << i << ": ";
- int a;
- cin >> a;
- if (a != 0)
- Summand->addElem(a, i);
- }
- cout << "Введите X, в котором необходимо вычислить многочлен: ";
- float x;
- cin >> x;
- cout << "Результат: " << Summand->summandValue(x) << endl;
- Summand->printSummand();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement