Guest User

Untitled

a guest
Jul 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <windows.h>
  5. using namespace std;
  6. class mn
  7. {
  8.         private:
  9.                 int n;
  10.                 double *x;
  11.         public:
  12.                 mn();
  13.                 ~mn();
  14.                 mn(int n,double *x);
  15.                 double cs();
  16.  
  17.                 mn &operator=(const mn &Object); // Перегрузка =
  18.                 mn operator+(mn &); // Сложение
  19.                 mn operator-(mn &); // Вычитание
  20.                 mn operator*(mn &); // Перемножение
  21.  
  22.                 // Перегрузка оператора << для вывода
  23.                 friend ostream &operator<<(ostream &, mn &);
  24.                 // Перегрузка оператора >> для ввода матрицы
  25.                 friend istream &operator>>(istream &, mn &);
  26.  
  27. };
  28.  
  29. mn::mn(int nn,double *xx)
  30. {
  31.         unsigned char i;
  32.         n=nn;
  33.         x=new double[n+1];
  34.         for(i=0;i<=n;x[i++]=xx[i]);
  35. }
  36.  
  37. mn::mn()
  38. {
  39.         double x[1]={0};
  40.         mn::mn(0,x);
  41. }
  42.  
  43. mn::~mn()
  44. {
  45.         delete []x;
  46. }
  47.  
  48. double mn::cs()
  49. {
  50.         return x[0];
  51. }
  52.  
  53.  
  54. // Перегрузка оператора =
  55. mn& mn::operator=(const mn &Object)
  56. {
  57.         n = Object.n;
  58.                   delete []x;
  59.         x=new double[n+1];
  60.         for(int i=0;i<=n;)x[i++]=Object.x[i];
  61.         return *this;
  62. }
  63.  
  64. // Перегрузка оператора +
  65. mn mn::operator+(mn &fp1)
  66. {
  67.         mn ret;
  68.         ret=(n>fp1.n)?*this:fp1;
  69.         int minind=(n>fp1.n)?fp1.n:n;
  70.  
  71.         for(int i=0;i<=minind;ret.x[i++]+=x[i]);
  72.  
  73.         return ret;
  74. }
  75.  
  76. // Перегрузка оператора -
  77. mn mn::operator-(mn &fp1)
  78. {
  79.         mn ret;
  80.         int i;
  81.         ret=(n>fp1.n)?*this:fp1;
  82.         if(fp1.n>n) for(i=n;i<=ret.n;ret.x[i++]=-ret.x[i]);
  83.         int minind=(n>fp1.n)?fp1.n:n;
  84.  
  85.         for(i=0;i<=minind;ret.x[i++]=x[i]-fp1.x[i]);
  86.         return ret;
  87.  
  88. }
  89.  
  90. // Перегрузка оператора *
  91. mn mn::operator*(mn &fp1)
  92. {
  93.         int newindex=fp1.n+n;
  94.         double *empty=new double[newindex+1];
  95.         for(int i=0;i<=newindex;empty[i++]=0);
  96.  
  97.         mn ret(newindex,empty);
  98.  
  99.         for(int i=0;i<=fp1.n;i+=1)
  100.                 for(int j=0;j<=n;j+=1) ret.x[i+j]+=fp1.x[i]*x[j];
  101.  
  102.         return ret;
  103.  
  104. }
  105.  
  106. // Перегрузка оператора >>
  107. istream &operator>>(istream &fi, mn &fp)
  108. {
  109.         cout<<"n=";
  110.         fi >> fp.n;
  111.  
  112.         delete []fp.x;
  113.         fp.x=new double[fp.n+1];
  114.         for(int i=0;i<=fp.n;)
  115.         {
  116.                 cout<<"x["<<i<<"]=";
  117.                 fi >> fp.x[i++];
  118.  
  119.         }
  120.  
  121.         return fi;
  122. }
  123.  
  124. // Перегрузка оператора <<
  125. ostream &operator<<(ostream &fo, mn &fp)
  126. {
  127.  
  128.         for(int i=0;i<=fp.n;i+=1) fo <<"x["<<i<<"]="<<int(fp.x[i])<<endl;
  129.         return fo;
  130. }
  131.  
  132. int main()
  133. {
  134.     SetConsoleCP(1251);
  135.     SetConsoleOutputCP(1251);
  136.         double x[]={6,3,8};
  137.         double x2[]={3,1,4};
  138.         mn m1(2,x);
  139.         cout<<"Первый многочлен:"<<endl;
  140.         cout<<m1<<endl;
  141.         cout<<"Введите второй многочлен:"<<endl;
  142.         mn m2(2, x2);
  143.         //cin>>m2;
  144.         cout<<m2<<endl;
  145.  
  146.         //cout<<endl<<"Сумма m1 и m2:"<<endl<<m1+m2<<endl;
  147.  
  148.  
  149.         getch();
  150.         //cout<<"Разность m1 и m2:"<<endl<<m1-m2<<endl;
  151.         getch();
  152.         mn m3;
  153.         m3=m1*m2;
  154.         cout<<"Произведение m1 и m2:"<<endl<<m3<<endl;
  155.         getch();
  156.         //cout<<"Свободный коэффициент произведения m1 и m2: "<<m3.cs()<<endl;
  157.         //m1.~mn();
  158.        // m2.~mn();
  159.        // m3.~mn();
  160.         return 0;
  161. }
Add Comment
Please, Sign In to add comment