Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program main;
- type
- pt = ^element;
- element = record
- power:integer;
- coefficient:integer;
- next:pt;
- end;
- var
- first,second,result: pt;
- procedure make(element:pt;length:integer);
- var
- i,temp: integer;
- begin
- write('Enter the coefficients: ');
- for i := 1 to length do
- begin
- read(temp);
- if temp<>0 then
- begin
- new(element^.next);
- element:=element^.next;
- element^.power:=i;
- element^.coefficient:=temp;
- end;
- end;
- element^.next:=nil;
- end;
- procedure print(element:pt);
- begin
- if element^.coefficient <> 1 then
- write(element^.coefficient);
- if element^.power <> 1 then
- write('x^',element^.power)
- else
- write('x');
- element:=element^.next;
- while element<>nil do
- begin
- if (element^.coefficient>0) then
- begin
- write ('+',element^.coefficient,'x^',element^.power);
- element:=element^.next;
- end
- else
- begin
- write (element^.coefficient,'x^',element^.power);
- element:=element^.next;
- end;
- end;
- end;
- function equality(firstElement,secondElement: pt):boolean;
- var
- i:integer;
- begin
- equality:=true;
- while ((firstElement<>nil) and (secondElement<>nil)) do
- begin
- if (firstElement^.coefficient<>secondElement^.coefficient) or (firstElement^.power<>secondElement^.power) then
- equality:=false;
- firstElement:=firstElement^.next;
- secondElement:=secondElement^.next;
- end;
- if ((firstElement=nil) and (secondElement<>nil)) or ((firstElement<>nil) and (secondElement=nil)) then
- equality:=false;
- end;
- function pow(x:real;degree:integer):real;
- var
- xNow:real;
- i: Integer;
- begin
- xNow:=x;
- for i := 2 to degree do
- begin
- xNow:=xNow*x;
- end;
- pow:=xNow;
- end;
- function meaning(polynomial:pt;point:real):real;
- var
- polynomialInPoint:real;
- begin
- while (polynomial<>nil) do
- begin
- polynomialInPoint:=polynomialInPoint+pow(point,polynomial^.power)*polynomial^.coefficient;
- polynomial:=polynomial^.next;
- end;
- meaning:=polynomialInPoint;
- end;
- procedure add(firstElement,secondElement,resAdd:pt);
- begin
- while ((firstElement<>nil) and (secondElement<>nil)) do
- begin
- new(resAdd^.next);
- resAdd:=resAdd^.next;
- if firstElement^.power=secondElement^.power then
- begin
- resAdd^.coefficient:=firstElement^.coefficient+secondElement^.coefficient;
- resAdd^.power:=secondElement^.power;
- firstElement:=firstElement^.next;
- secondElement:=secondElement^.next;
- end
- else
- begin
- if firstElement^.power>secondElement^.power then
- begin
- resAdd^.coefficient:=secondElement^.coefficient;
- resAdd^.power:=secondElement^.power;
- secondElement:=secondElement^.next;
- end
- else
- begin
- resAdd^.coefficient:=firstElement^.coefficient;
- resAdd^.power:=firstElement^.power;
- firstElement:=firstElement^.next;
- end;
- end;
- end;
- if (firstElement<>nil) and (secondElement=nil) then
- begin
- while (firstElement<>nil) do
- begin
- new(resAdd^.next);
- resAdd:=resAdd^.next;
- resAdd^.power:=firstElement^.power;
- resAdd^.coefficient:=firstElement^.coefficient;
- firstElement:=firstElement^.next;
- end;
- end
- else
- begin
- while (secondElement<>nil) do
- begin
- new(resAdd^.next);
- resAdd:=resAdd^.next;
- resAdd^.power:=secondElement^.power;
- resAdd^.coefficient:=secondElement^.coefficient;
- secondElement:=secondElement^.next;
- end;
- end;
- resAdd^.next:=nil;
- end;
- var
- degree:integer;
- dot:real;
- begin
- write('Enter the degree of the first polynomial: ');
- readln(degree);
- new(first);
- make(first,degree);
- first:=first^.next;
- write('The first polynomial: ');
- print(first);
- writeln;
- write('Enter the degree of the second polynomial: ');
- readln(degree);
- new(second);
- make(second,degree);
- second:=second^.next;
- write('The second polynomial: ');
- print(second);
- writeln;
- if equality(first,second) then
- writeln('The polynomials are equal')
- else
- writeln('The polynomials not equal');
- write('Enter a point: ');
- readln(dot);
- write('The value of the first polynomial at the point ',dot:3:0,': ',meaning(first,dot):5:3);
- writeln;
- write('The value of the second polynomial at the point ',dot:3:0,': ',meaning(second,dot):5:3);
- writeln;
- write('Sum of polynomials: ');
- new(result);
- add(first,second,result);
- result:=result^.next;
- print(result);
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement