venik2405

lab2_2_0

Oct 28th, 2020
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.80 KB | None | 0 0
  1. program LABA_2_2;
  2.  
  3. uses
  4.     System.SysUtils;
  5.  
  6. function Input(X :Integer): Integer;
  7. var
  8.     L : Integer;
  9.     IsCorrect : Boolean;
  10. Begin
  11.     Repeat
  12.         IsCorrect := True;
  13.         Writeln('Введите');
  14.         Try
  15.             Readln(L);
  16.         Except
  17.             Writeln('Введите целое число!');
  18.             IsCorrect := False;
  19.         End;
  20.         If (L = 0) and (X = 2) then
  21.         Begin
  22.             Writeln('Чило не должно быть равно 0');
  23.             IsCorrect := False;
  24.         End;
  25.     Until (IsCorrect);
  26.     Input :=L
  27. End;
  28.  
  29. function FindNumerator(M, N, P, Q :Integer):Integer;
  30. var
  31.     A: Integer;
  32. begin
  33.     A := M * Q + P * N;
  34.     FindNumerator := A;
  35. end;
  36.  
  37. function FindDenumerator(N, Q :Integer):Integer;
  38. var
  39.     B : Integer;
  40. begin
  41.     B := N * Q;
  42.     FindDenumerator := B;
  43. end;
  44.  
  45. function ReduceFraction(C, D , X :Integer):Integer;
  46. begin
  47.  
  48.     repeat
  49.         if C>D then
  50.             C:=C-D
  51.         else
  52.             D:=D-C;
  53.     until (D = C);
  54.     if X = 1 then
  55.         ReduceFraction := C;
  56.     if X = 2 then
  57.         ReduceFraction := D;
  58. end;
  59.  
  60. procedure PrintFraction(A, B, C, D :Integer);
  61. begin
  62.     writeln (A div C , '/', B div D);
  63. end;
  64.  
  65. procedure Main();
  66. var
  67.     A, B, C, D, M, N, P, Q :Integer;
  68. begin
  69.     Writeln('Данная программа находит сумму двух несократимых дробей.');
  70.     Writeln('Введите дроби');
  71.     M := Input(1);
  72.     N := Input(2);
  73.     P := Input(1);
  74.     Q := Input(2);
  75.     A := FindNumerator(M, N, P, Q);
  76.     B := FindDenumerator(N, Q);
  77.     C := A;
  78.     D := B;
  79.     C := ReduceFraction(C, D, 1);
  80.     D := ReduceFraction(C, D , 2);
  81.     Writeln('Их сумма');
  82.     PrintFraction(A, B, C, D);
  83.     Readln;
  84. end;
  85.  
  86. Begin
  87.     Main();
  88. End.
Add Comment
Please, Sign In to add comment