Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Lab_1_1;
- {$APPTYPE CONSOLE}
- Uses
- SysUtils,
- Math,
- Windows;
- Type
- TPElement = ^TElement;
- TElement = Record
- Coefficient: Integer;
- Degree: Integer;
- Next: TPElement;
- End;
- Function Meaning(P: TPElement; X: Integer): Integer; //вычисляет значение, подставляю х
- Var
- Sum: Integer;
- Begin
- Sum := 0;
- While P <> Nil Do
- Begin
- Sum := Sum + P^.Coefficient * Round(Power(X, P^.Degree));
- P := P^.Next;
- End;
- Meaning := Sum;
- End;
- Function Equality(P, Q: TPElement): Boolean; //провряет равенство многочленов
- Var
- IsEqual: Boolean;
- Begin
- IsEqual := True;
- While (P <> Nil) And (Q <> Nil) And IsEqual Do
- Begin
- If (P = Nil) And (Q = Nil) Then
- IsEqual := False
- Else
- Begin
- If (P^.Degree <> Q^.Degree) Or (P^.Coefficient <> Q^.Coefficient) Then
- IsEqual := False;
- P := P^.Next;
- Q := Q^.Next;
- End;
- End;
- Equality := IsEqual;
- End;
- Procedure Add(Var R: TPElement; P, Q: TPElement); // создание 3 многочлена из двух
- Var
- X, Y: TPElement;
- Begin
- New(X);
- New(Y);
- R := X;
- While (P <> Nil) And (Q <> Nil) Do
- Begin
- If P^.Degree > Q^.Degree Then
- Begin
- X^.Degree := P^.Degree;
- X^.Coefficient := P^.Coefficient;
- P := P^.Next;
- End
- Else
- If P^.Degree < Q^.Degree Then
- Begin
- X^.Degree := Q^.Degree;
- X^.Coefficient := Q^.Coefficient;
- Q := Q^.Next;
- End
- Else
- Begin
- X^.Degree := P^.Degree;
- X^.Coefficient := P^.Coefficient + Q^.Coefficient;
- P := P^.Next;
- Q := Q^.Next;
- End;
- Y := X;
- New(X);
- Y^.Next := X;
- End;
- Y^.Next := Nil;
- End;
- Function InputList(Degree: Integer): TPElement; //ввод
- Var
- Element, X, Y: TPElement;
- I, Coefficient: Integer;
- Begin
- New(X);
- New(Y);
- Element := X;
- For I := Degree Downto 0 Do
- Begin
- If I <> 0 Then
- Begin
- Write('X^', I, ' * ');
- Read(Coefficient);
- Write(' + ');
- End
- Else
- Begin
- Readln(Coefficient);
- End;
- If Coefficient <> 0 Then
- Begin
- X^.Coefficient := Coefficient;
- X^.Degree := I;
- Y := X;
- New(X);
- Y^.Next := X;
- End;
- End;
- Y^.Next := Nil;
- InputList := Element;
- End;
- Procedure OutputList(List: TPElement); // вывод
- Var
- IsFirstElement: Boolean;
- Begin
- IsFirstElement := True;
- While List <> Nil Do
- Begin
- If Not IsFirstElement Then
- Write(' + ');
- IsFirstElement := False;
- If List^.Degree <> 0 Then
- Write(List^.Coefficient, 'X^', List^.Degree)
- Else
- Write(List^.Coefficient);
- List := List^.Next;
- End;
- Writeln;
- End;
- //Main
- Var
- P, Q, R: TPElement;
- Degree, X: Integer;
- Begin
- //Ввод
- Write('Введите степень многочлена P(X): ');
- Readln(Degree);
- Writeln('Введите коэффициенты: ');
- P := InputList(Degree);
- Write('Введите степень многочлена Q(X): ');
- Readln(Degree);
- Writeln('Введите коэффициенты: ');
- Q := InputList(Degree);
- Write('Многочлен P(X): ');
- OutputList(P);
- Write('Многочлен Q(X): ');
- OutputList(Q);
- Add(R, P, Q);
- Write('Многочлен R(X) = P(X) + Q(X): ');
- OutputList(R);
- Write('Введите значение X: ');
- Readln(X);
- //Вычисление
- Writeln('Q(X) = ', Meaning(Q, X));
- Writeln('P(X) = ', Meaning(P, X));
- Writeln('R(X) = ', Meaning(R, X));
- Write('Равны ли Q(X) и P(X)? Ответ: ');
- If Equality(P, Q) Then
- Write('Равны')
- Else
- Write('Неравны');
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement