Calculation Of Polynomial(多项式的运算) C++

本文实现了一个多项式的类,类中重载了多项式的加法、减法、乘法,并且对>> 和 << 进行了重载。

其中多项式的输入(输出)格式为: K  N1 aN1 N2 aN2 ... NK aNK

其中K表示多项式的系数,NK表示第K项的系数aNk相应的系数,并且多项式的系数递减的序列。

  1 //polynomial.h
  2 #include <iostream>
  3 #include <vector>
  4 #include <iomanip>
  5 
  6 class polynomial
  7 {
  8 private:
  9     std::vector<double> coefficient;
 10     std::vector<int> radix;
 11 public:
 12     polynomial(){}
 13     polynomial(const polynomial &poly){ *this = poly; }
 14     ~polynomial(){}
 15     friend polynomial operator+(const polynomial &poly1, const polynomial &poly2);
 16     friend polynomial operator-(const polynomial &poly1, const polynomial &poly2);
 17     polynomial& operator+=(const polynomial &poly){ *this = *this + poly; return *this; }
 18     polynomial& operator-=(const polynomial &poly){ *this = *this - poly; return *this; }
 19     friend polynomial operator*(const polynomial &poly1, const polynomial &poly2);
 20     friend std::istream& operator>>(std::istream &is, polynomial &poly);
 21     friend std::ostream& operator<<(std::ostream &os, const polynomial &poly);
 22 };
 23 
 24 polynomial operator+(const polynomial &poly1, const polynomial &poly2)
 25 {
 26     polynomial polysum;
 27 
 28     int i = 0, j = 0;
 29     while (i<poly1.radix.size() || j<poly2.radix.size())
 30     {
 31         //only poly1
 32         if (i<poly1.radix.size() && j == poly2.radix.size())
 33         {
 34             polysum.radix.push_back(poly1.radix[i]);
 35             polysum.coefficient.push_back(poly1.coefficient[i]);
 36             i++;
 37         }
 38         //only poly2
 39         else if (j<poly2.radix.size() && i == poly1.radix.size())
 40         {
 41             polysum.radix.push_back(poly2.radix[j]);
 42             polysum.coefficient.push_back(poly2.coefficient[j]);
 43             j++;
 44         }
 45         //the radix of poly1 greater than poly2
 46         else if (poly1.radix[i] > poly2.radix[j])
 47         {
 48             polysum.radix.push_back(poly1.radix[i]);
 49             polysum.coefficient.push_back(poly1.coefficient[i]);
 50             i++;
 51         }
 52         //the radix of poly1 smaller than poly2
 53         else if (poly1.radix[i] < poly2.radix[j])
 54         {
 55             polysum.radix.push_back(poly2.radix[j]);
 56             polysum.coefficient.push_back(poly2.coefficient[j]);
 57             j++;
 58         }
 59         //the radix of poly1 equal to poly2
 60         else
 61         {
 62             if (poly1.coefficient[i] + poly2.coefficient[j] != 0)
 63             {
 64                 polysum.radix.push_back(poly1.radix[i]);
 65                 polysum.coefficient.push_back(poly1.coefficient[i] + poly2.coefficient[j]);
 66             }
 67             i++;
 68             j++;
 69         }
 70     }
 71 
 72     return polysum;
 73 }
 74 
 75 polynomial operator-(const polynomial &poly1, const polynomial &poly2)
 76 {
 77     polynomial negativePoly;
 78 
 79     negativePoly = poly2;
 80     for (int i = 0; i < negativePoly.coefficient.size(); i++)
 81         negativePoly.coefficient[i] = -negativePoly.coefficient[i];
 82 
 83     return poly1 + negativePoly;
 84 }
 85 
 86 polynomial operator*(const polynomial &poly1, const polynomial &poly2)
 87 {
 88     polynomial mul;
 89 
 90     int i = 0;
 91     while (i < poly2.coefficient.size())
 92     {
 93         polynomial part = poly1;
 94         double coefficient = poly2.coefficient[i];
 95         int radix = poly2.radix[i];
 96 
 97         for (int j = 0; j < part.coefficient.size(); j++)
 98         {
 99             part.radix[j] += radix;
100             part.coefficient[j] *= coefficient;
101         }
102         mul += part;
103         i++;
104     }
105 
106     return mul;
107 }
108 
109 std::istream& operator>>(std::istream &is, polynomial &poly)
110 {
111     int k;
112     is >> k;
113     for (int i = 0; i<k; i++)
114     {
115         double tmpcoe;
116         int tmprad;
117         is >> tmprad >> tmpcoe;
118         poly.radix.push_back(tmprad);
119         poly.coefficient.push_back(tmpcoe);
120     }
121 
122     return is;
123 }
124 
125 std::ostream& operator<<(std::ostream &os, const polynomial &poly)
126 {
127     os << poly.radix.size();
128     if (poly.radix.size() != 0)
129         std::cout << " ";
130     for (int i = 0; i<poly.radix.size(); i++)
131     {
132         os << poly.radix[i] << " " << std::fixed << std::setprecision(1) << poly.coefficient[i];
133         if (i != poly.radix.size() - 1)
134             os << " ";
135     }
136 
137     return os;
138 }

 

posted @ 2015-08-03 20:53  JackWang822  阅读(1053)  评论(0编辑  收藏  举报