Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Records;
- uses
- System.SysUtils, Math;
- type
- PPolynomialNode = ^PolynomialNode;
- PolynomialNode = record
- Coefficient: Integer;
- Power: Integer;
- Next: PPolynomialNode;
- end;
- procedure InputPolynomial(var p: PPolynomialNode);
- var
- n, coef, pow: Integer;
- temp, last: PPolynomialNode;
- begin
- Writeln('Input polynom power: ');
- ReadLn(n);
- last := nil;
- while (n >= 0) do
- begin
- Write('Input coefficient for variable with power ', n, ': ');
- ReadLn(coef);
- new(temp);
- temp^.Coefficient := coef;
- temp^.Power := n;
- temp^.Next := nil;
- if last = nil then
- p := temp
- else
- last^.Next := temp;
- last := temp;
- dec(n);
- end;
- end;
- function Equality(p, q: PPolynomialNode): Boolean;
- begin
- while (p <> nil) and (q <> nil) do
- begin
- if (p^.Coefficient <> q^.Coefficient) or
- (p^.Power <> q^.Power) then
- exit(False);
- p := p^.Next;
- q := q^.Next;
- end;
- Result := (p = nil) and (q = nil);
- end;
- function Meaning(p: PPolynomialNode; x: Integer): Integer;
- var
- sum, termValue: Integer;
- begin
- sum := 0;
- while p <> nil do
- begin
- termValue := Round(p^.Coefficient * Power(x, p^.Power));
- sum := sum + termValue;
- p := p^.Next;
- end;
- Result := sum;
- end;
- procedure Add(p, q: PPolynomialNode; var r: PPolynomialNode);
- var
- last, temp: PPolynomialNode;
- begin
- new(r);
- last := r;
- while (p <> nil) and (q <> nil) do
- begin
- new(temp);
- if p^.Power > q^.Power then
- begin
- temp^ := p^;
- p := p^.Next;
- end
- else if q^.Power > p^.Power then
- begin
- temp^ := q^;
- q := q^.Next;
- end
- else
- begin
- temp^.Coefficient := p^.Coefficient + q^.Coefficient;
- temp^.Power := p^.Power;
- p := p^.Next;
- q := q^.Next;
- end;
- last^.Next := temp;
- last := temp;
- end;
- if p <> nil then
- last^.Next := p
- else
- last^.Next := q;
- end;
- procedure PrintPolynomial(p: PPolynomialNode);
- var isFirstEl: boolean;
- begin
- isFirstEl := true;
- while p <> nil do
- begin
- if (p^.Next <> nil) and (p^.Coefficient <> 0) and (isFirstEl = false) then
- begin
- Write(' + ', p^.Coefficient, 'x^', p^.Power)
- end
- else if (p^.Next = nil) and (p^.Coefficient <> 0) and (isFirstEl = false) then
- Write(' + ', p^.Coefficient, 'x^', p^.Power)
- else if (isFirstEl = true) and (p^.Coefficient <> 0) then
- begin
- Write(p^.Coefficient, 'x^', p^.Power);
- isFirstEl := false;
- end;
- p := p^.Next;
- end;
- WriteLn;
- end;
- var
- p, q, r: PPolynomialNode;
- x: Integer;
- begin
- WriteLn('Input polynom p:');
- InputPolynomial(p);
- WriteLn('Input polynom q:');
- InputPolynomial(q);
- WriteLn('Polynom p: ');
- PrintPolynomial(p);
- WriteLn('Polynom q: ');
- PrintPolynomial(q);
- if Equality(p, q) then
- WriteLn('Polynoms are equal')
- else
- WriteLn('Polynoms are not equal');
- WriteLn('Input X value:');
- ReadLn(x);
- WriteLn('Polynom p value with x = ', x, ' equals ', Meaning(p, x));
- Add(p, q, r);
- Write('Sum of p and q: ');
- PrintPolynomial(r);
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement