Hey guys i have small problem with my program. I wrote 2 functions for adding and substracting polynomials. Here's a function for adding.
Wielomian Wielomian::operator +(Wielomian &w)
{
Wielomian pomW=*this;
Wielomian pomM=w;
if(pomM.st > pomW.st)
{
pomM = *this;
pomW = w;
}
for(int i=pomM.st;i>=0;i--)
{
pomW.wsp[i] = pomW.wsp[i] + pomM.wsp[i];
}
return pomW;
}
And i created also 2 polynomials. Wielomian w1("15x^3+x^2+x+2"); Wielomian w2("3x^2+x+3");
The thing is when i make a call: cout << w1-w2; where the first polynomial is bigger all is ok, but when im making cout << w2-w1; where the first polynomial is smaller, the program is showing good result but then crushing. Please help me. The same thing is with substracting.
ostream & operator <<(ostream &wyjscie, const Wielomian &w)
{
for (int i=w.st; i>=0;i--)
{
if(i!=0 && w.wsp[i]!=0)
{
if(i==1)
wyjscie << w.wsp[i] << "x" << (w.wsp[i-1]>0 ? "+" : "");
else
wyjscie << w.wsp[i] << "x^" << i << (w.wsp[i-1]>0 ? "+" : "");
}
else if(i!=0 && w.wsp[i]==0)
{
wyjscie << (w.wsp[i-1]>0 ? "+" : "");
}
else if(i==0)
wyjscie << w.wsp[i];
}
return wyjscie;
}
Variables of class:
int st;
int *wsp;
Copy constructor:
Wielomian::Wielomian(Wielomian &w)
{
st=w.st;
wsp = new int[w.st];
for(int i=0; i<=st; i++)
wsp[i]=w.wsp[i];
}