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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
S:string='';
ind,pn,Dli,Ind2,PrSc,LvSc:integer;
s2,s1,s3,s4,W1:string;
Fl:boolean;
implementation
{$R *.dfm}
function Filter(h:string):string;
begin
h:=StringReplace(h,'not','/',[rfReplaceAll,rfIgnoreCase]);
h:=StringReplace(h,'and','^',[rfReplaceAll,rfIgnoreCase]);
h:=StringReplace(h,'or','|',[rfReplaceAll,rfIgnoreCase]);
result:=h;
end;
function ChrToBool(h:char):boolean;
begin
if h in['T','t'] then result:=true
else result:=false;
end;
function BoolToStr(h:boolean):char;
begin
if h then result:='T'
else result:='F';
end;
function LogAnswer(h:string):boolean;
var
First,second,redy:boolean;
i,j1,j2:integer;
begin
while pos('(',h)<>0 do
begin
redy:=false;
for i:=1 to length(h)do
begin
if redy then continue;
case h[i] of
'(':j1:=i;
')':begin
j2:=i;
redy:=true;
end;
end;
end;
h:=copy(h,1,j1-1)+BoolToStr(LogAnswer(copy(h,j1+1,j2-j1-1)))+copy(h,j2+1,length(h)-j2+1);
end;
while pos('/',h)<>0 do
begin
i:=pos('/',h);
if h[i+1]in['t','T'] then first:=False
else first:=True;
h:=copy(h,1,i-1)+BoolToStr(first)+copy(h,i+2,length(h)-i+2);
end;
while pos('^',h)<>0 do
begin
i:=pos('^',h);
first:=ChrToBool(h[i-1]);
second:=ChrToBool(h[i+1]);
first:=first and second;
h:=copy(h,1,i-2)+BoolToStr(first)+copy(h,i+2,length(h)-i+2);
end;
while pos('|',h)<>0 do
begin
i:=pos('|',h);
first:=ChrToBool(h[i-1]);
second:=ChrToBool(h[i+1]);
first:=first or second;
h:=copy(h,1,i-2)+BoolToStr(first)+copy(h,i+2,length(h)-i+2);
end;
result:=ChrToBool(h[1]);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
s:=edit1.Text;
Dli:=length(s) ;
PrSc:=0;
LvSc:=0;
Fl:=true;
for Ind2:=1 to Dli do
begin
if s[Ind2]='(' then PrSC:=PrSc+1;
if s[Ind2]=')' then LvSc:=LvSc+1;
end;
if PrSc > LvSc then begin label2.Caption:='=Nexvataet ZakriV Scobki' ; Fl:=false ; end;
if PrSc < LvSc then begin label2.Caption:='=Nexvataet OtkrV Scobki' ; Fl:=false; end;
if Fl<>false then
begin
while (Dli<>1) and (Dli<>4) and (Dli<>7) and (Dli<>8) do
begin
Dli:=length(s);
for ind:=1 to Dli do
begin
If s[ind]=')' then
begin
PN:=Ind;
break;
end;
end;
if s[Pn-2] ='(' then
begin
W1:='Not'+s[pn-1];
if LogAnswer(filter(W1))
then
begin
s[pn-5]:='T';
s3:=copy(s,1,Pn-5);
s4:=copy(s,pn+1,dli);
s:=s3+s4;
Dli:=length(s);
end
else
begin
s[pn-5]:='F';
s3:=copy(s,1,pn-5);
s4:=copy(s,pn+1,dli);
s:=s3+s4;
dli:=length(s);
end;
end
else //else1 Esli ne Not
begin //else 1.1
if s[pn-5]='r'
then
begin//then2.1
W1:=s[pn-3]+'or'+s[pn-1];
if LogAnswer(filter(W1))
then//then2.2
begin//then2.2
s[pn-6]:='T';
s3:=copy(s,1,pn-6);
s4:=copy(s,pn+1,dli);
s:=s3+s4;
Dli:=length(s);
end//then2.2
else//else2.2
begin//else2.2
s[pn-6]:='F';
s3:=copy(s,1,pn-6);
s4:=copy(s,pn+1,dli);
s:=s3+s4;
Dli:=length(s);
end; //else2.2
end//then2.1
else//else2.1
begin//else2.2
W1:=s[pn-3]+'and'+s[pn-1];
if LogAnswer(filter(W1))
then//then2.3
begin//then2.3
s[pn-7]:='T';
s3:=copy(s,1,pn-7);
s4:=copy(s,pn+1,dli);
s:=s3+s4;
Dli:=length(s);
end//then2.3
else//else2.3
begin//else2.3
s[pn-7]:='F';
s3:=copy(s,1,pn-7);
s4:=copy(s,pn+1,dli);
s:=s3+s4;
Dli:=length(s);
end;//else2.3
end;//else2.2
end; //End Else 1.1
Dli:=length(s);
end; //while
case Dli of
1:begin//1
if LogAnswer(filter(s)) then label2.caption:='=True'
else label2.caption:='=False';
end;//1
6:begin//6
if LogAnswer(filter(s)) then label2.caption:='=True'
else label2.caption:='=False';
end;//6
7:begin//7
s1:=s[4]+'or'+s[6];
if LogAnswer(filter(s1)) then label2.caption:='=True'
else label2.caption:='=False';
end;//7
8:begin//8
s2:=s[5]+'and'+s[7];
if LogAnswer(filter(s2)) then label2.caption:='=True'
else label2.caption:='=False';
end;//8
end;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
label2.Caption:='=';
end;
end. |