*Urgent* Please help

Hi, im sorry for the huge post, but I really need help until Monday.

The goal of this is to create a list of polynomials as a <Pol> vector, where Pol is the class I developed.
It "reads" the .txt, gets the strings, transforms the polynomials in the strings into class Pol objects, and it places them in the polynomial vector.

My problem is this: I remove the strings from the text where the polynomials are, and I convert those polynomials(in string) to actual polynomials ( "Convert" in MPol.C ,bellow) , but in line 169 from Mpol.C , when I try to put "temp" ( the highest degree of the polynomial), i get a "memory corruption" error or similar. However if instead of "temp", I place "temp+1" I no longer get any errors, but i get an extra coefficient.

Compilation: g++ -o teste2.exe Pol.C MPol.C teste2.C

Main is "teste2.c"


Pol.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef __POL__
#define __POL__

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class Pol
{
 public:
  //constructores
  Pol();                         //(nome, grau zero, coeff nulos)
  Pol(string ,int);              //(nome, grau variavel, coeff nulos)
  Pol(string , int, int*);       //(nome, grau variavel coeff (array))
  Pol(string ,int, vector<int>); //(nome, grau variavel, coeff(vector)
  Pol(Pol*);
  Pol(Pol&);
  Pol(const Pol&);
  
  void SetPolynomial(int,int*);
  void SetCoefficient(int,int);
  double Evaluate(double);
  const Pol Derivative(); 
  void SetName(string); 
  void SetGrau(int);
 
  string GetName();
  int GetGrau();
  int* GetCoeff();

  //fazer funçoes get e tal (se o tempo o permitir)

  void Addp(const Pol,const Pol); //soma
  void Subp(const Pol,const Pol); //sub
  void Mulpp(const Pol,const Pol); //mult entre pol
  void Scale(int); //mult por escalar


  //definiçao de = -= += *=
  const Pol& operator+(const Pol&); 
  const Pol& operator-(const Pol&);
  const Pol& operator=(const Pol&);
  const Pol& operator-=(const Pol&);
  const Pol& operator+=(const Pol&);
  const Pol& operator*=(const Pol&);
  const Pol& operator* (const Pol&);


  virtual void Print(); // print
  virtual void PrintPol();

  ~Pol() {;} //destrutor

 private:
  string name; //nome polinomio
  int grau; //grau polinomio
  int* coeff; //array com os coeficientes
 };
#endif 


Pol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "Pol.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

Pol::Pol()
{
  name = "";
  grau=0;
  coeff=new int[1];
  coeff=NULL;
}

Pol::Pol(string pname, int pgrau)
{
  name=pname;
  grau=pgrau;
  coeff=new int[1];
  coeff=NULL;
}

Pol::Pol(string pname, int pgrau, int* pcoeff)
{
  name=pname;
  grau=pgrau;
  coeff = new int[pgrau+1];
  for (int i=0;i<grau+1;++i)
    {
      coeff[i]=pcoeff[i];
    }
}

Pol::Pol(string pname, int pgrau, vector<int> vcoeff)
{
  name=pname;
  grau=pgrau;
  coeff = new int[pgrau+1];
  for (int i=0;i<grau+1;++i)
    {
      coeff[i]=vcoeff[i];
    }
}

Pol::Pol(Pol* p)
{
  name="_" + p->name;
  grau=p->grau;
  coeff = new int[grau+1];
  for (int i=0; i<grau+1;++i) 
    {
      coeff[i]=p->coeff[i];
    }
}

Pol::Pol(Pol& p) {
  name="_" + p.name;
  grau=p.grau;
  coeff = new int[grau+1];
  for (int i=0; i<grau+1;++i)  {
    coeff[i]=p.coeff[i];
  }
}

Pol::Pol(const Pol& p) {
  name="_" + p.name;
  grau=p.grau;
  coeff = new int[grau+1];
  for (int i=0; i<grau+1;++i)  {
    coeff[i]=p.coeff[i];
  }
}


void Pol::SetPolynomial(int pgrau,int* pcoeff)
{
  grau=pgrau;
  coeff=pcoeff;
}

void Pol::SetCoefficient(int indice,int cfnew)
{
  coeff[indice]=cfnew;
}

double Pol::Evaluate(double x)
{
  double poln=0;
  poln=coeff[0];
  for(int i=1;i<=grau;++i)
    {
      poln=poln + coeff[i]*pow(x,i);
    }

  return poln;
}

const Pol Pol::Derivative()
{
  Pol P("Derivada "+name, grau-1);
  P.coeff = new int[grau-1];
  for(int i=P.grau; i>=0; i--)
    {
      P.coeff[i]=coeff[i+1]*(i+1);
    }
  return P;
}

void Pol::SetName(string pname)
{
  name=pname;
}

void Pol::SetGrau(int pgrau)
{
  grau=pgrau;
}


void Pol::Addp(const Pol V,const Pol Q) {
  int i=0;
  name="W_Add";
  if((V.grau) >= (Q.grau))
    {
      grau=V.grau;
      coeff=new int[V.grau+1];
      for(i=0; i<=(Q.grau); i++) {
	coeff[i]=V.coeff[i]+Q.coeff[i];
      }
      while(i<=V.grau) {
	coeff[i]=V.coeff[i];
	i++;
      }
    }
  else
    {
      grau=Q.grau;
      coeff=new int[Q.grau+1];
      for(i=0; i<=(V.grau); i++) {
	coeff[i]=V.coeff[i]+Q.coeff[i];
      }
      while(i<=Q.grau) {
	coeff[i]=Q.coeff[i];
	i++;
      }
    }
}

void Pol::Subp(const Pol V,const Pol Q) {
  int i=0;
  name="W_Sub";
  if((V.grau) >= (Q.grau))
    {
      grau=V.grau;
      coeff=new int[1+V.grau];
      for(i=0; i<=(V.grau); ++i)
	{
	  if(i<=Q.grau)
	    coeff[i]=V.coeff[i]-Q.coeff[i];
	  else // if(i>Q.grau) 
	    {
	      coeff[i] = V.coeff[i];
	    }
	}
    }
  else
    {
      grau=Q.grau;
      coeff=new int[Q.grau+1];
      for(i=0; i<=(Q.grau); i++) {
	if(i<=V.grau)
	  coeff[i]=V.coeff[i]-Q.coeff[i];
	else
	  coeff[i]=-Q.coeff[i];
      }
    }
}


void Pol::Mulpp(const Pol V,const Pol Q)
{
 name="W_Mulp";
  if(V.grau>=Q.grau)
    {
      grau=V.grau+Q.grau;
      int i=0;
      coeff=new int[Q.grau+V.grau+1];
      while(i<=V.grau)
	{
	  for(int j=0;j<=Q.grau;++j)
	    {
	      coeff[i+j]=V.coeff[i]*Q.coeff[j]+coeff[i+j];
	    }
	  ++i;
	}
    }
  else //Q.grau>V.grau
    {
      grau=V.grau+Q.grau;
      coeff=new int[Q.grau+V.grau+1];
      int j=0;
      while(j<=Q.grau)
	{
	  for(int i=0;i<=V.grau;++i)
	    {
	      coeff[i+j]=Q.coeff[j]*V.coeff[i]+coeff[i+j];
	    }
	  ++j;
	}
    }
}

void Pol::Scale(int c)
{
  for(int i=0;i<=grau;++i)
    {
     coeff[i]=c*(coeff[i]);
    }
}

string Pol::GetName()
{
  return name;
}

int Pol::GetGrau()
{
  return grau;
}

int* Pol::GetCoeff()
{
  return coeff;
}

void Pol::Print()
{
  std::cout << "\nPrint() Nome: " << name << std::endl;
  for (int i=0;i<grau;++i)
    { 
      std::cout << "Coeffs " << coeff[i] << std::endl;
    }
}

void Pol::PrintPol()
{
  cout << "\nPrint()Pol Nome: " << name << endl;
  for (int i=0;i<=grau;++i)
    { 
      if(coeff[i] != 0) {
	if(i!=grau && i!=0)
	  {      cout << "+ " << coeff[i] << "*X^" << i << " ";
	  }
	else if(i==0)
	  {
	    cout << " " << coeff[i] << "*X^" << i;
	  }
	else
	  {
	    cout << "+ " << coeff[i] << "*X^" << i;
	  }
      }
    }
  cout << "\n " << endl;
}

const Pol& Pol::operator=(const Pol& cp) {
  if (this != &cp) {
    name=cp.name;
    grau=cp.grau;
    coeff=new int[grau];
    for(int i=0; i<=grau ; i++)
      coeff[i]=cp.coeff[i];
  }
  return *this;
}

Pol cp;

const Pol& Pol::operator- (const Pol& pc) {
  cp = *this;
  cp -= pc;
  return cp;
}

const Pol& Pol::operator-=(const Pol& cp) {
  
  if(grau<cp.grau) {
    int* pc =new int[cp.grau];
    grau=cp.grau;

    for(int i=0; i<=cp.grau ; i++) {
      if(i<=grau)
	pc[i]=coeff[i];
     else
       pc[i]=0;
   }

    for(int i=0; i<=cp.grau ; i++) {
      if(i>grau)
	coeff[i]=0;
      coeff[i] =pc[i]-cp.coeff[i];
    }
    return *this;
 }
 else {
   for(int i=0; i<=cp.grau ; i++) {
       coeff[i]-=cp.coeff[i];
   } 
    return *this;
  }
}

const Pol& Pol::operator+=(const Pol& cp) {
 
 if(grau<cp.grau) {
   int* pc =new int[cp.grau];
   grau=cp.grau;

   for(int i=0; i<=cp.grau ; i++) {
     if(i<=grau)
       pc[i]=coeff[i];
     else
       pc[i]=0;
   }

    for(int i=0; i<=cp.grau ; i++) {
      coeff[i]=pc[i]+cp.coeff[i];
    }
    return *this;
 }
 else {
   for(int i=0; i<=cp.grau ; i++) {
       coeff[i]+=cp.coeff[i];
   } 
    return *this;
  }
}

const Pol& Pol::operator+ (const Pol& pc) {
cp = *this;
cp += pc;
return cp;
}

const Pol& Pol::operator* (const Pol& pc) {
cp = *this;
cp *= pc;
return cp;
}

const Pol& Pol::operator*= (const Pol& cp) {
  int* M;

  M =new int[cp.grau+1];

  for(int i=0; i<=cp.grau ; i++) 
    M[i]=cp.coeff[i];
  
  Pol J("PM",cp.grau,M);

 this->Mulpp((const Pol) *this, (const Pol) J);

return *this;
}


The other parts:

Mpol.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef __MPol__
#define __MPol__

#include <string>
#include <vector>
#include "Pol.h"

using namespace std;

class MPol
{
 public:
  MPol() {Pol P; lista;}
  ~MPol() {;}

  vector<string> ReadFile(string);
  Pol Convert(string);
  void AddPol (Pol&);
  void DelPol (string);
  Pol FindPol (string);
  vector<Pol> List();
  void Print();

 private:
  vector<Pol> lista;
};

#endif 


Mpol.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "MPol.h"
#include "Pol.h"
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

vector <string> MPol::ReadFile(string tname)
{
  string line;
  ifstream infile;
  infile.open (tname.c_str());
  int i=0;
  while(getline(infile, line))
    i=i+1;

  //voltar ao inicio
  infile.clear();
  infile.seekg (0, ios::beg);

  vector<string> lpols;
  
  int j=0;
  while(infile.eof()!=true) // To get you all the lines.
    {
      getline(infile,line); // Saves the line in STRING.
      lpols.push_back(line);
      ++j;
    }

  infile.close();

  return lpols;
}

Pol MPol::Convert (string linha)
{
  char arr[4];
  arr[0]=linha[0];
  arr[1]='(';
  arr[2]='x';
  arr[3]=')';
  arr[4]='\0';
  string polname(arr);
  int* a=new int[15];
  for(int bn=0;bn<10;++bn)
    { a[bn]=0; }

  int p=0;
  int k=1;
  int ait=0;
  int agrau[10];
  int temp=0;

  while(p<linha.size())
    {
      string search_str = string("X^");
      p = linha.find_first_of(search_str,p);
      if(p !=-1)
	{
	  char d=linha[p+2];
	  int exp = d-'0';

	  int h=0; 
	  string search_str2 = string(" ");
	  h = linha.find_first_of(search_str2,p-4);
	  if(linha[h+1] != '=' && linha[h+1] != 'X' && linha[h+1] != '+' && linha[h+1] != '-')
	    {
	      char c=linha[h+1];
	      int num = c-'0';
	      a[exp] =num;
	    }
	  else if(linha[h+1] == '-')
	    { 
	      int u=0;
	      int o=2;
	      if(linha[h+o] == 'X') 
		{
		  a[exp] = -1;
		}
	      else
		{
		  while(linha[h+o] != '*')
		    {
		      ++u;
		      ++o; 			 
		    }
		  int num;
		  char vu[3];
		  for (int it=0;it<u;++it)
		    {
		      vu[it]=linha[h+o-u+it];
		    }
		 a[exp] = -atoi(vu);
		}

	    }
	  else if(linha[h+1] == '=')
	    {
	      h = linha.find_first_of(search_str2,h+2);
	      if(linha[h+1] == 'X')
		{
		  a[exp] = 1;
		}
	      else if(linha[h+1] == '+')  
		{
		  char c=linha[h+2];
		  int num = c-'0';
		  a[exp] =num;
		}
	      else if(linha[h+1] == '-') 
		{
		  char c=linha[h+2];
		  int num = c-'0';
		  a[exp] = -num;
		}
	      else 
		{
		  char c=linha[h+1];
		  int num = c-'0';
		  a[exp] = num;
		}
	    }
	  else if(linha[h+1] == '+')
	    {
	      if(linha[h+2] == 'X')  
		{
		  a[exp] = 1;
		}
	      else	
		{
		  char c=linha[h+2];
		  int num = c-'0';
		  a[exp] = num;
		}
	    }
	  else if(linha[h+1] == 'X')
	    {
	      a[exp] = 1;
	    }

	  char c=linha[p+2];
	  agrau[ait]= c -'0';
	  if(agrau[ait]>temp)
	    temp=agrau[ait];

	  p=p+2;	
	}
      /* else
	{
	  q=linha.size();
	  }*/
    }

  int* coef=new int[temp+1];
  
  for(int i=0;i<temp+1;++i)
    {
      coef[i]=a[i];
    }
  // coef[temp+1]=0;
/*
  cout <<"Grau " << temp << endl;
  cout <<"Nome " << polname << endl;
  */
  Pol Px(polname,temp+1,coef);
  //Px.PrintPol();

  delete [] a;
  delete [] coef;
  return Px;
}


void MPol::AddPol(Pol& Q)
{
  lista.push_back(Q);
}


void MPol::DelPol(string nomep)
{
  int teste=0;
  for(int i=0;i<lista.size();i++){
    if(lista.at(i).GetName()==nomep){
      lista.erase(lista.begin()+i);
      teste=1;
      break;
    }
  }
  if(teste==0)
    cout<<"Não existe um polinómio com o nome introduzido" << endl;
}

Pol MPol::FindPol(string nomep)
{
  for(int i=0;i<lista.size();++i)
    {
      if(lista[i].GetName()==nomep)
	{
	  return lista[i];
	}
    }
  cout << "Não existe um polinómio com o nome introduzido" << endl;
  return  Pol("Inexistente",0);	
}

vector<Pol> MPol::List()
{
  return lista;
}

void MPol::Print()
{
  cout<<"\nLista dos polinomios" << endl;
  for(int i=0;i<lista.size();i++)
    lista.at(i).Pol::PrintPol();
}


Teste2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include "MPol.h"
#include "Pol.h"
//#include "cTrab1.h"

using namespace std;

int main()
{
  // cTrab1 C;
  // string s=C.GetPolOperations(B02);
  // cout << "\n" << s << endl;
  //R-P*T


  MPol A;
  vector<string> v;

  v=A.ReadFile("Pol.txt");
    
  for (int i=0;i<6;++i)
    {
      cout << v.at(i) << endl;
    }

  for (int i=0;i<6;++i)
    {
      Pol X;
      X=A.Convert(v[i]);
      A.AddPol(X);
    }

  A.Print();

  vector <Pol> list;
  list=A.List();

  Pol R1;
  R1=A.FindPol("__R(x)");
  Pol P1;
  P1=A.FindPol("____P(x)");
  Pol T1;
  T1=A.FindPol("_T(x)");

  Pol M;
  M.Mulpp(T1,P1);
  Pol D;
  D.Subp(R1,M);

  A.AddPol(M);
  A.AddPol(D);

  vector <Pol> list2;
  list2=A.List();

  A.DelPol("___Q(x)");
  A.DelPol("_T(x)");

  A.Print();

  return 0;
}


Pol.txt
1
2
3
4
5
6
P = 4*X^4 -5*X^6 +2*X^0 -X^1 +2*X^3
Q = 2*X^5 -3*X^9 +4*X^2 +X^1
R = 2*X^5 +1*X^0 +6*X^1
S = 4*X^4 -5*X^6 +2*X^0 -X^1 +2*X^3 
T = X^2 +2*X^1 +2*X^0 
U = -2*X^2 +4*X^1 -5*X^4 +8*X^9 -12*X^7 +7*X^6
Unfortunately, your parser is sort of a mess. It starts by finding "X^" and then works forward and backward from there. I think will fail for something "21X^2" (a multi-digit coefficient).

You should be able to do this by reading it from left to right:
A line is NAME = terms
The terms are an optional coefficient (possibly signed), then an optional "*", then "X^" and then the exponent.

After you get the exponent and coefficient, you can add them to your Polynomial using SetCoefficient(). Or can you? SetCoefficient needs to check whether the exponent is larger than the current degree (grau) and expand the coefficient array if it is.

Some other comments:
operator- should not return cp. What would pol1 + pol2 +pol3 do?
Instead of Pol &operator-() it should be Pol operator-(). (returning a value not a reference. Now here's the cool part: you can define operator-() using operator-=()

1
2
3
4
5
6
Pol Pol::operator-(const Pol &right)
{
    Pol result(*this);
    result -= right;
    return result;
}


Use the same trick for operators +, -, *, and / the same way.

Define a destructor for Pol() to avoid memory leaks.

Pol::operator-=() isn't right. You're setting grau too early. move line 292 to line 300 Also you create the new coeff array at line line 291, but never assign it to coeff. This results in memory corruption as you assign values into the old coeff array and beyond the end of it.

operator+=() probably has the same problem.

Topic archived. No new replies allowed.