Advertisement
Sauka1337

Untitled

Feb 16th, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.52 KB | None | 0 0
  1. program Records;
  2.  
  3. uses
  4.     System.SysUtils, Math;
  5.  
  6. type
  7.     PPolynomialNode = ^PolynomialNode;
  8.  
  9.     PolynomialNode = record
  10.         Coefficient: Integer;
  11.         Power: Integer;
  12.         Next: PPolynomialNode;
  13.     end;
  14.  
  15. procedure InputPolynomial(var p: PPolynomialNode);
  16. var
  17.     n, coef, pow: Integer;
  18.     temp, last: PPolynomialNode;
  19. begin
  20.     Writeln('Input polynom power: ');
  21.     ReadLn(n);
  22.     last := nil;
  23.     while (n >= 0) do
  24.     begin
  25.         Write('Input coefficient for variable with power ', n, ': ');
  26.         ReadLn(coef);
  27.         new(temp);
  28.         temp^.Coefficient := coef;
  29.         temp^.Power := n;
  30.         temp^.Next := nil;
  31.         if last = nil then
  32.             p := temp
  33.         else
  34.             last^.Next := temp;
  35.         last := temp;
  36.         dec(n);
  37.     end;
  38. end;
  39.  
  40. function Equality(p, q: PPolynomialNode): Boolean;
  41. begin
  42.  
  43.     while (p <> nil) and (q <> nil) do
  44.     begin
  45.         if (p^.Coefficient <> q^.Coefficient) or
  46.           (p^.Power <> q^.Power) then
  47.             exit(False);
  48.         p := p^.Next;
  49.         q := q^.Next;
  50.     end;
  51.     Result := (p = nil) and (q = nil);
  52. end;
  53.  
  54. function Meaning(p: PPolynomialNode; x: Integer): Integer;
  55. var
  56.     sum, termValue: Integer;
  57. begin
  58.     sum := 0;
  59.  
  60.     while p <> nil do
  61.     begin
  62.         termValue := Round(p^.Coefficient * Power(x, p^.Power));
  63.         sum := sum + termValue;
  64.  
  65.         p := p^.Next;
  66.     end;
  67.  
  68.     Result := sum;
  69. end;
  70.  
  71. procedure Add(p, q: PPolynomialNode; var r: PPolynomialNode);
  72. var
  73.     last, temp: PPolynomialNode;
  74. begin
  75.     new(r);
  76.     last := r;
  77.     while (p <> nil) and (q <> nil) do
  78.     begin
  79.         new(temp);
  80.         if p^.Power > q^.Power then
  81.         begin
  82.             temp^ := p^;
  83.             p := p^.Next;
  84.         end
  85.         else if q^.Power > p^.Power then
  86.         begin
  87.             temp^ := q^;
  88.             q := q^.Next;
  89.         end
  90.         else
  91.         begin
  92.             temp^.Coefficient := p^.Coefficient + q^.Coefficient;
  93.             temp^.Power := p^.Power;
  94.             p := p^.Next;
  95.             q := q^.Next;
  96.         end;
  97.         last^.Next := temp;
  98.         last := temp;
  99.     end;
  100.     if p <> nil then
  101.         last^.Next := p
  102.     else
  103.         last^.Next := q;
  104. end;
  105.  
  106. procedure PrintPolynomial(p: PPolynomialNode);
  107. var isFirstEl: boolean;
  108. begin
  109.     isFirstEl := true;
  110.     while p <> nil do
  111.     begin
  112.         if (p^.Next <> nil) and (p^.Coefficient <> 0) and (isFirstEl = false) then
  113.         begin
  114.             Write(' + ', p^.Coefficient, 'x^', p^.Power)
  115.         end
  116.         else if (p^.Next = nil) and (p^.Coefficient <> 0) and (isFirstEl = false) then
  117.             Write(' + ', p^.Coefficient, 'x^', p^.Power)
  118.         else if (isFirstEl = true) and (p^.Coefficient <> 0) then
  119.         begin
  120.             Write(p^.Coefficient, 'x^', p^.Power);
  121.             isFirstEl := false;
  122.         end;
  123.         p := p^.Next;
  124.     end;
  125.     WriteLn;
  126. end;
  127.  
  128. var
  129.     p, q, r: PPolynomialNode;
  130.     x: Integer;
  131.  
  132. begin
  133.     WriteLn('Input polynom p:');
  134.     InputPolynomial(p);
  135.     WriteLn('Input polynom q:');
  136.     InputPolynomial(q);
  137.  
  138.     WriteLn('Polynom p: ');
  139.     PrintPolynomial(p);
  140.     WriteLn('Polynom q: ');
  141.     PrintPolynomial(q);
  142.  
  143.     if Equality(p, q) then
  144.         WriteLn('Polynoms are equal')
  145.     else
  146.         WriteLn('Polynoms are not equal');
  147.  
  148.     WriteLn('Input X value:');
  149.     ReadLn(x);
  150.     WriteLn('Polynom p value with x = ', x, ' equals ', Meaning(p, x));
  151.  
  152.     Add(p, q, r);
  153.  
  154.     Write('Sum of p and q: ');
  155.     PrintPolynomial(r);
  156.     ReadLn;
  157.  
  158. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement