Home > Back-end >  For help, up for a week, it is no mistake... Really tuned for a week, back to the dormitory, there i
For help, up for a week, it is no mistake... Really tuned for a week, back to the dormitory, there i

Time:09-21


file1:
 # # ifndef POLYNOMIAL_H_ 
# define POLYNOMIAL_H_
The class Momomial {
Public:
int a;//the coefficient of monomial
int n;//x exponential
Long val_m;//the value of monomial
};

The class Polynomial {

Static int num_p;//define the number of polynomial
Polynomial (const Polynomial & amp; A, int n);
Public:
Int x;
Momomial * p;//define a momomial object pointer
Int num_m;//the number of polynomial
Polynomial () {}
Polynomial (const Polynomial & amp; A);//copy constructor copies (deep)
Polynomial (int n1, int n2);//the constructor n1 is a polynomial number of items, n2 is the value of x
~ Polynomial ();//destructors, release the dynamic array memory
Void input ();//enter each monomial coefficient and the number of times (power)
Void the print ();//print the polynomial
Static int numnum () {return (num_p + +); };//o num (define the number of polynomial)
Long valp (int n);//the value of the polynomial
Friend Polynomial operator + (Polynomial & amp; A, Polynomial & amp; B);//"+" operator, to realize the addition of a polynomial
Polynomial operator=(const Polynomial & amp; A);
};

# endif


The realization of the file2: class
 # include "polynomial. H" 
#include
using namespace std;



Int Polynomial: : num_p=0;



Polynomial: : Polynomial (const Polynomial & amp; A) {
X=a.x;
Num_m=a.n um_m;
P=new Momomial [num_m];
for(int i=0; I}

Polynomial Polynomial: : operator=(const Polynomial & amp; A) {
If (this==& amp; A) return * this;
X=a.x;
Num_m=a.n um_m;
P=new Momomial [num_m];
for(int i=0; IP [I] a=Amy polumbo [I] a;
P [I] n=Amy polumbo [I] n.
}
Return * this;
}




Polynomial: : Polynomial (int n1, int n2) {
Num_p + +;
Num_m=n1;
P=new Momomial (n1);
X=n2;
}

Polynomial: : ~ Polynomial () {
The delete [] p;
}

Void Polynomial: : input () {
for(int i=0; Icin> P [I] a> P [I] n.
}

Void Polynomial: : print () {
for(int j=0; JCoutCout}

Long Polynomial: : valp (int n) {
Long sum=0;
for(int i=0; ILong temp=1;
for(int j=0; J

P [I] val_m=(p [I] a) * temp.
Sum +=p [I] val_m;
}
return sum;
}

Polynomial operator + (Polynomial & amp; A, Polynomial & amp; B) {
Momomial * q=new Momomial [a.n um_m + b.n um_m];
for(int i=0; IIf (iQ [I] a=Amy polumbo [I] a;
Q [I] n=Amy polumbo [I] n.
}
The else {
Q [I] a=p. [I] a;
Q [I] n=p. [I] n.
}
}
int count=0;
For (int i1=0; I1 & lt; A.n um_m; {i1 + +)
For (int j1=0; J1 & lt; B.n um_m; {j1 + +)
If (q [i1] n==q [a.n um_m + j1]. N) {
Q [i1]. A +=q [a.n um_m + j1]. A;
Q [j1 + a.n um_m] a=0;
count++;
break; }
}
}
Polynomial c (a.n um_m + b.n um_m - count, a.x);
Int count1=0;
For (int i2=0; I2 & lt; A.n um_m + b.n um_m; I2 + +) {
If ((q + i2) - & gt; a!=0) {
C.p [count1] a=q [i2]. A;
C.p [count1] n=q [i2] n.
Count1 + +; }
}
The delete [] q;
return c;
}







File3: main function
 # include 
# include "polynomial. H"
using namespace std;
Int main ()
{
Const int x0=3;//the value of x
Const int n1=2;
Polynomial n1, x0 (cat);
The input ();
Polynomial dog (n1, x0);
T input ();
Polynomial dat=dog + cat;
Dat. Print ();
Coutreturn 0;
}

I declare the two class Momomial, Polynomial, the Polynomial has a pointer to an array Momomial, then I want to overload operators "+", make two Polynomial class together
Now I can almost sure the problem in the definition of the overloaded function "+"
 Polynomial operator + (Polynomial & amp; A, Polynomial & amp; B) {
Momomial * q=new Momomial [a.n um_m + b.n um_m];
for(int i=0; IIf (iQ [I] a=Amy polumbo [I] a;
Q [I] n=Amy polumbo [I] n.
}
The else {
Q [I] a=p. [I] a;
Q [I] n=p. [I] n.
}
}
int count=0;
For (int i1=0; I1 & lt; A.n um_m; {i1 + +)
For (int j1=0; J1 & lt; B.n um_m; {j1 + +)
If (q [i1] n==q [a.n um_m + j1]. N) {
Q [i1]. A +=q [a.n um_m + j1]. A;
Q [j1 + a.n um_m] a=0;
count++;
break; }
}
}
Polynomial c (a.n um_m + b.n um_m - count, a.x);
Int count1=0;
For (int i2=0; I2 & lt; A.n um_m + b.n um_m; I2 + +) {
If ((q + i2) - & gt; a!=0) {
C.p [count1] a=q [i2]. A;
C.p [count1] n=q [i2] n.
Count1 + +; }
}
The delete [] q;
return c;
}


Below is a screenshot I run the program

CodePudding user response:

Screenshot didn't pass, I in the hair the
  • Related