Advertisement
strCarne

Untitled

Jun 14th, 2023
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.52 KB | None | 0 0
  1. program main;
  2.  
  3. type
  4.   pt = ^element;
  5.   element = record
  6.       power:integer;
  7.       coefficient:integer;
  8.       next:pt;
  9.       end;
  10.   var
  11.         first,second,result: pt;
  12.  
  13. procedure make(element:pt;length:integer);
  14. var
  15.   i,temp: integer;
  16. begin
  17.   write('Enter the coefficients: ');
  18.  
  19.   for i := 1 to length do
  20.   begin
  21.     read(temp);
  22.     if temp<>0 then
  23.     begin
  24.       new(element^.next);
  25.       element:=element^.next;
  26.       element^.power:=i;
  27.       element^.coefficient:=temp;
  28.     end;
  29.   end;
  30.     element^.next:=nil;
  31. end;
  32.  
  33. procedure print(element:pt);
  34. begin
  35.     if element^.coefficient <> 1 then
  36.         write(element^.coefficient);
  37.     if element^.power <> 1 then
  38.     write('x^',element^.power)
  39.     else
  40.         write('x');
  41.   element:=element^.next;
  42.   while element<>nil do
  43.   begin
  44.     if (element^.coefficient>0) then
  45.     begin
  46.       write ('+',element^.coefficient,'x^',element^.power);
  47.       element:=element^.next;
  48.     end
  49.     else
  50.     begin
  51.       write (element^.coefficient,'x^',element^.power);
  52.       element:=element^.next;
  53.     end;
  54.   end;
  55.  
  56. end;
  57.  
  58. function equality(firstElement,secondElement: pt):boolean;
  59. var
  60.   i:integer;
  61. begin
  62.   equality:=true;
  63.   while ((firstElement<>nil) and (secondElement<>nil)) do
  64.   begin
  65.     if (firstElement^.coefficient<>secondElement^.coefficient) or (firstElement^.power<>secondElement^.power) then
  66.       equality:=false;
  67.     firstElement:=firstElement^.next;
  68.     secondElement:=secondElement^.next;
  69.   end;
  70.   if ((firstElement=nil) and (secondElement<>nil)) or ((firstElement<>nil) and (secondElement=nil)) then
  71.     equality:=false;
  72. end;
  73.  
  74. function pow(x:real;degree:integer):real;
  75. var
  76.   xNow:real;
  77.   i: Integer;
  78. begin
  79.   xNow:=x;
  80.   for i := 2 to degree do
  81.   begin
  82.     xNow:=xNow*x;
  83.   end;
  84.   pow:=xNow;
  85. end;
  86.  
  87. function meaning(polynomial:pt;point:real):real;
  88. var
  89.   polynomialInPoint:real;
  90. begin
  91.   while (polynomial<>nil) do
  92.   begin
  93.     polynomialInPoint:=polynomialInPoint+pow(point,polynomial^.power)*polynomial^.coefficient;
  94.     polynomial:=polynomial^.next;
  95.   end;
  96.   meaning:=polynomialInPoint;
  97. end;
  98.  
  99. procedure add(firstElement,secondElement,resAdd:pt);
  100. begin
  101.   while ((firstElement<>nil) and (secondElement<>nil)) do
  102.   begin
  103.     new(resAdd^.next);
  104.     resAdd:=resAdd^.next;
  105.     if firstElement^.power=secondElement^.power then
  106.     begin
  107.       resAdd^.coefficient:=firstElement^.coefficient+secondElement^.coefficient;
  108.       resAdd^.power:=secondElement^.power;
  109.       firstElement:=firstElement^.next;
  110.       secondElement:=secondElement^.next;
  111.     end
  112.     else
  113.     begin
  114.       if firstElement^.power>secondElement^.power then
  115.       begin
  116.         resAdd^.coefficient:=secondElement^.coefficient;
  117.         resAdd^.power:=secondElement^.power;
  118.         secondElement:=secondElement^.next;
  119.       end
  120.       else
  121.       begin
  122.         resAdd^.coefficient:=firstElement^.coefficient;
  123.         resAdd^.power:=firstElement^.power;
  124.         firstElement:=firstElement^.next;
  125.       end;
  126.     end;
  127.   end;
  128.   if (firstElement<>nil) and (secondElement=nil) then
  129.   begin
  130.     while (firstElement<>nil) do
  131.     begin
  132.       new(resAdd^.next);
  133.       resAdd:=resAdd^.next;
  134.       resAdd^.power:=firstElement^.power;
  135.       resAdd^.coefficient:=firstElement^.coefficient;
  136.       firstElement:=firstElement^.next;
  137.     end;
  138.   end
  139.   else
  140.   begin
  141.     while (secondElement<>nil) do
  142.     begin
  143.       new(resAdd^.next);
  144.       resAdd:=resAdd^.next;
  145.       resAdd^.power:=secondElement^.power;
  146.       resAdd^.coefficient:=secondElement^.coefficient;
  147.       secondElement:=secondElement^.next;
  148.     end;
  149.   end;
  150.   resAdd^.next:=nil;
  151. end;
  152. var
  153.   degree:integer;
  154.   dot:real;
  155. begin
  156.   write('Enter the degree of the first polynomial: ');
  157.   readln(degree);
  158.   new(first);
  159.   make(first,degree);
  160.   first:=first^.next;
  161.   write('The first polynomial: ');
  162.   print(first);
  163.   writeln;
  164.   write('Enter the degree of the second polynomial: ');
  165.   readln(degree);
  166.   new(second);
  167.   make(second,degree);
  168.   second:=second^.next;
  169.   write('The second polynomial: ');
  170.   print(second);
  171.   writeln;
  172.   if equality(first,second) then
  173.     writeln('The polynomials are equal')
  174.   else
  175.     writeln('The polynomials not equal');
  176.  
  177.   write('Enter a point: ');
  178.   readln(dot);
  179.   write('The value of the first polynomial at the point ',dot:3:0,': ',meaning(first,dot):5:3);
  180.   writeln;
  181.   write('The value of the second polynomial at the point ',dot:3:0,': ',meaning(second,dot):5:3);
  182.   writeln;
  183.   write('Sum of polynomials: ');
  184.   new(result);
  185.   add(first,second,result);
  186.   result:=result^.next;
  187.   print(result);
  188.   readln;
  189. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement