Advertisement
Proff_Ust

Лаба для Ольги(первая)

Dec 14th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <clocale>
  4. using namespace std;
  5.  
  6. class TSummand
  7. {
  8. private:
  9.     float _a;
  10.     int _pow;
  11.     TSummand* next;
  12. public:
  13.     TSummand()
  14.     {
  15.         _a = 0;
  16.         _pow = 0;
  17.         next = NULL;
  18.     }
  19.  
  20.     TSummand(int a, int pow)
  21.     {
  22.         _a = a;
  23.         _pow = pow;
  24.         next = NULL;
  25.     }
  26.  
  27.     int isEmpty()
  28.     {
  29.         return (next==NULL)&&(_pow==0);
  30.     }
  31.  
  32.     int oneValue(int xx)
  33.     {
  34.         return pow(xx,_pow) * _a;
  35.     }
  36.  
  37.     void addElem(int addA, int addPow)
  38.     {
  39.         if(isEmpty())
  40.         {
  41.             _a = addA;
  42.             _pow = addPow;
  43.         }
  44.         else
  45.         {
  46.             TSummand* t;
  47.             t = this;
  48.             int tA, tPow;
  49.             tA = _a;
  50.             tPow = _pow;
  51.             _a = addA;
  52.             _pow = addPow;
  53.             TSummand* ad = new TSummand(tA, tPow);
  54.             while(t->next!=NULL)
  55.             {
  56.                 t        = t->next;
  57.                 ad->_a   = t->_a;
  58.                 ad->_pow = t->_pow;
  59.                 t->_a    = tA;
  60.                 t->_pow  = tPow;
  61.                 tA       = ad->_a;
  62.                 tPow     = ad->_pow;
  63.             }
  64.             t->next = ad;
  65.         }
  66.     }
  67.  
  68.     void printSummand()
  69.     {
  70.         TSummand* t;
  71.         t = this;
  72.         while(t!=NULL)
  73.         {
  74.             cout<<"Коэффициент "<<t->_a<<" степень "<<t->_pow<<"| ";
  75.             t = t->next;
  76.         }
  77.     }
  78.  
  79.     int summandValue(float xx)
  80.     {
  81.         if(isEmpty())
  82.             return 0;
  83.         else
  84.         {
  85.             TSummand* t;
  86.             float result = 0;
  87.             t = this;
  88.             while(t!=NULL)
  89.             {
  90.                 result += t->oneValue(xx);
  91.                 t = t->next;
  92.             }
  93.             return result;
  94.         }
  95.     }
  96. };
  97. int main()
  98. {
  99.     setlocale(0, "RUSSIAN");
  100.     int n;
  101.     cout << "Введите степень многочлена: ";
  102.     cin >> n;
  103.     TSummand *Summand = new TSummand;
  104.     cout << "Введите коэффициенты при икс в степени" << endl;
  105.     for (int i = n; i >= 0; i--)
  106.     {
  107.         cout << i << ": ";
  108.         int a;
  109.         cin >> a;
  110.         if (a != 0)
  111.             Summand->addElem(a, i);
  112.     }
  113.     cout << "Введите X, в котором необходимо вычислить многочлен: ";
  114.     float x;
  115.     cin >> x;
  116.     cout << "Результат: " << Summand->summandValue(x) << endl;
  117.     Summand->printSummand();
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement