home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 September
/
Chip_2002-09_cd1.bin
/
zkuste
/
delphi
/
kompon
/
d6
/
YPPARSER.ZIP
/
Generator
/
MainForm.pas
< prev
Wrap
Pascal/Delphi Source File
|
2002-06-16
|
14KB
|
388 lines
{********************************************************}
{ }
{ Generator }
{ IMPORTANT-READ CAREFULLY: }
{ }
{ This End-User License Agreement is a legal }
{ agreement between you (either an individual }
{ or a single entity) and Pisarev Yuriy for }
{ the software product identified above, which }
{ includes computer software and may include }
{ associated media, printed materials, and "online" }
{ or electronic documentation ("SOFTWARE PRODUCT"). }
{ By installing, copying, or otherwise using the }
{ SOFTWARE PRODUCT, you agree to be bound by the }
{ terms of this LICENSE AGREEMENT. }
{ }
{ If you do not agree to the terms of this }
{ LICENSE AGREEMENT, do not install or use }
{ the SOFTWARE PRODUCT. }
{ }
{ License conditions }
{ }
{ No part of the software or the manual may be }
{ multiplied, disseminated or processed in any }
{ way without the written consent of Pisarev }
{ Yuriy. Violations of these conditions will be }
{ prosecuted in every case. }
{ }
{ The use of the software is done at your own }
{ risk. The manufacturer and developer accepts }
{ no liability for any damages, either as direct }
{ or indirect consequence of the use of this }
{ product or software. }
{ }
{ Only observance of these conditions allows you }
{ to use the hardware and software in your computer }
{ system. }
{ }
{ All rights reserved. }
{ Copyright 2002 Pisarev Yuriy }
{ }
{ yuriy_mbox@hotmail.com }
{ }
{********************************************************}
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ToolWin, ActnMan, ActnCtrls, StdCtrls, ActnList, ComCtrls,
StdActns, ImgList, ActnMenus, Menus, DataEditor, Math, ExtCtrls, AppEvnts,
ExtActns;
type
TTextInfo = record
Text: string;
ElementsCount: Integer;
end;
TFunction = record
FunctionName: string;
RequireValue1, RequireValue2: Boolean;
end;
TFunctions = array of TFunction;
TProbability = 10..90;
TMain = class(TForm)
ActionManager1: TActionManager;
ImageList1: TImageList;
RichEdit: TRichEdit;
HelpAbout: TAction;
ActionMainMenuBar1: TActionMainMenuBar;
PopupMenu1: TPopupMenu;
ServiceGenerate: TAction;
StatusBar: TStatusBar;
Panel1: TPanel;
gbSettings: TGroupBox;
tbElementsCount: TTrackBar;
ServiceExecute: TAction;
ApplicationEvents1: TApplicationEvents;
Label1: TLabel;
rgFunctions: TRadioGroup;
tbEmbeddingsFactor: TTrackBar;
Label2: TLabel;
EditCut: TEditCut;
EditCopy: TEditCopy;
EditPaste: TEditPaste;
EditSelectAll: TEditSelectAll;
EditUndo: TEditUndo;
EditDelete: TEditDelete;
FileOpen: TFileOpen;
FileSaveAs: TFileSaveAs;
FileExit: TFileExit;
FileSave: TAction;
FilePrint: TAction;
Undo1: TMenuItem;
N1: TMenuItem;
Cut1: TMenuItem;
Copy1: TMenuItem;
Paste1: TMenuItem;
Delete1: TMenuItem;
N2: TMenuItem;
SelectAll1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ApplicationEvents1Hint(Sender: TObject);
procedure ServiceGenerateExecute(Sender: TObject);
procedure ServiceExecuteExecute(Sender: TObject);
procedure FileOpenAccept(Sender: TObject);
procedure FileSaveExecute(Sender: TObject);
procedure FileSaveUpdate(Sender: TObject);
procedure FileSaveAsAccept(Sender: TObject);
procedure FilePrintExecute(Sender: TObject);
procedure tbElementsCountChange(Sender: TObject);
procedure rgFunctionsClick(Sender: TObject);
procedure HelpAboutExecute(Sender: TObject);
procedure tbEmbeddingsFactorChange(Sender: TObject);
private
FMax: Integer;
FMin: Integer;
FNumbers: string;
FFileName: string;
FDataEditor: TDataEditor;
FFunctions: TFunctions;
FEmbeddingsFactor: TProbability;
protected
property Functions: TFunctions read FFunctions write FFunctions;
public
function Generate(Min, Max, EmbeddingsFactor: Integer;
Functions: TFunctions; Numbers: string): TTextInfo;
property FileName: string read FFileName write FFileName;
property DataEditor: TDataEditor read FDataEditor write FDataEditor;
property Numbers: string read FNumbers write FNumbers;
property Max: Integer read FMax write FMax;
property Min: Integer read FMin write FMin;
property EmbeddingsFactor: TProbability read FEmbeddingsFactor
write FEmbeddingsFactor;
end;
const
sNumbers = '0123456789';
sStandardNumbers = '123456789';
Links: array[0..2] of string = ('', '-', '+');
MinElementsCount = 5;
StandardFunctions: array[0..1] of TFunction = (
(FunctionName: '*'; RequireValue1: True; RequireValue2: True),
(FunctionName: '/'; RequireValue1: True; RequireValue2: True));
resourcestring
MenuFileName = 'Menu.dat';
var
Main: TMain;
implementation
{$R *.dfm}
{ TMain }
function TMain.Generate(Min, Max, EmbeddingsFactor: Integer;
Functions: TFunctions; Numbers: string): TTextInfo;
function SubCode(Count: Integer; var TextInfo: TTextInfo): Boolean;
begin
Result := (Count >= MinElementsCount) and
(EmbeddingsFactor >= Random(100));
if Result then TextInfo := Generate(MinElementsCount,
Count, EmbeddingsFactor, Functions, Numbers);
end;
var
I, J, NumbersCount: Integer;
TextInfo: TTextInfo;
S1, S2: string;
begin
FillChar(Result, SizeOf(Result), 0);
NumbersCount := Length(Numbers);
J := Min + Random(Max - Min + 1);
while Result.ElementsCount <= J do begin
I := Random(Length(Functions));
with Functions[I] do
if RequireValue1 and RequireValue2 then begin
if Result.Text = '' then begin
S1 := Links[Random(2)] + Numbers[1 + Random(NumbersCount)];
Inc(Result.ElementsCount);
end else if SubCode(J - Result.ElementsCount, TextInfo) then begin
S1 := Links[1 + Random(2)] + '(' + TextInfo.Text + ')';
Inc(Result.ElementsCount, TextInfo.ElementsCount);
end else begin
S1 := Links[1 + Random(2)] + Numbers[1 + Random(NumbersCount)];
Inc(Result.ElementsCount);
end;
if SubCode(J - Result.ElementsCount, TextInfo) then begin
S2 := '(' + TextInfo.Text + ')';
Inc(Result.ElementsCount, TextInfo.ElementsCount);
end else begin
S2 := Numbers[1 + Random(NumbersCount)];
Inc(Result.ElementsCount);
end;
Result.Text := Result.Text + S1 + FunctionName + S2;
Inc(Result.ElementsCount);
end else if not RequireValue1 and RequireValue2 then begin
if Result.Text = '' then S1 := Links[Random(2)]
else S1 := Links[1 + Random(2)];
if SubCode(J - Result.ElementsCount, TextInfo) then begin
S2 := '(' + TextInfo.Text + ')';
Inc(Result.ElementsCount, TextInfo.ElementsCount);
end else begin
S2 := Numbers[1 + Random(NumbersCount)];
Inc(Result.ElementsCount);
end;
Result.Text := Result.Text + S1 + FunctionName + S2;
Inc(Result.ElementsCount);
end else if RequireValue1 and not RequireValue2 then begin
if Result.Text = '' then begin
S1 := Links[Random(2)] + Numbers[1 + Random(NumbersCount)];
Inc(Result.ElementsCount);
end else if SubCode(J - Result.ElementsCount, TextInfo) then begin
S1 := Links[1 + Random(2)] + '(' + TextInfo.Text + ')';
Inc(Result.ElementsCount, TextInfo.ElementsCount);
end else begin
S1 := Links[1 + Random(2)] + Numbers[1 + Random(NumbersCount)];
Inc(Result.ElementsCount);
end;
Result.Text := Result.Text + S1 + FunctionName;
Inc(Result.ElementsCount);
end else begin
if Result.Text = '' then S1 := Links[Random(2)]
else S1 := Links[1 + Random(2)];
Result.Text := Result.Text + S1 + FunctionName;
Inc(Result.ElementsCount);
end;
end;
end;
procedure TMain.FormCreate(Sender: TObject);
begin
FDataEditor := TDataEditor.Create(Self);
with ActionManager1 do begin
FileName := ExtractFilePath(Application.ExeName) + MenuFileName;
if FileExists(FileName) then LoadFromFile(FileName);
end;
rgFunctionsClick(nil);
tbElementsCountChange(nil);
tbEmbeddingsFactorChange(nil);
Randomize;
end;
procedure TMain.FormDestroy(Sender: TObject);
begin
FFunctions := nil;
end;
procedure TMain.ApplicationEvents1Hint(Sender: TObject);
begin
if Length(Application.Hint) > 0 then begin
StatusBar.SimplePanel := True;
StatusBar.SimpleText := Application.Hint
end else StatusBar.SimplePanel := False;
end;
procedure TMain.ServiceGenerateExecute(Sender: TObject);
var
TextInfo: TTextInfo;
Value, TickCount: Double;
begin
Screen.Cursor := crHourGlass;
try
with FDataEditor do TextInfo := Generate(FMin, FMax,
FEmbeddingsFactor, FFunctions, FNumbers);
TickCount := GetTickCount;
FDataEditor.StringToNumScript(TextInfo.Text);
TickCount := GetTickCount - TickCount;
StatusBar.Panels[2].Text := Format('Translation: %d sec %d msec',
[Trunc(TickCount / 1000), Trunc(TickCount - Trunc(TickCount / 1000) * 1000)]);
TickCount := GetTickCount;
Value := FDataEditor.ExecuteNum;
TickCount := GetTickCount - TickCount;
StatusBar.Panels[3].Text := Format('Execution: %d sec %d msec',
[Trunc(TickCount / 1000), Trunc(TickCount - Trunc(TickCount / 1000) * 1000)]);
StatusBar.Panels[0].Text := Format('Result: %f', [Value]);
StatusBar.Panels[1].Text := Format('Elements count: %d', [TextInfo.ElementsCount]);
RichEdit.Lines.Add(TextInfo.Text);
finally
Screen.Cursor := crDefault;
end;
end;
procedure TMain.ServiceExecuteExecute(Sender: TObject);
var
Value, TickCount: Double;
begin
Screen.Cursor := crHourGlass;
try
TickCount := GetTickCount;
FDataEditor.StringToNumScript(RichEdit.Text);
TickCount := GetTickCount - TickCount;
StatusBar.Panels[2].Text := Format('Translation: %d sec %d msec',
[Trunc(TickCount / 1000), Trunc(TickCount - Trunc(TickCount / 1000) * 1000)]);
TickCount := GetTickCount;
Value := FDataEditor.ExecuteNum;
TickCount := GetTickCount - TickCount;
StatusBar.Panels[3].Text := Format('Execution: %d sec %d msec',
[Trunc(TickCount / 1000), Trunc(TickCount - Trunc(TickCount / 1000) * 1000)]);
StatusBar.Panels[0].Text := Format('Result: %f', [Value]);
StatusBar.Panels[1].Text := '';
finally
Screen.Cursor := crDefault;
end;
end;
procedure TMain.FileOpenAccept(Sender: TObject);
begin
FFileName := FileOpen.Dialog.FileName;
RichEdit.Lines.LoadFromFile(FFileName);
end;
procedure TMain.FileSaveExecute(Sender: TObject);
begin
with FileSaveAs.Dialog do if FileExists(FileName) then
RichEdit.Lines.SaveToFile(FileName);
end;
procedure TMain.FileSaveUpdate(Sender: TObject);
begin
FileSave.Enabled := FileExists(FFileName);
end;
procedure TMain.FileSaveAsAccept(Sender: TObject);
begin
FFileName := FileSaveAs.Dialog.FileName;
RichEdit.Lines.SaveToFile(FFileName);
end;
procedure TMain.FilePrintExecute(Sender: TObject);
begin
RichEdit.Print('');
end;
procedure TMain.tbElementsCountChange(Sender: TObject);
begin
FMin := tbElementsCount.Position * 100;
FMax := tbElementsCount.Position * 100;
end;
procedure TMain.tbEmbeddingsFactorChange(Sender: TObject);
begin
FEmbeddingsFactor := tbEmbeddingsFactor.Position * 10;
end;
procedure TMain.rgFunctionsClick(Sender: TObject);
var
I, J: Integer;
begin
if rgFunctions.ItemIndex = 0 then with FDataEditor do begin
SetLength(FFunctions, Length(NumFunctionsData) - 1);
J := 0;
for I := Low(NumFunctionsData) to High(NumFunctionsData) do
with NumFunctionsData[I] do if PInteger(P)^ = NumReservedID then Inc(J)
else begin
FFunctions[I - J].FunctionName := FunctionName;
FFunctions[I - J].RequireValue1 := RequireValue1;
FFunctions[I - J].RequireValue2 := RequireValue2;
end;
FNumbers := sNumbers;
end else begin
SetLength(FFunctions, Length(StandardFunctions));
for I := Low(StandardFunctions) to High(StandardFunctions) do
FFunctions[I] := StandardFunctions[I];
FNumbers := sStandardNumbers;
end;
end;
procedure TMain.HelpAboutExecute(Sender: TObject);
begin
MessageBox(0, 'Demonstration program "Generator" and parser ' +
'"TDataEditor" are written by Pisarev Yuriy. You can contact ' +
'with me by address: yuriy_mbox@hotmail.com', 'About program', mb_Ok);
end;
end.