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
| program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.Classes, System.Math;
(*
Deuxième étape. Suppression des lignes à ignorer en fonction des conditions.
N.B. La solution ci-dessous ne fonctionne que s'il y a une seule condition,
ce qui est la cas du script donné en exemple.
*)
function ValeurExpression(const aExpression: string): boolean;
const
V: array[0..1] of boolean = (FALSE, TRUE);
begin
result := TRUE;
//result := FALSE;
//result := V[Random(2)];
end;
type
TData = record
iIf,
iElse,
iEnd,
iEtage: integer;
sExpression: string;
end;
var
liste1, liste2, debug: TStringList;
data: array[1..100]of TData;
index, dernier_index: integer;
etage, dernier_etage: integer;
i, j, k, l: integer;
begin
Randomize;
liste1 := TStringList.Create;
liste2 := TStringList.Create;
liste1.LoadFromFile('1.txt');
index := 0;
dernier_index := 0;
etage := 0;
dernier_etage := 0;
for i := 0 to liste1.Count - 1 do
begin
if (Pos('IF', liste1.Strings[i]) = 1)
and not (Pos('IF_END', liste1.Strings[i]) = 1) then
(*
Ici je ne distingue pas entre IF et IFNOT.
*)
begin
Inc(index);
dernier_index := Max(index, dernier_index);
Inc(etage);
dernier_etage := Max(etage, dernier_etage);
data[index].iIf := i;
data[index].iElse := 0;
data[index].iEnd := 0;
data[index].iEtage := etage;
data[index].sExpression := Copy(
liste1.Strings[i],
3,
Length(liste1.Strings[i]) - 2
);
end;
if Pos('ELSE', liste1.Strings[i]) = 1 then
begin
data[index].iElse := i;
end;
if Pos('IF_END', liste1.Strings[i]) = 1 then
begin
data[index].iEnd := i;
Dec(index);
Dec(etage);
end;
end;
for etage := dernier_etage downto 1 do
for index := 1 to dernier_index do
if data[index].iEtage = etage then
begin
liste1.Strings[data[index].iIf] := Concat(''''{, Format('(%d)', [index])}, liste1.Strings[data[index].iIf]);
liste1.Strings[data[index].iEnd] := Concat(''''{, Format('(%d)', [index])}, liste1.Strings[data[index].iEnd]);
if data[index].iElse <> 0 then
liste1.Strings[data[index].iElse] := Concat(''''{, Format('(%d)', [index])}, liste1.Strings[data[index].iElse]);
if ValeurExpression(data[index].sExpression) then
begin
if data[index].iElse <> 0 then
for k := data[index].iElse + 1 to data[index].iEnd - 1 do
liste1.Strings[k] := Concat(''''{, Format('(%d)', [index])}, liste1.Strings[k]);
end else
begin
if data[index].iElse <> 0 then
l := data[index].iElse
else
l := data[index].iEnd;
for k := data[index].iIf + 1 to l - 1 do
liste1.Strings[k] := Concat(''''{, Format('(%d)', [index])}, liste1.Strings[k]);
end;
end;
debug := TStringList.Create;
for i := 1 to dernier_index do
with data[i] do
debug.Add(Format(
'%d %d %d %d [%s]',
[i, iIf, iElse, iEnd, sExpression]
));
debug.SaveToFile('debug.txt');
for i := 0 to liste1.Count - 1 do
if Copy(liste1.Strings[i], 1, 1) <> '''' then
liste2.Add(liste1.Strings[i]);
liste2.SaveToFile('2.txt');
liste1.Free;
liste2.Free;
debug.Free;
end. |
Partager