-
Notifications
You must be signed in to change notification settings - Fork 0
/
uPrincipal.pas
151 lines (130 loc) · 3.08 KB
/
uPrincipal.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
unit uPrincipal;
interface
uses
FMX.Controls,
FMX.Controls.Presentation,
FMX.Dialogs,
FMX.Edit,
FMX.Forms,
FMX.Graphics,
FMX.Memo,
FMX.ScrollBox,
FMX.StdCtrls,
FMX.Types,
System.Classes,
System.SysUtils,
System.Types,
System.UITypes,
System.Variants;
type
TfrmPrincipal = class(TForm)
lblTexto: TLabel;
memTexto: TMemo;
memChave: TLabel;
edtChave: TEdit;
cbEmbaralhar: TCheckBox;
btnAdicionar: TButton;
memResultado: TMemo;
procedure btnAdicionarClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmPrincipal: TfrmPrincipal;
function MisturaPalavra(Seed: Integer; Palavra: string): string;
function MisturaFrase(Seed: Integer; Frase: string): string;
implementation
{$R *.fmx}
function MisturaPalavra(Seed: Integer; Palavra: string): string;
var
X, R, Len: Integer;
C: Char;
begin
RandSeed := Seed;
Len := Length(Palavra);
for X := 1 to Len do
begin
R := Random(Len) + 1;
C := Palavra[X];
Palavra[X] := Palavra[R];
Palavra[R] := C;
end;
Result := Palavra;
end;
function MisturaFrase(Seed: Integer; Frase: string): string;
var
Palavras: TStringList;
I: Integer;
Ponto: Boolean;
begin
if Frase = '.' then
Result := '.'
else
begin
Palavras := TStringList.Create;
try
I := Pos('.', Frase);
if I > 0 then
begin
Delete(Frase, I, 1);
Ponto := True;
end
else
Ponto := False;
I := Pos(' ', Frase);
while I > 0 do
begin
Palavras.Add(Copy(Frase, 1, I - 1));
Delete(Frase, 1, I);
I := Pos(' ', Frase)
end;
Palavras.Add(Frase);
if frmPrincipal.cbEmbaralhar.IsChecked then
begin
var
X, R, Len: Integer;
var
C: string;
RandSeed := Seed;
Len := Palavras.Count;
for X := 0 to Len - 1 do
begin
R := Random(Len);
C := Palavras[X];
Palavras[X] := Palavras[R];
Palavras[R] := C;
end;
end;
Result := MisturaPalavra(Seed, Palavras[0]);
if Palavras.Count > 1 then
for I := 1 to Palavras.Count - 1 do
Result := Result + ' ' + MisturaPalavra(Seed, Palavras[I]);
if Ponto then
Result := Result + '.'
finally
FreeAndNil(Palavras);
end;
end;
end;
procedure TfrmPrincipal.btnAdicionarClick(Sender: TObject);
var
Frase, TextoInicial, TextoFinal: string;
I, Seed: Integer;
begin
TextoInicial := memTexto.Text;
Seed := StrToIntDef(edtChave.Text, 5);
I := Pos('.', TextoInicial);
while I > 0 do
begin
Frase := Copy(TextoInicial, 1, I);
Delete(TextoInicial, 1, I);
TextoFinal := TextoFinal + MisturaFrase(Seed, Frase);
I := Pos('.', TextoInicial);
end;
if Length(TextoInicial) > 0 then
TextoFinal := TextoFinal + MisturaFrase(Seed, TextoInicial);
memResultado.Lines.Add(TextoFinal)
end;
end.