Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.67 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, Menus;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     GroupBox1: TGroupBox;
  12.     mm_Out: TMemo;
  13.     mm_In: TMemo;
  14.     MainMenu1: TMainMenu;
  15.     N1: TMenuItem;
  16.     N2: TMenuItem;
  17.     N3: TMenuItem;
  18.     N4: TMenuItem;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     N5: TMenuItem;
  22.     procedure N2Click(Sender: TObject);
  23.     procedure N3Click(Sender: TObject);
  24.     procedure N4Click(Sender: TObject);
  25.     procedure N5Click(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.   FIn, FOut : TextFile;
  35.  
  36. implementation
  37.  
  38. {$R *.dfm}
  39.  
  40.  
  41.  
  42. procedure TForm1.N2Click(Sender: TObject);
  43.  
  44. const
  45.   WordMax = 30;
  46. type
  47.   StringList = array[1..WordMax] of String;
  48. var
  49.   HandlingText, OutString : String;
  50.   DiffWords : StringList;
  51.   I, Count : Integer;
  52.  
  53.  
  54. procedure SplitText(Text : String; var Mass : StringList; var N : Integer);
  55. var
  56.   DiffWords : StringList;
  57.   TempText, Word : String;
  58.   Position, Len: Integer;
  59. begin
  60.   TempText := Text;
  61.   Position := Pos(' ', TempText);
  62.   N := 1;
  63.   while Len > 0 do
  64.   begin
  65.     if Position = 0 then
  66.     begin
  67.       Word := TempText;
  68.       TempText := '';
  69.     end
  70.     else
  71.     begin
  72.       Word := Copy(TempText, 1, Position - 1);
  73.       TempText := Copy(TempText, Position + 1, Length(TempText) - Position);
  74.     end;
  75.  
  76.     if Length(Word) > 10 then
  77.       ShowMessage('Максимальная длина слова -- 10. Слово"' + Word + '" Будет проигнорировано.')
  78.     else
  79.     begin
  80.       Mass[N] := Word;
  81.       N := N + 1;
  82.     end;
  83.     Len := Length(TempText);
  84.     Position := Pos(' ', TempText);
  85.     if N > 30 then
  86.     begin
  87.       Len := 0;
  88.       ShowMessage('Максимальное количество слов -- 30. Слова, следующие за 30-м будут проигнорированы.');
  89.     end;
  90.   end;
  91.   N := N - 1;
  92. end;
  93. procedure HandleText(Text : String; var DiffWords : StringList; var J : Integer);
  94. var
  95.   Words : StringList;
  96.   LastWord,x : String;
  97.   Count, I, K, Sum : Integer;
  98. begin
  99.   Text := StringReplace(Text, '  ', ' ', [rfReplaceAll]);
  100.   SplitText(Text, Words, Count);
  101.  
  102.   LastWord := StringReplace(Words[Count], '.', '', [rfReplaceAll]);
  103.  
  104.   J := 1;
  105.   for I := 1 to Count - 1 do
  106.     if Words[I] <> LastWord then
  107.     begin
  108.     Sum := 0;
  109.       for K := 1 to Length(Words[I]) do
  110.       begin
  111.         if Words[I][K] in ['0'..'9'] then
  112.           Sum := Sum + StrToInt(Words[I][K]);
  113.       end;
  114.       if Sum <> 0 then Words[I] := Words[I] + IntToStr(Sum);
  115.  
  116.       if Length(Words[I]) > 0 then
  117.       begin
  118.         DiffWords[J] := Words[I];
  119.         J := J + 1;
  120.       end;
  121.     end;
  122.   J := J - 1;
  123. end;
  124.  
  125. begin
  126.   Reset(FIn);
  127.   Read(FIn, HandlingText);
  128.   CloseFile(FIn);
  129.   mm_In.Text := HandlingText;
  130.   HandleText(HandlingText, DiffWords, Count);
  131.   OutString := '';
  132.   for I := 1 to Count do
  133.   OutString := Concat(OutString, DiffWords[I], ' ');
  134.   mm_Out.Text := OutString;
  135.   Rewrite(FOut);
  136.   Write(FOut, OutString);
  137.   CloseFile(FOut);
  138. end;
  139.  
  140. procedure TForm1.N3Click(Sender: TObject);
  141. begin
  142.   mm_In.Text := '';
  143.   mm_Out.Text := '';
  144. end;
  145.  
  146. procedure TForm1.N4Click(Sender: TObject);
  147. begin
  148. Close;
  149. end;
  150.  
  151. procedure TForm1.N5Click(Sender: TObject);
  152. begin
  153.  mm_in.Lines.SaveToFile('InFile.txt'); //Сохраняем входной текст в файл
  154. end;
  155.  
  156. initialization
  157. AssignFile(FIn, 'InFile.txt');   //Инициализируем входной файл
  158. AssignFile(FOut, 'OutFile.txt');  //Инициализируем выходной файл
  159. Finalization
  160.  
  161. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement