using namespace std;
Typedef struct
{
Int coef.
Int index;
} datatype.
Typedef struct
{
Datatype * elem;
int length;
} SeqList;
Void BuildPoly (SeqList & amp; S)
{
S.e lem=new datatype [10].
S.l ength=0; int i=1;
Cin> S.e lem - & gt; Coef> S.e lem - & gt; The index;
While (s.e lem [I - 1]. Coef!=1 & amp; & S.e lem [I - 1]. Index!=1)
{s.l ength=I;
Cin> S.e lem [I] coef> S.e lem [I] index; i++; }
}
Void AddPoly (SeqList SeqList s1, s2, SeqList & amp; S3)
{
S3. Elem=new datatype [10].
S3. The length=0;
Datatype * p1=s1. Elem, * p2=s2. Elem, * p3=s3. Elem;
While (p1 and s1. Elem
If (p1 - & gt; Index
P1 + +; P3 + +; S3. Length++;
}
Else if (p1 - & gt; Index> The p2 - & gt; The index)
{p3 - & gt; Coef=p2 - & gt; Coef. P3 - & gt; The index=p2 - & gt; The index;
The p2 + +; P3 + +; S3. Length++; }
Else if (p1 - & gt; The index==p2 - & gt; The index)
{
If (p1 - & gt; Coef + p2 - & gt; Coef!=0)
{
P3 - & gt; Coef=p1 - & gt; Coef + p2 - & gt; Coef.
P3 - & gt; The index=p1 - & gt; The index;
P3 + +; S3. Length++;
}
P1 + +; The p2 + +;
}
}
While (p1 and s1. Elem
P3 - & gt; Coef=p1 - & gt; Coef. P3 - & gt; The index=p1 - & gt; The index;
P1 + +; P3 + +; S3. Length++;
}
While (p2 - s2. Elem
P3 - & gt; Coef=p2 - & gt; Coef. P3 - & gt; The index=p2 - & gt; The index;
The p2 + +; P3 + +; S3. Length++;
}
}
Void DispList (SeqList s)
{
for(int i=0; i
}
Int main ()
{
SeqList s1, s2, s3;
BuildPoly (s1);
BuildPoly (s2);
AddPoly (s1, s2, s3);
DispList (s3);
return 0;
}
CodePudding user response:
void BuildPoly (SeqList & amp; S)
{
S.e lem=new datatype [10].
S.l ength=0;
int i=1;
//cin> S.e lem - & gt; Coef> S.e lem - & gt; The index;
Cin> S.e lem [I - 1]. Coef> S.e lem [I - 1]. The index;
While (s.e lem [I - 1]. Coef!=1 & amp; & S.e lem [I - 1]. Index!=1)
{
S.l ength=I;
Cin> S.e lem [I] coef> S.e lem [I] index;
i++;
}
}
For your reference ~
The test code:
1
12 2
- 1-1
1 1
2 2
- 1-1
1 4 2
Add some print ~ can better understand the program execution order
CodePudding user response:
Thank you very much, that if you want to put the coefficient is zero a delete what to do