Advertisement
ksyshshot

курсач

May 28th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 15.65 KB | Source Code | 0 0
  1. unit MainForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, System.Actions,
  8.   Vcl.ActnList, Vcl.WinXCalendars, Vcl.WinXCtrls, Vcl.ExtCtrls, Vcl.Grids,
  9.   Vcl.CheckLst, UnitTranslateFromRussian, UnitTranslateFromEnglish,
  10.   UnitEditDictionary, Vcl.ValEdit, UnitFavorites, UnitFlashCards,
  11.   Vcl.Imaging.pngimage;
  12.  
  13. const
  14.     FILE_NAME = 'Dictionary.rcd';
  15.     FILE_FAVORITES = 'Fav.rcd';
  16.     TEMP_FILE_NAME = 'Temp.rcd';
  17.     FILE_FLASHCARDS = 'FlashCards.rcd';
  18.  
  19. type
  20.   TPartsOfSpeech = (TNoun, TVerb, TAdjective);
  21.   EnglStr = String[91];
  22.   RussStr = String[51];
  23.   TWord = record
  24.       Russian: RussStr;
  25.       English: EnglStr;
  26.       PartOfSpeech: TPartsOfSpeech;
  27.   end;
  28.   TPElem = ^TElem;
  29.   TElem = record
  30.       StartPage: EnglStr;
  31.       FlipPage: EnglStr;
  32.       Next: TPElem;
  33.       Prev: TPElem;
  34.   end;
  35.   TFormMenu = class(TForm)
  36.     PanelTabs: TPanel;
  37.     PanelWelcome: TPanel;
  38.     LabelWelcome: TLabel;
  39.     PanelFrame: TPanel;
  40.     PanelDictionaries: TPanel;
  41.     LabelDictionary: TLabel;
  42.     ButtonTranslateFromRussian: TButton;
  43.     ButtonTranslateFromEnglish: TButton;
  44.     Panel1: TPanel;
  45.     PanelTools: TPanel;
  46.     LabelTools: TLabel;
  47.     ButtonFlashCards: TButton;
  48.     ButtonChangeDictionary: TButton;
  49.     PanelTopBorderOfFrame: TPanel;
  50.     PanelBottomBorderOfFrame: TPanel;
  51.     PanelLeftBorderOfFrame: TPanel;
  52.     PanelRightBorderOfFrame: TPanel;
  53.     ButtonFavorites: TButton;
  54.     FrameTranslateFromRussian: TFrameTranslateFromRussian;
  55.     FrameTranslateFromEnglish: TFrameTranslateFromEnglish;
  56.     FrameEditDictionary: TFrameEditDictionary;
  57.     FrameFavorites: TFrameFavorites;
  58.     FrameFlashCards: TFrameFlashCards;
  59.     Image1: TImage;
  60.     procedure ButtonTranslateFromRussianClick(Sender: TObject);
  61.     procedure ButtonTranslateFromEnglishClick(Sender: TObject);
  62.     procedure ButtonFlashCardsClick(Sender: TObject);
  63.     procedure ButtonChangeDictionaryClick(Sender: TObject);
  64.     procedure ButtonFavoritesClick(Sender: TObject);
  65.   private
  66.     { Private declarations }
  67.   public
  68.     procedure WriteToFavorites(NumbRow: Integer; Dictionary: TValueListEditor; Path: String);
  69.     procedure ClearDictionary(var Dict: TValueListEditor; Count: Integer);
  70.     function ChooseArticle(Word: String): String;
  71.     function GetFinalAppearance(Part: TPartsOfSpeech; Word: String): String;
  72.     function IsRowEmpty(Dictionary: TValueListEditor; NumbRow: Integer): Boolean;
  73.     procedure DeleteWord(var Dictionary: TValueListEditor);
  74.     procedure SaveDictionaryToFile(Dictionary: TValueListEditor; Path: String);
  75.     function RecognizePartOfSpeech(var ResearchWord: EnglStr): TPartsOfSpeech;
  76.     function Search(Dictionary: TValueListEditor; var NewDictionary: TValueListEditor; SoughtFor: String): Boolean;
  77.     function IsWordInDictionary(Dictionary: TValueListEditor; SearchStr: String): Boolean;
  78.     function GetPartOfSpeech(var EnglishWord: EnglStr; var RadGroup: TRadioGroup; IsForAdding: Boolean): TPartsOfSpeech;
  79.   end;
  80.  
  81. var
  82.   FormMenu: TFormMenu;
  83.  
  84. implementation
  85.  
  86. {$R *.dfm}
  87.  
  88. uses UnitError;
  89.  
  90. function TFormMenu.GetPartOfSpeech(var EnglishWord: EnglStr; var RadGroup: TRadioGroup; IsForAdding: Boolean): TPartsOfSpeech;
  91. var
  92.     Part: TPartsOfSpeech;
  93. begin
  94.     if (IsForAdding) then
  95.     begin
  96.         case RadGroup.ItemIndex of
  97.             0:
  98.                 Part := TVerb;
  99.             1:
  100.                 Part := TNoun;
  101.             2:
  102.                 Part := TAdjective;
  103.         end;
  104.     end
  105.     else
  106.     begin
  107.         Part := MainForm.FormMenu.RecognizePartOfSpeech(EnglishWord);
  108.         case (Part) of
  109.             TNoun:
  110.                 RadGroup.ItemIndex := 1;
  111.             TVerb:
  112.                 RadGroup.ItemIndex := 0;
  113.             TAdjective:
  114.                 RadGroup.ItemIndex := 2;
  115.         end;
  116.     end;
  117.     GetPartOfSpeech := Part;
  118. end;
  119.  
  120. procedure TFormMenu.DeleteWord(var Dictionary: TValueListEditor);
  121. var
  122.     CurrRow: Integer;
  123. begin
  124.     CurrRow := Dictionary.Row;
  125.     Dictionary.Row := 1;
  126.     Dictionary.DeleteRow(CurrRow);
  127. end;
  128.  
  129. function TFormMenu.IsRowEmpty(Dictionary: TValueListEditor; NumbRow: Integer): Boolean;
  130. begin
  131.     if (Dictionary.Keys[NumbRow] <> '') and (Dictionary.Cells[1, NumbRow] <> '') then
  132.     begin
  133.         IsRowEmpty := false;
  134.     end
  135.     else
  136.     begin
  137.         IsRowEmpty := true;
  138.     end;
  139. end;
  140.  
  141. function TFormMenu.ChooseArticle(Word: String): String;
  142. begin
  143.     if Word[1] in ['a', 'e', 'u', 'i', 'o'] then
  144.     begin
  145.         ChooseArticle := 'an';
  146.     end
  147.     else
  148.     begin
  149.         ChooseArticle := 'a';
  150.     end;
  151. end;
  152.  
  153. function TFormMenu.GetFinalAppearance(Part: TPartsOfSpeech; Word: String): String;
  154. var
  155.     Answer: String;
  156. begin
  157.     case Part of
  158.         TNoun:
  159.             Answer := ChooseArticle(Word) + ' ' + Word;
  160.         TVerb:
  161.             Answer := 'to' + ' ' + Word;
  162.         TAdjective:
  163.             Answer := Word;
  164.     end;
  165.     GetFinalAppearance := Answer;
  166. end;
  167.  
  168. procedure TFormMenu.ClearDictionary(var Dict: TValueListEditor; Count: Integer);
  169. var
  170.     I: Integer;
  171. begin
  172.     if (Dict.RowCount > 1) then
  173.     begin
  174.         if not(IsRowEmpty(Dict, (Dict.RowCount - 1))) then
  175.         begin
  176.             Dict.Row := 1;
  177.             for I := Count downto 1 do
  178.             begin
  179.                 Dict.DeleteRow(I);
  180.             end;
  181.         end;
  182.     end;
  183. end;
  184.  
  185. procedure TFormMenu.WriteToFavorites(NumbRow: Integer; Dictionary: TValueListEditor; Path: String);
  186. var
  187.     F: File of TWord;
  188.     AddedWord: TWord;
  189.     EnglishWord: EnglStr;
  190. begin
  191.     AddedWord.Russian := Dictionary.Keys[NumbRow];
  192.     EnglishWord := Dictionary.Cells[1, NumbRow];
  193.     AddedWord.PartOfSpeech := MainForm.FormMenu.RecognizePartOfSpeech(EnglishWord);
  194.     AddedWord.English := EnglishWord;
  195.     AssignFile(F, Path);
  196.     try
  197.         try
  198.             Reset(F);
  199.             Seek(F, FileSize(F));
  200.             Write(F, AddedWord);
  201.         finally
  202.             CloseFile(F);
  203.         end;
  204.     except
  205.         UnitError.FormError.LabelError.Caption := 'Возникла ошибка при записи нового слова!';
  206.         UnitError.FormError.ShowModal;
  207.         UnitError.FormError.LabelError.Caption := '';
  208.     end;
  209. end;
  210.  
  211. function TFormMenu.IsWordInDictionary(Dictionary: TValueListEditor; SearchStr: String): Boolean;
  212. var
  213.     I: Integer;
  214.     DictSize: Integer;
  215.     IsFound: Boolean;
  216. begin
  217.     IsFound := false;
  218.     DictSize := Dictionary.RowCount;
  219.     I := 1;
  220.     while (I < DictSize) and not(IsFound) do
  221.     begin
  222.         if (Dictionary.Cells[0, I] = SearchStr) or (Dictionary.Cells[1, I] = SearchStr) then
  223.         begin
  224.             IsFound := true;
  225.         end;
  226.         Inc(I);
  227.     end;
  228.     IsWordInDictionary := IsFound;
  229. end;
  230.  
  231. function TFormMenu.Search(Dictionary: TValueListEditor; var NewDictionary: TValueListEditor; SoughtFor: String): Boolean;
  232. var
  233.     DictSize: Integer;
  234.     I: Integer;
  235.     CurrWord: String;
  236.     IsFound: Boolean;
  237. begin
  238.     IsFound := false;
  239.     DictSize := Dictionary.RowCount;
  240.     I := 1;
  241.     while (I < DictSize) do
  242.     begin
  243.         CurrWord := Dictionary.Cells[0, I];
  244.         if (Length(CurrWord) >= Length(SoughtFor)) then
  245.         begin
  246.             if (Pos(SoughtFor, CurrWord) <> 0) then
  247.             begin
  248.                 IsFound := true;
  249.                 if not(IsWordInDictionary(NewDictionary, CurrWord)) then
  250.                 begin
  251.                     NewDictionary.InsertRow(Dictionary.Cells[0, I], Dictionary.Cells[0, I], true);
  252.                 end;
  253.             end;
  254.         end;
  255.         Inc(I);
  256.     end;
  257.     Search := IsFound;
  258. end;
  259.  
  260. function TFormMenu.RecognizePartOfSpeech(var ResearchWord: EnglStr): TPartsOfSpeech;
  261. var
  262.     WordStart: String;
  263. begin
  264.     RecognizePartOfSpeech := TAdjective;
  265.     if (length(ResearchWord) > 3) then
  266.     begin
  267.         WordStart := Copy(ResearchWord, 1, 2);
  268.         if (WordStart = 'a ') then
  269.         begin
  270.             Delete(ResearchWord, 1, 2);
  271.             RecognizePartOfSpeech := TNoun;
  272.         end;
  273.         if (WordStart = 'an') and (Copy(ResearchWord, 3, 1) = ' ') then
  274.         begin
  275.             Delete(ResearchWord, 1, 3);
  276.             RecognizePartOfSpeech := TNoun;
  277.         end;
  278.         if (WordStart = 'to') and (Copy(ResearchWord, 3, 1) = ' ') then
  279.         begin
  280.             Delete(ResearchWord, 1, 3);
  281.             RecognizePartOfSpeech := TVerb;
  282.         end;
  283.     end
  284.     else
  285.     begin
  286.         RecognizePartOfSpeech := TAdjective;
  287.     end;
  288. end;
  289.  
  290. function LoadDictionaryFromFile(var Dictionary: TValueListEditor; IsFromEnglish: Boolean; Path: String): Boolean;
  291. var
  292.     NewWord: TWord;
  293.     F: File of TWord;
  294.     Size, I: Integer;
  295.     Translation, ForeignWord: String;
  296. begin
  297.     AssignFile(F, Path);
  298.     try
  299.         Reset(F);
  300.         Seek(F, 0);
  301.         Size := FileSize(F);
  302.         I := 0;
  303.         while (I < Size) do
  304.         begin
  305.             Read(F, NewWord);
  306.             if (IsFromEnglish) then
  307.             begin
  308.                 ForeignWord := FormMenu.GetFinalAppearance(NewWord.PartOfSpeech, NewWord.English);
  309.                 Translation := NewWord.Russian;
  310.             end
  311.             else
  312.             begin
  313.                 Translation := FormMenu.GetFinalAppearance(NewWord.PartOfSpeech, NewWord.English);
  314.                 ForeignWord := NewWord.Russian;
  315.             end;
  316.             Dictionary.InsertRow(ForeignWord, Translation, True);
  317.             Inc(I);
  318.         end;
  319.         CloseFile(F);
  320.         LoadDictionaryFromFile := true;
  321.     except
  322.         UnitError.FormError.LabelError.Caption := 'Возникла ошибка при загрузке словаря из файла! Проверьте наличие файла: ' + path;
  323.         UnitError.FormError.ShowModal;
  324.         UnitError.FormError.LabelError.Caption := '';
  325.         FormMenu.ClearDictionary(Dictionary, (I + 1));
  326.         LoadDictionaryFromFile := false;
  327.     end;
  328. end;
  329.  
  330. procedure TFormMenu.SaveDictionaryToFile(Dictionary: TValueListEditor; Path: String);
  331. var
  332.     AddedWord: TWord;
  333.     TempF: File of TWord;
  334.     RecordsCount, I: Integer;
  335.     EnglishWord: EnglStr;
  336. begin
  337.     AssignFile(TempF, TEMP_FILE_NAME);
  338.     try
  339.         I := 1;
  340.         try
  341.             Rewrite(TempF);
  342.             while (I < Dictionary.RowCount) do
  343.             begin
  344.                 if not(MainForm.FormMenu.IsRowEmpty(Dictionary, Dictionary.Row)) then
  345.                 begin
  346.                     AddedWord.Russian := Dictionary.Keys[I];
  347.                     EnglishWord := Dictionary.Cells[1, I];
  348.                     AddedWord.PartOfSpeech := RecognizePartOfSpeech(EnglishWord);
  349.                     AddedWord.English := EnglishWord;
  350.                     Write(TempF, AddedWord);
  351.                 end;
  352.                 Inc(I);
  353.             end;
  354.         finally
  355.             CloseFile(TempF);
  356.         end;
  357.         DeleteFile(Path);
  358.         RenameFile(TEMP_FILE_NAME, Path);
  359.     except
  360.         UnitError.FormError.LabelError.Caption := 'Возникла ошибка при записи словаря в файл! Проверьте наличие файла: ' + path;
  361.         UnitError.FormError.ShowModal;
  362.         UnitError.FormError.LabelError.Caption := '';
  363.     end;
  364. end;
  365.  
  366.  
  367. function CheckFileExistence(Path: String): Boolean;
  368. var
  369.     F: File of TWord;
  370.     IsCorrect: Boolean;
  371. begin
  372.     IsCorrect := true;
  373.     try
  374.         AssignFile(F, Path);
  375.         try
  376.             Reset(F);
  377.         finally
  378.             CloseFile(F);
  379.         end;
  380.     except
  381.         IsCorrect := false;
  382.         UnitError.FormError.LabelError.Caption := 'Возникла ошибка при записи словаря в файл! Проверьте наличие файла: ' + path;
  383.         UnitError.FormError.ShowModal;
  384.         UnitError.FormError.LabelError.Caption := '';
  385.     end;
  386.     CheckFileExistence := IsCorrect;
  387. end;
  388.  
  389. procedure TFormMenu.ButtonChangeDictionaryClick(Sender: TObject);
  390. begin
  391.     FrameFlashCards.Enabled := false;
  392.     FrameFlashCards.Visible := false;
  393.     FrameTranslateFromRussian.Enabled := false;
  394.     FrameTranslateFromRussian.Visible := false;
  395.     FrameTranslateFromEnglish.Enabled := false;
  396.     FrameTranslateFromEnglish.Visible := false;
  397.     FrameFavorites.Visible := false;
  398.     FrameFavorites.Enabled := false;
  399.     ClearDictionary(FrameEditDictionary.Dictionary, (FrameEditDictionary.Dictionary.RowCount - 1));
  400.     if (CheckFileExistence(FILE_NAME)) and (LoadDictionaryFromFile(FrameEditDictionary.Dictionary, false, FILE_NAME)) then
  401.     begin
  402.         FrameEditDictionary.Visible := true;
  403.         FrameEditDictionary.Enabled := true;
  404.     end;
  405. end;
  406.  
  407. procedure TFormMenu.ButtonFavoritesClick(Sender: TObject);
  408. begin
  409.     FrameFlashCards.Enabled := false;
  410.     FrameFlashCards.Visible := false;
  411.     FrameTranslateFromRussian.Enabled := false;
  412.     FrameTranslateFromRussian.Visible := false;
  413.     FrameTranslateFromEnglish.Enabled := false;
  414.     FrameTranslateFromEnglish.Visible := false;
  415.     FrameEditDictionary.Visible := false;
  416.     FrameEditDictionary.Enabled := false;
  417.     ClearDictionary(FrameFavorites.Dictionary, (FrameFavorites.Dictionary.RowCount - 1));
  418.     if (CheckFileExistence(FILE_FAVORITES)) and (LoadDictionaryFromFile(FrameFavorites.Dictionary, false, FILE_FAVORITES)) then
  419.     begin
  420.         FrameFavorites.Visible := true;
  421.         FrameFavorites.Enabled := true;
  422.     end;
  423. end;
  424.  
  425. procedure TFormMenu.ButtonFlashCardsClick(Sender: TObject);
  426. begin
  427.     FrameTranslateFromRussian.Enabled := false;
  428.     FrameTranslateFromRussian.Visible := false;
  429.     FrameTranslateFromEnglish.Enabled := false;
  430.     FrameTranslateFromEnglish.Visible := false;
  431.     FrameEditDictionary.Visible := false;
  432.     FrameEditDictionary.Enabled := false;
  433.     FrameFavorites.Visible := false;
  434.     FrameFavorites.Enabled := false;
  435.     ClearDictionary(FrameFlashCards.DictionaryChangeWordSet, (FrameFlashCards.DictionaryChangeWordSet.RowCount - 1));
  436.     if (CheckFileExistence(FILE_FLASHCARDS)) and (LoadDictionaryFromFile(FrameFlashCards.DictionaryChangeWordSet, false, FILE_FLASHCARDS)) then
  437.     begin
  438.         FrameFlashCards.Enabled := true;
  439.         FrameFlashCards.Visible := true;
  440.     end;
  441. end;
  442.  
  443. procedure TFormMenu.ButtonTranslateFromEnglishClick(Sender: TObject);
  444. var
  445.     Count: Integer;
  446. begin
  447.     FrameFlashCards.Enabled := false;
  448.     FrameFlashCards.Visible := false;
  449.     FrameTranslateFromRussian.Enabled := false;
  450.     FrameTranslateFromRussian.Visible := false;
  451.     FrameEditDictionary.Visible := false;
  452.     FrameEditDictionary.Enabled := false;
  453.     FrameFavorites.Visible := false;
  454.     FrameFavorites.Enabled := false;
  455.     Count := FrameTranslateFromEnglish.Dictionary.RowCount;
  456.     Dec(Count);
  457.     ClearDictionary(FrameTranslateFromEnglish.Dictionary, Count);
  458.     if (CheckFileExistence(FILE_NAME)) and (LoadDictionaryFromFile(FrameTranslateFromEnglish.Dictionary, true, FILE_NAME)) then
  459.     begin
  460.         FrameTranslateFromEnglish.Enabled := true;
  461.         FrameTranslateFromEnglish.Visible := true;
  462.     end;
  463. end;
  464.  
  465. procedure TFormMenu.ButtonTranslateFromRussianClick(Sender: TObject);
  466. begin
  467.     FrameFlashCards.Enabled := false;
  468.     FrameFlashCards.Visible := false;
  469.     FrameEditDictionary.Visible := false;
  470.     FrameEditDictionary.Enabled := false;
  471.     FrameTranslateFromEnglish.Enabled := false;
  472.     FrameTranslateFromEnglish.Visible := false;
  473.     FrameFavorites.Visible := false;
  474.     FrameFavorites.Enabled := false;
  475.     ClearDictionary(FrameTranslateFromRussian.Dictionary, (FrameTranslateFromRussian.Dictionary.RowCount - 1));
  476.     if (CheckFileExistence(FILE_NAME)) and (LoadDictionaryFromFile(FrameTranslateFromRussian.Dictionary, false, FILE_NAME)) then
  477.     begin
  478.         FrameTranslateFromRussian.Enabled := true;
  479.         FrameTranslateFromRussian.Visible := true;
  480.     end;
  481. end;
  482.  
  483. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement