Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. #include "cstdio"
  4. #include "stdlib.h"
  5. #include "cmath"
  6. #include "iomanip"
  7. #include "string"
  8. #include "math.h"
  9.  
  10. using namespace std;
  11. class Polynomial{
  12. private:
  13.     int* koefs;
  14.     int Index;
  15. public:
  16.  
  17.     Polynomial(){
  18.  
  19.     }
  20.     Polynomial(int index){
  21.         Index = index;
  22.         koefs = new int[Index];
  23.     }
  24.     void Init(){
  25.         cout << "Vvedite kollichestvo koeff";
  26.         cin >> Index;
  27.         koefs = new int[Index];
  28.         for (int i = 0; i < Index; i++){
  29.             cout << "Vvedite koeff";
  30.             cin >> koefs[i];
  31.         }
  32.     }
  33.  
  34.     Polynomial Polynomial::operator +(Polynomial other) //что должен выполнить оператор +
  35.     {
  36.         int maxIndex = Index > other.Index ? Index : other.Index;
  37.         Polynomial result(maxIndex);
  38.         for (int i = 0; i < maxIndex; i++){
  39.             result.koefs[i] = ((i < Index) ? koefs[i] : 0) + ((i < other.Index) ? other.koefs[i] : 0);
  40.         }
  41.         return result;
  42.     }
  43.  
  44.     int GetResult(int x){
  45.         int result = 0;
  46.         for (int i = 0; i < Index; i++){
  47.             result += koefs[i] * pow(x, i);
  48.         }
  49.         return result;
  50.     }
  51. };
  52.  
  53.  
  54. void main()
  55. {
  56.     cout << "Create polynomial" << endl;
  57.     Polynomial polynomialFirst;
  58.     polynomialFirst.Init();
  59.     Polynomial polynomialSecond;
  60.     polynomialSecond.Init();
  61.     Polynomial resultPoly;
  62.     resultPoly = polynomialFirst + polynomialSecond;
  63.     cout << "Vvedite x" << endl;
  64.     int x;
  65.     cin >> x;
  66.     int result = resultPoly.GetResult(x);
  67.     cout << "Result = " << result << endl;
  68.     system("pause");
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement