Advertisement
nikitaxe132

lab3.1

Nov 20th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.25 KB | None | 0 0
  1. program laba31;
  2.  
  3. uses
  4.   System.SysUtils;
  5.  
  6. function Choic : Boolean;
  7. var Console, IsntCorrect : Boolean;
  8.     Choice : Char;
  9. begin
  10.    IsntCorrect := True;
  11.    While(IsntCorrect) do
  12.    begin
  13.       WriteLn('If you want to enter data from the console, enter Y or y, and if from the file, enter N or n');
  14.       ReadLn(Choice);
  15.       if (Choice = 'Y') or (Choice = 'y') or (Choice = 'N') or (Choice = 'n') then
  16.       begin
  17.          IsntCorrect := False;
  18.          if (Choice = 'Y') or (Choice = 'y') then
  19.             Console := True
  20.          else Console := False;
  21.       end
  22.          else
  23.          begin
  24.             IsntCorrect := True;
  25.             WriteLn('This is mistake');
  26.          end;
  27.    end;
  28.    choic := Console;
  29. end;
  30.  
  31. function OpenFile : String;
  32. var
  33.    Line : String;
  34.    IsCorrect : Boolean;
  35.    FileIn : TextFile;
  36. begin
  37.    repeat
  38.       WriteLn('Enter file location');
  39.       ReadLn(Line);
  40.          try
  41.             AssignFile(FileIn, Line);
  42.             Reset(FileIn);
  43.             if FileExists(Line) then
  44.                IsCorrect := True
  45.             else
  46.             begin
  47.                WriteLn('Can not open this file. Please enter the name again');
  48.                IsCorrect := False;
  49.             end;
  50.             except
  51.                WriteLn('Can not open this file');
  52.          end;
  53.    until(IsCorrect);
  54.    OpenFile := Line;
  55. end;
  56.  
  57. function EnterText : String;
  58. var Text : String;
  59. begin
  60.    WriteLn('Enter the text. All special characters and numbers such as 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, /, -, _, =, +, @, #, $, %, ^, &, *, (, ) will be deleted.');
  61.    ReadLn(Text);
  62.    EnterText := Text;
  63. end;
  64.  
  65. function EnterFileText : String;
  66. var FileIn : TextFile;
  67.     Text, Line : String;
  68. begin
  69.    Line := OpenFile;
  70.    AssignFile(FileIn, Line);
  71.    Reset(FileIn);
  72.    while (not EOF(FileIn)) do
  73.       Readln(FileIn, Text);
  74.    CloseFile(FileIn);
  75.    EnterFileText := Text;
  76. end;
  77.  
  78. function EditText(Choise : Boolean) : String;
  79. type
  80.      InvalidValues = set of char;
  81. var  Lengt, I : Integer;
  82.      InvalidData, Data : InvalidValues;
  83.      Text : String;
  84. begin
  85.    Data := ['!', ';', ':', ',', '.', '?'];
  86.    InvalidData := ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '"', ']', '}', '}', '[', '/', '-', '_', '=', '+', '@', '#', '$', '%', '^', '&', '*', '(', ')'];
  87.    if Choise then
  88.    Text := EnterText
  89.    else
  90.    Text := EnterFileText;
  91.    Lengt := Length(Text);
  92.    I := 1;
  93.    while (I <= Lengt) do
  94.    begin
  95.       if Text[I] in InvalidData then
  96.       begin
  97.          Delete(Text, I, 1);
  98.          Dec(Lengt);
  99.          Dec(I);
  100.       end;
  101.       if (Text[I] = ' ') and (Text[I - 1] = ' ') then
  102.       begin
  103.          Delete(Text, I, 1);
  104.          Dec(Lengt);
  105.          Dec(I);
  106.       end;
  107.       if (Text[I] in Data) and (Text[I - 1] in Data) then
  108.       begin
  109.          Delete(Text, I, 1);
  110.          Dec(Lengt);
  111.          Dec(I);
  112.       end;
  113.       Inc(I);
  114.    end;
  115.    editText := Text;
  116. end;
  117.  
  118. procedure Encryption (Choice : Boolean);
  119. type
  120.    Values = set of Char;
  121. var FileOut : TextFile;
  122.     Text, Temp : String;
  123.     Lengt, I, Count, Parity : Integer;
  124.     Data : Values;
  125. begin
  126.    Data := ['!', ';', ':', ',', '.', '?'];
  127.    Text := EditText(Choice);
  128.    Lengt := Length(Text);
  129.    Count := 0;
  130.    I := 1;
  131.    while (I <= Lengt) do
  132.    begin
  133.       Count := Count + 1;
  134.       if (Text[I] = ' ')  then
  135.       begin
  136.          Parity := Parity + 1;
  137.          if Parity mod 2 = 0 then
  138.          begin
  139.             Temp := Copy(Text, I - (Count - 1), Count - 1);
  140.             Delete(Text, I - (Count - 1), Count - 1);
  141.             Insert(AnsiUpperCase(Temp), Text, I - (Count - 1));
  142.             Count := 0;
  143.          end
  144.          else
  145.          begin
  146.             Insert('(', Text, I - (Count - 1));
  147.             Insert(')', Text, I + 1);
  148.             Lengt := Lengt + 2;
  149.             I := I + 2;
  150.             Count := 0;
  151.          end;
  152.          if (Text[I + 1] in Data) then Inc(I);
  153.       end;
  154.        if (Text[I] in Data) and (I < Lengt) and (Text[I - 1] <> ' ') then
  155.       begin
  156.          Parity := Parity + 1;
  157.          if Parity mod 2 = 0 then
  158.          begin
  159.             Temp := Copy(Text, I - (Count - 1), Count - 1);
  160.             Delete(Text, I - (Count - 1), Count - 1);
  161.             Insert(AnsiUpperCase(Temp), Text, I - (Count - 1));
  162.             Count := 0;
  163.          end
  164.          else
  165.          begin
  166.             Insert('(', Text, I - (Count - 1));
  167.             Insert(')', Text, I + 1);
  168.             Count := 0;
  169.             Lengt := Lengt + 2;
  170.             I := I + 2;
  171.          end;
  172.          if (Text[I + 1] = ' ') then Inc(I);
  173.       end;
  174.       if (I = Lengt) and (Text[I] in Data) then
  175.       begin
  176.          Parity := Parity + 1;
  177.          if Parity mod 2 = 0 then
  178.          begin
  179.             Temp := Copy(Text, I - (Count - 1), Count - 1);
  180.             Delete(Text, I - (Count - 1), Count - 1);
  181.             Insert(AnsiUpperCase(Temp), Text, I - (Count - 1));
  182.             Count := 0;
  183.          end
  184.          else
  185.          begin
  186.             Lengt := Lengt + 2;
  187.             Insert('(', Text, I - (Count - 1));
  188.             Insert(')', Text, I + 1);
  189.             Count := 0;
  190.          end;
  191.       end
  192.       else
  193.       begin
  194.          if I = Lengt then
  195.          begin
  196.             Parity := Parity + 1;
  197.             if Parity mod 2 = 0 then
  198.             begin
  199.                Temp := Copy(Text, I - Count, Count + 1);
  200.                Delete(Text, I - Count, Count + 1);
  201.                Insert(AnsiUpperCase(Temp), Text, I - Count);
  202.                Count := 0;
  203.             end
  204.             else
  205.             begin
  206.                Insert('(', Text, I - (Count - 1));
  207.                Insert(')', Text, I + 4);
  208.                Lengt := Lengt + 2;
  209.                I := I + 2;
  210.                Count := 0;
  211.             end;
  212.          end
  213.       end;
  214.       Inc(I);
  215.    end;
  216.    WriteLn(Text);
  217.    if Choice = False then
  218.    begin
  219.       AssignFile(FileOut, 'D:\output.txt');
  220.       Rewrite(FileOut);
  221.       Writeln(FileOut,Text);
  222.       CloseFile(FileOut);
  223.    end;
  224.    ReadLn;
  225.    ReadLn;
  226. end;
  227.  
  228. procedure main;
  229. var Choice : Boolean;
  230. begin
  231.    Choice := Choic;
  232.    Encryption(Choice);
  233. end;
  234.  
  235. begin
  236.    WriteLn('This program in the text in every even word replaces all lowercase alphabetic characters with uppercase, and each odd word is enclosed in parentheses');
  237.    Main;
  238. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement