创建一个类,重载运算符实现多项式的加,减,乘运算

唉,我太弱了,这么个简单的东西,还是用数组写的,却因为各种错误,弄了一个晚上,C++还得好好学啊,各种语法,要弄清楚。

  1 #include <iostream>
  2 #include <cstdlib>
  3 #include <cstdio>
  4 #include <iomanip>
  5 #include <cstring>
  6 
  7 using namespace std;
  8 
  9 class Polynomial
 10 {
 11 public:
 12     Polynomial(int  = 0);
 13     friend istream & operator >> (istream & input, Polynomial & pl);
 14     friend ostream & operator << (ostream & output, Polynomial & pl);
 15     Polynomial & operator += (Polynomial & add);
 16     Polynomial & operator -= (Polynomial & add);
 17     Polynomial & operator - (Polynomial & add);
 18     Polynomial & operator * (Polynomial &);
 19     Polynomial & operator + (Polynomial & add);
 20     Polynomial & operator *= (Polynomial &);
 21     Polynomial & operator = (Polynomial & right);
 22     Polynomial & test(Polynomial & right);
 23 private:
 24     int *a;
 25     int size, num;
 26 };
 27 
 28 Polynomial & Polynomial::operator *= (Polynomial & mu)
 29 {
 30     Polynomial b, c;
 31     c = *this; b = *this;
 32     for (int j=0,r=0; j<b.num;++j,r+=2)
 33     {
 34         (*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
 35     }
 36     for (int i=1,k=2;i<mu.num;++i,k+=2)
 37     {
 38         for (int j=0,r=0; j<b.num;++j,r+=2)
 39         {
 40             b.a[r]+=mu.a[k];
 41             b.a[r+1]*=mu.a[k+1];
 42         }
 43         *this += b;
 44         b = c;
 45     }
 46     return *this;
 47 }
 48 Polynomial & Polynomial::operator * (Polynomial & mu)
 49 {
 50     Polynomial b, c;
 51     c = *this; b = *this;
 52     for (int j=0,r=0; j<b.num;++j,r+=2)
 53     {
 54         (*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
 55     }
 56     for (int i=1,k=2;i<mu.num;++i,k+=2)
 57     {
 58         for (int j=0,r=0; j<b.num;++j,r+=2)
 59         {
 60             b.a[r]+=mu.a[k];
 61             b.a[r+1]*=mu.a[k+1];
 62         }
 63         *this += b;
 64         b = c;
 65     }
 66     return *this;
 67 }
 68 
 69 Polynomial & Polynomial::operator = (Polynomial & right)
 70 {
 71     if (&right != this)
 72     {
 73         delete [] a;
 74         size = right.size;
 75         num = right.num;
 76         a = new int[size];
 77         for (int i = 0; i < size; ++i)
 78             a[i]=right.a[i];
 79     }
 80     else
 81         cout<<"Attempted assignment of a polynomial to itself" << endl;
 82     return *this;
 83 }
 84 
 85 Polynomial & Polynomial::operator -= (Polynomial & add)
 86 {
 87     int number = add.num, mrk = 0;
 88     int cosize = add.size; int * q = new int[cosize];
 89     for (int d=0;d<cosize;++d) q[d]=add.a[d];
 90     for (int i = 0, s = 0; i < number; ++i, s+=2)
 91     {
 92         mrk = 0;
 93         for (int j = 0, k = 0; j < num; ++j,k+=2)
 94         {
 95             if (q[s]==a[k])
 96             {
 97                 mrk = 1;
 98                 a[k+1]-=q[s+1];
 99                 q[s+1]=0;
100             }
101         }
102         if (!mrk)
103         {
104             int *p = new int[size+2];
105             for (int d=0;d<size;d++)
106                 p[d]=a[d];
107             p[size] = q[s];
108             p[size+1] = -q[s+1];
109             q[s+1]=0;
110             a = p;
111             num++;
112             size+=2;
113         }
114     }
115     return *this;
116 }
117 Polynomial & Polynomial::operator - (Polynomial & add)
118 {
119     int number = add.num, mrk = 0;
120     int cosize = add.size; int * q = new int[cosize];
121     for (int d=0;d<cosize;++d) q[d]=add.a[d];
122     for (int i = 0, s = 0; i < number; ++i, s+=2)
123     {
124         mrk = 0;
125         for (int j = 0, k = 0; j < num; ++j,k+=2)
126         {
127             if (q[s]==a[k])
128             {
129                 mrk = 1;
130                 a[k+1]-=q[s+1];
131                 q[s+1]=0;
132             }
133         }
134         if (!mrk)
135         {
136             int *p = new int[size+2];
137             for (int d=0;d<size;d++)
138                 p[d]=a[d];
139             p[size] = q[s];
140             p[size+1] = -q[s+1];
141             q[s+1]=0;
142             a = p;
143             num++;
144             size+=2;
145         }
146     }
147     return *this;
148 }
149 
150 Polynomial & Polynomial::operator += (Polynomial & add)
151 {
152     int number = add.num, mrk = 0;
153     int cosize = add.size; int * q = new int[cosize];
154     for (int d=0;d<cosize;++d) q[d]=add.a[d];
155     for (int i = 0, s = 0; i < number; ++i, s+=2)
156     {
157         mrk = 0;
158         for (int j = 0, k = 0; j < num; ++j,k+=2)
159         {
160             if (q[s]==a[k])
161             {
162                 mrk = 1;
163                 a[k+1]+=q[s+1];
164                 q[s+1]=0;
165             }
166         }
167         if (!mrk)
168         {
169             int *p = new int[size+2];
170             for (int d=0;d<size;d++)
171                 p[d]=a[d];
172             p[size] = q[s];
173             p[size+1] = q[s+1];
174             q[s+1]=0;
175             a = p;
176             num++;
177             size+=2;
178         }
179     }
180     return *this;
181 }
182 
183 Polynomial & Polynomial::operator + (Polynomial & add)
184 {
185     int number = add.num, mrk = 0;
186     Polynomial & mi = *this;
187     int cosize = add.size; int * q = new int[cosize];
188     for (int d=0;d<cosize;++d) q[d]=add.a[d];
189     for (int i = 0, s = 0; i < number; ++i, s+=2)
190     {
191         mrk = 0;
192         for (int j = 0, k = 0; j < mi.num; ++j,k+=2)
193         {
194             if (q[s]==mi.a[k])
195             {
196                 mrk = 1;
197                 mi.a[k+1]+=q[s+1];
198                 q[s+1]=0;
199             }
200         }
201         if (!mrk)
202         {
203             int *p = new int[mi.size+2];
204             for (int d=0;d<mi.size;d++)
205                 p[d]=mi.a[d];
206             p[mi.size] = q[s];
207             p[mi.size+1] = q[s+1];
208             q[s+1]=0;
209             mi.a = p;
210             mi.num++;
211             mi.size+=2;
212         }
213     }
214     return mi;
215 }
216 
217 ostream & operator << (ostream & output, Polynomial & pl)
218 {
219     for (int i = 0, j = 0; i < pl.num; ++i)
220     {
221         if (pl.a[j+1]==0)
222         {
223             output << "0 0 "; j+=2; continue; 
224         }
225         output << pl.a[j+1] << " " << pl.a[j] << " ";
226         j+=2;
227     }
228     output << endl;
229     return output;
230 }
231 
232 istream & operator >> (istream & input, Polynomial & pl)
233 {
234     cout << "input the number of Polynomial terms: ";
235     input >> pl.num;
236     pl.size = pl.num * 2;
237     pl.a = new int[pl.size];
238     int s, t;
239     for (int i = 0, j = 0; i < pl.num;)
240     {
241         input >> s >> t; 
242         pl.a[j] = t; pl.a[j+1] = s;
243         i++; j+=2;
244     }
245     return input;
246 }
247 
248 Polynomial::Polynomial(int n)
249     :num(n)
250 {
251     size = num * 2;
252     a = new int[size];
253 }
254 
255 int main(void)
256 {
257     Polynomial poly1, poly2, poly;
258     freopen("in", "r", stdin);
259 
260     cin >> poly1;
261     cout << endl;
262     cin >> poly2;
263     cout << endl; 
264 
265 /*    poly = poly1 + poly2;
266     cout << poly;
267 
268     poly1 -= poly2;
269     poly = poly1;
270     cout << poly;
271 */
272     poly = poly1 * poly2;
273     cout << poly;
274 
275     return 0;
276 }

  这个程序写得不好,重载运算符+,-,*和+=,-=,*=的代码是一样的,以后再改一下。而且很多地方,效率很低,很繁琐。

  总结一下:因为在一个成员函数里面重复定义了一个变量,导致怎么也不对。最后没办法,现学gdb调试了,唉,这才发现错误,以后写代码的时候要过脑子,别想怎么写就怎么写,想想为什么这么写,认真一点儿,就会节约很多时间!当程序出现错误的时候,你的信念不应该是:我的程序是对的啊,怎么看怎么对。而应该是这样的:我的程序肯定在某个地方出错了,然后一行一行地检查。这样才会尽快找出错误。

posted on 2012-12-14 08:44  aries__liu  阅读(930)  评论(0编辑  收藏  举报