创建一个类,重载运算符实现多项式的加,减,乘运算
December 14, 2012
唉,我太弱了,这么个简单的东西,还是用数组写的,却因为各种错误,弄了一个晚上,C++还得好好学啊,各种语法,要弄清楚。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <cstring>
using namespace std;
class Polynomial
{
public:
Polynomial(int = 0);
friend istream & operator >> (istream & input, Polynomial & pl);
friend ostream & operator << (ostream & output, Polynomial & pl);
Polynomial & operator += (Polynomial & add);
Polynomial & operator -= (Polynomial & add);
Polynomial & operator - (Polynomial & add);
Polynomial & operator * (Polynomial &);
Polynomial & operator + (Polynomial & add);
Polynomial & operator *= (Polynomial &);
Polynomial & operator = (Polynomial & right);
Polynomial & test(Polynomial & right);
private:
int *a;
int size, num;
};
Polynomial & Polynomial::operator *= (Polynomial & mu)
{
Polynomial b, c;
c = *this; b = *this;
for (int j=0,r=0; j<b.num;++j,r+=2)
{
(*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
}
for (int i=1,k=2;i<mu.num;++i,k+=2)
{
for (int j=0,r=0; j<b.num;++j,r+=2)
{
b.a[r]+=mu.a[k];
b.a[r+1]*=mu.a[k+1];
}
*this += b;
b = c;
}
return *this;
}
Polynomial & Polynomial::operator * (Polynomial & mu)
{
Polynomial b, c;
c = *this; b = *this;
for (int j=0,r=0; j<b.num;++j,r+=2)
{
(*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
}
for (int i=1,k=2;i<mu.num;++i,k+=2)
{
for (int j=0,r=0; j<b.num;++j,r+=2)
{
b.a[r]+=mu.a[k];
b.a[r+1]*=mu.a[k+1];
}
*this += b;
b = c;
}
return *this;
}
Polynomial & Polynomial::operator = (Polynomial & right)
{
if (&right != this)
{
delete [] a;
size = right.size;
num = right.num;
a = new int[size];
for (int i = 0; i < size; ++i)
a[i]=right.a[i];
}
else
cout<<"Attempted assignment of a polynomial to itself" << endl;
return *this;
}
Polynomial & Polynomial::operator -= (Polynomial & add)
{
int number = add.num, mrk = 0;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < num; ++j,k+=2)
{
if (q[s]==a[k])
{
mrk = 1;
a[k+1]-=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[size+2];
for (int d=0;d<size;d++)
p[d]=a[d];
p[size] = q[s];
p[size+1] = -q[s+1];
q[s+1]=0;
a = p;
num++;
size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator - (Polynomial & add)
{
int number = add.num, mrk = 0;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < num; ++j,k+=2)
{
if (q[s]==a[k])
{
mrk = 1;
a[k+1]-=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[size+2];
for (int d=0;d<size;d++)
p[d]=a[d];
p[size] = q[s];
p[size+1] = -q[s+1];
q[s+1]=0;
a = p;
num++;
size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator += (Polynomial & add)
{
int number = add.num, mrk = 0;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < num; ++j,k+=2)
{
if (q[s]==a[k])
{
mrk = 1;
a[k+1]+=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[size+2];
for (int d=0;d<size;d++)
p[d]=a[d];
p[size] = q[s];
p[size+1] = q[s+1];
q[s+1]=0;
a = p;
num++;
size+=2;
}
}
return *this;
}
Polynomial & Polynomial::operator + (Polynomial & add)
{
int number = add.num, mrk = 0;
Polynomial & mi = *this;
int cosize = add.size; int * q = new int[cosize];
for (int d=0;d<cosize;++d) q[d]=add.a[d];
for (int i = 0, s = 0; i < number; ++i, s+=2)
{
mrk = 0;
for (int j = 0, k = 0; j < mi.num; ++j,k+=2)
{
if (q[s]==mi.a[k])
{
mrk = 1;
mi.a[k+1]+=q[s+1];
q[s+1]=0;
}
}
if (!mrk)
{
int *p = new int[mi.size+2];
for (int d=0;d<mi.size;d++)
p[d]=mi.a[d];
p[mi.size] = q[s];
p[mi.size+1] = q[s+1];
q[s+1]=0;
mi.a = p;
mi.num++;
mi.size+=2;
}
}
return mi;
}
ostream & operator << (ostream & output, Polynomial & pl)
{
for (int i = 0, j = 0; i < pl.num; ++i)
{
if (pl.a[j+1]==0)
{
output << "0 0 "; j+=2; continue;
}
output << pl.a[j+1] << " " << pl.a[j] << " ";
j+=2;
}
output << endl;
return output;
}
istream & operator >> (istream & input, Polynomial & pl)
{
cout << "input the number of Polynomial terms: ";
input >> pl.num;
pl.size = pl.num * 2;
pl.a = new int[pl.size];
int s, t;
for (int i = 0, j = 0; i < pl.num;)
{
input >> s >> t;
pl.a[j] = t; pl.a[j+1] = s;
i++; j+=2;
}
return input;
}
Polynomial::Polynomial(int n)
:num(n)
{
size = num * 2;
a = new int[size];
}
int main(void)
{
Polynomial poly1, poly2, poly;
freopen("in", "r", stdin);
cin >> poly1;
cout << endl;
cin >> poly2;
cout << endl;
/* poly = poly1 + poly2;
cout << poly;
poly1 -= poly2;
poly = poly1;
cout << poly;
*/
poly = poly1 * poly2;
cout << poly;
return 0;
}
这个程序写得不好,重载运算符+,-,和+=,-=,=的代码是一样的,以后再改一下。而且很多地方,效率很低,很繁琐。 总结一下:因为在一个成员函数里面重复定义了一个变量,导致怎么也不对。最后没办法,现学gdb调试了,唉,这才发现错误,以后写代码的时候要过脑子,别想怎么写就怎么写,想想为什么这么写,认真一点儿,就会节约很多时间!当程序出现错误的时候,你的信念不应该是:我的程序是对的啊,怎么看怎么对。而应该是这样的:我的程序肯定在某个地方出错了,然后一行一行地检查。这样才会尽快找出错误。