Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program lab1_1;
- {$APPTYPE CONSOLE}
- {$R *.res}
- Uses
- SysUtils;
- Type
- Polynom = ^EPolynom;
- EPolynom = Record
- Number: Integer;
- Power: Integer;
- Next: Polynom;
- End;
- Procedure SystemIn(Var Res: Integer);
- Var
- Error: Integer;
- S: String;
- Begin
- Repeat
- Readln(S);
- Val(S, Res, Error); // строка -> целое число
- If Error <> 0 Then
- WriteLn('Ввведите число: ');
- Until Error = 0;
- End;
- Procedure Make(X: Polynom; N: Integer);
- Var
- Y: Polynom;
- Number: Integer;
- Begin
- While (N >= 0) Do
- Begin
- Write('Введите множитель (X^', N, ') = ');
- SystemIn(Number);
- If Number <> 0 Then
- Begin
- X^.Number := Number;
- X^.Power := N;
- Y := X;
- New(X);
- Y^.Next := X;
- End;
- N := N - 1;
- End;
- Y^.Next := Nil;
- End;
- Procedure SystemOut(P: Polynom);
- Begin
- If P^.Power <> 0 Then
- Begin
- If P^.Number = 1 Then
- Write('X^', P^.Power)
- Else If P^.Number > 1 Then
- Write(P^.Number, 'X^', P^.Power)
- Else If P^.Number = -1 Then
- Write('-X^', P^.Power)
- Else If P^.Number < 0 Then
- Write(P^.Number, 'X^', P^.Power);
- End
- Else
- Write(P^.Number);
- While P <> Nil Do
- Begin
- P := P^.Next;
- If (P <> Nil) Then
- Begin
- If (P^.Number < 0) Then
- Begin
- Write(' - ');
- If P^.Power <> 0 Then
- Begin
- If P^.Number = -1 Then
- Write('X^', P^.Power)
- Else
- Write(-1 * P^.Number, 'X^', P^.Power);
- End
- Else
- Begin
- Write(-1 * P^.Number);
- End;
- End
- Else
- Begin
- Write(' + ');
- If P^.Power <> 0 Then
- Begin
- If P^.Number = 1 Then
- Write('X^', P^.Power)
- Else
- Write(P^.Number, 'X^', P^.Power);
- End
- Else
- Begin
- Write(P^.Number);
- End;
- End;
- End;
- End;
- End;
- Function Equality(P: Polynom; Q: Polynom): Boolean;
- // проверяет равенство многочленов Р и Q
- Var
- Flag: Boolean;
- Begin
- Flag := True;
- While (P <> Nil) And (Q <> Nil) And Flag Do
- Begin
- If P^.Power = Q^.Power Then
- Begin
- If P^.Number <> Q^.Number Then
- Flag := False;
- End
- Else
- Flag := False;
- P := P^.Next;
- Q := Q^.Next;
- End;
- If Flag Then
- WriteLn('Равны')
- Else
- WriteLn('Не равны.');
- Result := Flag;
- End;
- Function Meaning(P: Polynom; X: Integer): Integer;
- // вычисляет значение многочлена в целочисленной точке х
- Var
- I, Mult: Integer;
- Begin
- Result := 0;
- While P <> Nil Do
- Begin
- Mult := 1;
- For I := 1 To P^.Power Do
- Mult := Mult * X;
- Result := Result + P^.Number * Mult;
- P := P^.Next;
- End;
- End;
- Procedure Add(P: Polynom; Q: Polynom; R: Polynom);
- // вычисление суммы многочленов Q и R
- Var
- Y: Polynom;
- Begin
- While (Q <> Nil) And (P <> Nil) Do
- Begin
- If P^.Power < Q^.Power Then
- Begin
- R^.Number := Q^.Number;
- R^.Power := Q^.Power;
- Q := Q^.Next;
- End
- Else If P^.Power = Q^.Power Then
- Begin
- R^.Number := Q^.Number + P^.Number;
- R^.Power := Q^.Power;
- P := P^.Next;
- Q := Q^.Next;
- End
- Else If P^.Power > Q^.Power Then
- Begin
- R^.Number := P^.Number;
- R^.Power := P^.Power;
- P := P^.Next;
- End;
- Y := R;
- New(R);
- Y^.Next := R;
- End;
- While (Q <> Nil) Do
- Begin
- R^.Number := Q^.Number;
- R^.Power := Q^.Power;
- Q := Q^.Next;
- Y := R;
- New(R);
- Y^.Next := R;
- End;
- While (P <> Nil) Do
- Begin
- R^.Number := P^.Number;
- R^.Power := P^.Power;
- P := P^.Next;
- Y := R;
- New(R);
- Y^.Next := R;
- End;
- Y^.Next := Nil;
- End;
- Var
- First, Second, Third: Polynom;
- N, X: Integer;
- Begin
- New(First);
- New(Second);
- New(Third);
- Write('Введите максимальную степень уравнения Р: ');
- SystemIn(N);
- Make(First, N);
- Write('P: ');
- SystemOut(First);
- WriteLn;
- Write('Введите максимальную степень уравнения Q: ');
- SystemIn(N);
- Make(Second, N);
- Write('Q: ');
- SystemOut(Second);
- WriteLn;
- Equality(First, Second);
- Write('Сумма уравнений P и Q: ');
- Add(First, Second, Third);
- SystemOut(Third);
- WriteLn;
- Write('Введите целочисленное X1 для функции P: ');
- Readln(X);
- WriteLn('P(', X, ') = ', Meaning(First, X));
- Write('Введите целочисленное X2 для функции Q: ');
- Readln(X);
- WriteLn('Q(', X, ') = ', Meaning(Second, X));
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement