Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- procedure TForm1.Button1Click(Sender: TObject); // ОСНОВНАЯ КНОПКА
- type
- Op = record
- id: string;
- spen: integer;
- end;
- var
- digits: set of char;
- variables: array of Op;
- i, j, count, spen, num, position: integer;
- SpenOfOPerators, SpenOfOperands : integer;
- first, find: boolean;
- buf: string;
- begin
- Form1.Memo2.Lines.clear;
- DeleteMultiLineComments;
- DeleteOneLineComments;
- variables := nil;
- TOperators.RowCount := 2;
- TOperands.RowCount := 2;
- with form1.Memo1 do
- begin
- count:=Lines.Count-1;
- while count>= 0 do
- begin //подготовка кода
- str:= lines[count];
- DeleteStrConst(str);
- dec(count);
- end;
- j := 1; // НАЧАЛО РАБОТЫ
- digits := ['0'..'9'];
- SpenOfOperators := 0;
- for i := 1 to m do
- begin // ищу каждый оператор
- first := true;
- spen := 0;
- count := 0;
- while count<=Lines.Count-1 do // по каждой строке
- begin
- str:= lines[count];
- while pos(operators[i], str)<>0 do
- begin
- inc(spen);
- if first then
- begin
- TOperators.RowCount:=j+1;
- if (i=3) then
- TOperators.Cells[1, j]:='while'
- else
- if (i=4) then
- TOperators.Cells[1, j]:='do...while'
- else
- TOperators.Cells[1, j]:=Operators[i];
- TOperators.Cells[0, j]:=IntToStr(j);
- first := false;
- end;
- if (i = 7) then
- begin // '('
- if (pos(Operators[i], str)-6 = pos('while', str)) or (pos(Operators[i], str) -7 = pos('typeof', str) ) then
- dec(spen);
- if (pos(Operators[i], str) + 1 = pos('type', str)) or (pos(Operators[i], str) - 4 = pos('for', str)) then
- dec(spen);
- if (pos(Operators[i], str) - 3 = pos('if', str)) or (pos(Operators[i], str) - 7 = pos('switch', str)) then
- dec(spen);
- end;
- delete( str, pos(Operators[i], str), length(Operators[i]) );
- end;
- inc(count);
- end; // каждая строка
- if first = false then
- begin
- TOperators.Cells[2, j]:=IntToStr(spen);
- inc(SpenOfOperators, spen);
- inc(j);
- end;
- end; // каждый оператор // закончили с операторами
- count := 0;
- num := 0;
- while count <= Lines.Count - 1 do // по каждой строке
- begin
- str := lines[count];
- j := 2;
- while j <= length(str) do
- begin
- if (str[j] in digits) and not(str[j-1] in ['A'..'Z', 'a'..'z', '_']) then
- begin
- buf:='';
- while (str[j] in digits) or (str[j] = ',') do
- begin
- buf:=buf+str[j];
- inc(j);
- end;
- if num = 0 then
- begin
- inc(num);
- setlength(variables, num);
- variables[num - 1].id := buf;
- end
- else
- begin
- find := false;
- for i := 0 to num - 1 do
- if variables[I].id = buf then //inc(variables[i].spen);
- find := true;
- if not find then
- begin
- inc(num);
- setlength(variables, num);
- variables[num - 1].id := buf;
- end;
- end;
- end;
- inc(j);
- end;
- inc(count);
- end;
- // ОПЕРАНДЫ
- for i := 1 to k do
- begin
- count := 0;
- while count <= Lines.Count - 1 do
- begin // сама работа
- str := lines[count];
- if pos(types[i], str) <> 0 then
- begin
- buf := '';
- position := pos(types[i], str) + length(types[i]);
- while (str[position] <> ' ') and (str[position] <> ';') and (str[position] <> '[') and (position < length(Str)) do
- begin // запись переменных;
- buf := buf+str[position];
- inc(position);
- end; // while
- if (pos(buf, str) +length(buf)+1) = pos('(', str )then
- begin
- TOperators.RowCount := TOperators.RowCount + 1;
- TOperators.Cells[1, TOperators.RowCount - 1] := buf;
- TOperators.Cells[2, TOperators.RowCount - 1] := '';
- TOperators.Cells[0, TOperators.RowCount - 1] := IntToStr(TOperators.RowCount - 1);
- end
- else
- begin
- inc(num);
- Setlength(variables, num);
- with variables[num - 1] do
- begin
- id:=buf;
- spen:=-1;
- end;
- end;
- end;
- inc(count);
- end; // while
- end; // for
- // нахождение спена
- count := 0;
- while count <= Lines.Count - 1 do
- begin
- str := lines[count];
- for i:=0 to num-1 do
- while pos(Concat(' ',variables[i].id, ' '), str) <> 0 do
- begin
- inc(variables[i].spen);
- delete(str, 1+pos(Concat(' ',variables[i].id,' '), str), length(variables[i].id));
- end;
- inc(count);
- end;
- end; // with form1.memo1 do
- SpenOfOperands := 0;
- TOperands.RowCount := num + 2;
- if TOperators.RowCount <> 2 then
- TOperators.RowCount:= TOperators.RowCount+1;
- for i := 0 to num - 1 do
- with variables[i] do
- begin
- TOperands.Cells[0, i+1]:=IntToStr(i+1);
- TOperands.Cells[1, i+1]:=id;
- TOperands.Cells[2, i+1]:=IntToStr(spen);
- inc(SpenOfOperands, spen);
- end;
- with TOPerators do
- begin
- for i:=0 to RowCount-1 do
- begin
- if cells[2, i] = '' then
- begin
- // считаем
- count := 0;
- spen := 0;
- while count<=FOrm1.Memo1.Lines.Count-1 do
- begin
- str:=FOrm1.Memo1.lines[count];
- if pos ( cells[1, i], str) <> 0 then
- inc( spen);
- inc(count);
- end;
- cells[2, i] := Inttostr(spen);
- end;
- end;
- end;
- TOperators.cells[0, TOperators.RowCount - 1] := 'H1 = ' + IntToStr(TOperators.RowCount - 2);
- TOperators.cells[1, TOperators.RowCount - 1] := '';
- TOperators.cells[2, TOperators.RowCount - 1] := 'N1 = ' + IntToStr(SpenOfOperators);
- TOperands.cells[0, num + 1] := 'H2 = ' + IntToStr(TOperands.RowCount - 2);
- TOperands.cells[1, num + 1]:= '';
- TOperands.cells[2, num + 1] := 'N1 = ' + IntToStr(SpenOfOperands);
- // ВЫВОД
- with Form1.Memo2.Lines do
- begin
- Add(' Словарь программы H = ' + IntToStr(num + TOperators.RowCount - 2));
- Add('Длина программы N = ' + IntToStr(SpenOfOPerators + SpenOfOperands));
- if num + j - 1 > 0 then
- Add('Объем программы V = ' + IntToStr(SpenOfOPerators + SpenOfOperands) + ' log2 ' + IntToStr(num + TOperators.RowCount - 2) + ' = ' + FloatToStrF(((SpenOfOPerators + SpenOfOperands)*(ln(num + TOperators.RowCount - 2) / ln(2))), ffGeneral, 4, 4));
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement