0

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];
}
Darek D
  • 9
  • 3
  • How your insertion operator(<<) and operator - is overloaded as they are more relevant to this question instead operator+ – PapaDiHatti May 20 '17 at 01:35
  • 2
    Recommendation: test for size, then assign. right now you could wind up doing the assignment twice. to track what's killing your program I think we'll need a [mcve]. – user4581301 May 20 '17 at 01:39
  • @Kapil i mean the situation is the same for adding and substracting operator. – Darek D May 20 '17 at 01:39
  • I added how operator << is overloaded. – Darek D May 20 '17 at 01:40
  • 1
    Does Wielomian obey [the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) Your assignments may be doomed if it does not. – user4581301 May 20 '17 at 01:40
  • @user4581301 i didn't declare destructor yet, that may cause a problem? – Darek D May 20 '17 at 01:42
  • What member variables does `Wielomian` have? And if it has explicit copy constructor and assignment operator can you show them? – Chris Drew May 20 '17 at 01:45
  • 1
    Can't tell without seeing the definition of pretty much the whole class, I'm afraid. Usually leaving out the destructor results in memory leaks, not crashes though. – user4581301 May 20 '17 at 01:45
  • @ChrisDrew i added variables and copy constructor to my main post + i dont have assignment constructor. – Darek D May 20 '17 at 01:50

1 Answers1

1

but when im making cout << w2-w1; where the first polynomial is smaller, the program is showing good result but then crushing.

You need to define the copy-assignment operator, which is used in this case:

if(pomM.st > pomW.st)
{
   pomM = *this;
   pomW = w;
}

You also should make some correction in the copy constructor to avoid going out of bounds in the wsp array.

Wielomian::Wielomian(const Wielomian &w) // <-- const
{
  st=w.st;
  wsp = new int[w.st];
  for(int i=0; i < st; i++) // <--  < instead of <=
    wsp[i]=w.wsp[i];
}

// assignment operator
Wielomian& operator= (const Wielomian &w)
{ if(st > 0) delete[] wsp;
  st=w.st;
  wsp = new int[w.st];
  for(int i=0; i < st; i++)
    wsp[i]=w.wsp[i];

  return *this;
}

Finally, you could use a std::vector for wsp, this is recommended and makes things easier and less error-prone. std::vector will manage itself the allocated resources so you wont need any additional non-custom destructor, copy-constructor etc (see "rule of zero").

If you don't mode to using std::vector, don't forget to write a custom destructor in order to delete[] wsp. (see "rule of three").

p.s. another out-of bound access is made here

for(int i=w.st; i>=0;i--)

should be for(int i=w.st - 1; i>=0; i--). You should check again all your iterations over the arrays and make sure to eliminate any out of bound access.

A.S.H
  • 29,101
  • 5
  • 23
  • 50