SHARE
TWEET

Untitled

a guest Nov 19th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program laba31;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10. type
  11.   Tset = set of Char;
  12.  
  13. procedure InputStringsFromConsole(var Str1: string; var Str2: string);
  14.  
  15. begin
  16.   Writeln('Enter first string');
  17.   Readln(Str1);
  18.   Writeln('Enter second string');
  19.   Readln(Str2);
  20. end;
  21.  
  22. procedure InputStringsFromFile(var Str1: string; var Str2: string);
  23.  
  24. var
  25.   IsCorrectInput: Boolean;
  26.   UserFile: TextFile;
  27.   FileName: String;
  28.  
  29. begin
  30.   repeat
  31.     IsCorrectInput := True;
  32.     Writeln('Inter file location');
  33.     Readln(FileName);
  34.     if FileExists(FileName) then
  35.     begin
  36.       try
  37.         Assign(UserFile, FileName);
  38.         Reset(UserFile);;
  39.       except
  40.         Writeln('Ur File cant');
  41.         IsCorrectInput := False
  42.       end;
  43.       if IsCorrectInput then
  44.       begin
  45.         if EoF(UserFile) then
  46.         begin
  47.           Writeln('File is empty, choose another file');
  48.           IsCorrectInput := False;
  49.         end
  50.         else
  51.         while not EoF(UserFile) do
  52.         begin
  53.           try
  54.             Readln(UserFile, Str1);
  55.             Readln(UserFile, Str2);
  56.           except
  57.             Writeln('Ivalid data, try again');
  58.             IsCorrectInput := False;
  59.           end;
  60.         end;
  61.         CloseFile(UserFile);
  62.       end;
  63.     end
  64.     else
  65.     begin
  66.       Writeln('Ur file not found, try again');
  67.       IsCorrectInput := False;
  68.     end;
  69.   until IsCorrectInput;
  70. end;
  71.  
  72. procedure ChooseInput(var Str1: String; var Str2: String);
  73.  
  74. var
  75.   Symbol: Char;
  76.   IsCorrect: Boolean;
  77.  
  78. begin
  79.   repeat
  80.     IsCorrect := True;
  81.     Writeln('If u want to input strings from console - enter "1"' + #10#13 + 'If u want to input strings from file - enter "2"' );
  82.     Readln(Symbol);
  83.     if (Symbol <> '1') and (Symbol <> '2') then
  84.     begin
  85.       Writeln('Ur input incorrect, try again');
  86.       IsCorrect := False;
  87.     end;
  88.   until IsCorrect;
  89.   if Symbol = '2' then
  90.     InputStringsFromFile(Str1, Str2)
  91.   else
  92.     InputStringsFromConsole(Str1, Str2);
  93. end;
  94.  
  95. procedure SetsOperations(OperationType: Char; Str1: string; Str2: string; var ResultSet: TSet);
  96.  
  97. var
  98.   FirstSet: TSet;
  99.   SecondSet: TSet;
  100.   i: Integer;
  101.  
  102. begin
  103.   FirstSet := [];
  104.   SecondSet := [];
  105.   ResultSet := [];
  106.   for i := 1 to length(Str1) do
  107.     FirstSet := FirstSet + [Str1 [i]];
  108.   for i := 1 to length(Str2) do
  109.     SecondSet := SecondSet + [Str2 [i]];
  110.   if (OperationType = '1') then
  111.   begin
  112.     ResultSet := FirstSet * SecondSet
  113.   end
  114.   else
  115.     ResultSet := (FirstSet + SecondSet) - (FirstSet * SecondSet);
  116. end;
  117.  
  118. procedure OutputAnswerToConsole(AnswerSet: TSet);
  119. var
  120.   ch: Char;
  121. begin
  122.   if (AnswerSet = []) then
  123.     WriteLn('Ur set is empty')
  124.   else
  125.   begin
  126.     Writeln('Answer set is...');
  127.     for ch := #0 to #255 do
  128.       if (ch in AnswerSet) and (ch <> ' ') then
  129.         Write(ch, ' ');
  130.   end;
  131.   Writeln;
  132. end;
  133.  
  134. procedure OutputAnswerToFile(AnswerSet: TSet);
  135.  
  136. var
  137.   IsCorrectOutput: Boolean;
  138.   ResFile: TextFile;
  139.   FileName: string;
  140.   ch: Char;
  141.  
  142. begin
  143.   repeat
  144.     IsCorrectOutput := True;
  145.     Writeln('Enter output file location');
  146.     Readln(FileName);
  147.     if FileExists(FileName) then
  148.     begin
  149.       try
  150.         Assign(ResFile, FileName);
  151.         Rewrite(ResFile);
  152.       except
  153.         Writeln('Ur file cant be opened');
  154.         IsCorrectOutput := False;
  155.       end;
  156.       if IsCorrectOutput then
  157.       begin
  158.         if (AnswerSet = []) then
  159.           Writeln(ResFile,'Подходящих значений нет.')
  160.         else
  161.         begin
  162.           for ch := #0 to #255 do
  163.             if (ch in AnswerSet)and (ch <> ' ') then
  164.               WriteLn(ResFile, ch, ' ');
  165.         end;
  166.         Writeln('Successfully recorded');
  167.         CloseFile(ResFile);
  168.       end;
  169.     end
  170.     else
  171.     begin
  172.       Writeln('File not found, try again');
  173.       IsCorrectOutput := False;
  174.     end;
  175.   until IsCorrectOutput;
  176. end;
  177.  
  178. var
  179.   FirstString, SecondString: String;
  180.   IsCorrect: Boolean;
  181.   OperationType: Char;
  182.   ResultSet: TSet;
  183.  
  184. begin
  185.   Writeln('Theme: Two lines are given. Develop a procedure that, accordingа'+ #10#13 +' to the value of the first parameter, performsа'+ #10#13 +' the following operations on these lines: itа'+ #10#13 +' finds characters that occur in both lines, finds characters that occur only in the first line, only in the second line.');
  186.   ChooseInput(FirstString, SecondString);
  187.   Writeln(FirstString);
  188.   Writeln(SecondString);
  189.   repeat
  190.     WriteLn('Choose operation:' + #10#13 + 'Enter ''1'' to choose intercaption ' + #10#13 + 'Enter ''2'' to choose difference ');
  191.     ReadLn(OperationType);
  192.     case OperationType of
  193.     '1': IsCorrect := True;
  194.     '2': IsCorrect := True;
  195.     else Writeln('Ur input incorrect, try again');
  196.     end;
  197.   until IsCorrect;
  198.   SetsOperations(OperationType, FirstString, SecondString, ResultSet);
  199.   OutputAnswerToConsole(ResultSet);
  200.   Writeln('Do u want to save ur set in file? (Y/N)');
  201.   IsCorrect := False;
  202.   repeat
  203.     Readln(OperationType);
  204.     OperationType := UpCase(OperationType);
  205.     case OperationType of
  206.     'Y': IsCorrect := True;
  207.     'N': IsCorrect := True;
  208.     else Writeln('Ur input incorrect, try again');
  209.     end;
  210.   until IsCorrect;
  211.   if OperationType = 'Y' then
  212.     OutputAnswerToFile(ResultSet);
  213.   Writeln('End of programm');
  214.   Readln;
  215. end.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top