Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.52 KB | None | 0 0
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. type
  3.     Op = record
  4.         id: string;
  5.         spen: integer;
  6.     end;
  7. var
  8.     digits: set of char;
  9.     variables: array of Op;
  10.     i, j, count, spen, num, position: integer;
  11.     SpenOfOPerators, SpenOfOperands : integer;
  12.     first, find: boolean;
  13.     buf: string;
  14. begin
  15.     Form1.Memo2.Lines.clear;
  16.     DeleteMultiLineComments;
  17.     DeleteOneLineComments;
  18.     variables := nil;
  19.     TOperators.RowCount := 2;
  20.     TOperands.RowCount := 2;
  21.     with form1.Memo1 do
  22.     begin
  23.         count:=Lines.Count-1;
  24.         while count>= 0 do
  25.         begin //ïîäãîòîâêà êîäà
  26.             str:= lines[count];
  27.             DeleteStrConst(str);
  28.             dec(count);
  29.         end;
  30.         j := 1;   // ÍÀ×ÀËÎ ÐÀÁÎÒÛ
  31.         digits := ['0'..'9'];
  32.         SpenOfOperators := 0;
  33.         for i := 1 to m do
  34.         begin    // èùó êàæäûé îïåðàòîð
  35.             first := true;
  36.             spen := 0;
  37.             count := 0;
  38.             while count<=Lines.Count-1 do    // ïî êàæäîé ñòðîêå
  39.             begin
  40.                 str:= lines[count];
  41.                 while pos(operators[i], str)<>0 do
  42.                 begin
  43.                     inc(spen);
  44.                     if first then
  45.                     begin
  46.                         TOperators.RowCount:=j+1;
  47.                         if (i=3) then
  48.                         TOperators.Cells[1, j]:='while'
  49.                         else
  50.                         if (i=4) then
  51.                             TOperators.Cells[1, j]:='do...while'
  52.                         else
  53.                             TOperators.Cells[1, j]:=Operators[i];
  54.                         TOperators.Cells[0, j]:=IntToStr(j);
  55.                         first := false;
  56.                     end;
  57.                     if (i = 7) then
  58.                     begin    // '('
  59.                         if  (pos(Operators[i], str)-6 = pos('while', str))  or  (pos(Operators[i], str) -7 = pos('typeof', str) ) then
  60.                             dec(spen);
  61.                         if  (pos(Operators[i], str) + 1 = pos('type', str)) or  (pos(Operators[i], str) - 4 = pos('for', str)) then
  62.                             dec(spen);
  63.                         if  (pos(Operators[i], str) - 3 = pos('if', str)) or  (pos(Operators[i], str) - 7 = pos('switch', str)) then
  64.                             dec(spen);
  65.                     end;
  66.                     delete( str, pos(Operators[i], str), length(Operators[i]) );
  67.                 end;
  68.                 inc(count);
  69.             end;     // êàæäàÿ ñòðîêà
  70.             if first = false then
  71.             begin
  72.                 TOperators.Cells[2, j]:=IntToStr(spen);
  73.                 inc(SpenOfOperators, spen);
  74.                 inc(j);
  75.             end;
  76.         end;   // êàæäûé îïåðàòîð          // çàêîí÷èëè ñ îïåðàòîðàìè
  77.  
  78.         count := 0;
  79.         num := 0;
  80.         while count <= Lines.Count - 1 do    // ïî êàæäîé ñòðîêå
  81.         begin
  82.             str := lines[count];
  83.             j := 2;
  84.             while j <= length(str) do
  85.             begin
  86.                 if (str[j] in digits) and not(str[j-1] in ['A'..'Z', 'a'..'z', '_']) then
  87.                 begin
  88.                     buf:='';
  89.                     while (str[j] in digits) or (str[j] = ',') do
  90.                     begin
  91.                         buf:=buf+str[j];
  92.                         inc(j);
  93.                     end;
  94.  
  95.                     if num = 0 then
  96.                     begin
  97.                         inc(num);
  98.                         setlength(variables, num);
  99.                         variables[num - 1].id := buf;
  100.                     end
  101.                     else
  102.                     begin
  103.                         find := false;
  104.                         for i := 0 to num - 1 do
  105.                             if variables[I].id = buf then //inc(variables[i].spen);
  106.                                 find := true;
  107.                         if not find then
  108.                         begin
  109.                             inc(num);
  110.                             setlength(variables, num);
  111.                             variables[num - 1].id := buf;
  112.                         end;
  113.                     end;
  114.                 end;
  115.                 inc(j);
  116.             end;
  117.             inc(count);
  118.         end;
  119.  
  120.         // ÎÏÅÐÀÍÄÛ
  121.  
  122.         for i := 1 to k do
  123.         begin
  124.             count := 0;
  125.             while count <= Lines.Count - 1 do
  126.             begin // ñàìà ðàáîòà
  127.                 str := lines[count];
  128.                 if pos(types[i], str) <> 0 then
  129.                 begin
  130.                     buf := '';
  131.                     position := pos(types[i], str) + length(types[i]);
  132.                     while (str[position] <> ' ') and (str[position] <> ';') and (str[position] <> '[') and (position < length(Str)) do
  133.                     begin                           // çàïèñü ïåðåìåííûõ;
  134.                         buf := buf+str[position];
  135.                         inc(position);
  136.                     end;    // while
  137.                     if (pos(buf, str) +length(buf)+1) = pos('(', str )then
  138.                     begin
  139.                         TOperators.RowCount := TOperators.RowCount + 1;
  140.                         TOperators.Cells[1, TOperators.RowCount - 1] := buf;
  141.                         TOperators.Cells[2, TOperators.RowCount - 1] := '';
  142.                         TOperators.Cells[0, TOperators.RowCount - 1] := IntToStr(TOperators.RowCount - 1);
  143.  
  144.                     end
  145.                     else
  146.                     begin
  147.                         inc(num);
  148.                         Setlength(variables, num);
  149.                         with  variables[num - 1] do
  150.                         begin
  151.                             id:=buf;
  152.                             spen:=-1;
  153.                         end;
  154.                     end;
  155.                 end;
  156.                 inc(count);
  157.             end; // while
  158.         end;     // for
  159.  
  160.         // íàõîæäåíèå ñïåíà
  161.         count := 0;
  162.         while count <= Lines.Count - 1 do
  163.         begin
  164.             str := lines[count];
  165.             for i:=0 to num-1 do
  166.                 while pos(Concat(' ',variables[i].id, ' '), str) <> 0 do
  167.                 begin
  168.                     inc(variables[i].spen);
  169.                     delete(str, 1+pos(Concat(' ',variables[i].id,' '), str), length(variables[i].id));
  170.                 end;
  171.             inc(count);
  172.         end;
  173.     end;  // with form1.memo1 do
  174.    
  175.     SpenOfOperands := 0;
  176.     TOperands.RowCount := num + 2;
  177.     if  TOperators.RowCount <> 2 then
  178.         TOperators.RowCount:= TOperators.RowCount+1;
  179.     for i := 0 to num - 1 do
  180.         with variables[i] do
  181.         begin
  182.             TOperands.Cells[0, i+1]:=IntToStr(i+1);
  183.             TOperands.Cells[1, i+1]:=id;
  184.             TOperands.Cells[2, i+1]:=IntToStr(spen);
  185.             inc(SpenOfOperands, spen);
  186.         end;
  187.  
  188.     with TOPerators do
  189.     begin
  190.         for i:=0 to RowCount-1 do
  191.         begin
  192.             if cells[2, i] = '' then
  193.             begin
  194.             // ñ÷èòàåì
  195.                 count := 0;
  196.                 spen := 0;
  197.                 while count<=FOrm1.Memo1.Lines.Count-1 do
  198.                 begin
  199.                     str:=FOrm1.Memo1.lines[count];
  200.                     if pos ( cells[1, i], str) <> 0 then
  201.                     inc( spen);
  202.                     inc(count);
  203.                 end;
  204.                 cells[2, i] := Inttostr(spen);
  205.             end;
  206.  
  207.         end;
  208.     end;
  209.  
  210.     TOperators.cells[0, TOperators.RowCount - 1] := 'H1 = ' + IntToStr(TOperators.RowCount - 2);
  211.     TOperators.cells[1, TOperators.RowCount - 1] := '';
  212.     TOperators.cells[2, TOperators.RowCount - 1] := 'N1 = ' + IntToStr(SpenOfOperators);
  213.     TOperands.cells[0, num + 1] := 'H2 = ' + IntToStr(TOperands.RowCount - 2);
  214.     TOperands.cells[1, num + 1]:= '';
  215.     TOperands.cells[2, num + 1] := 'N1 = ' + IntToStr(SpenOfOperands);
  216.  
  217.     // ÂÛÂÎÄ
  218.     with Form1.Memo2.Lines do
  219.     begin
  220.         Add(' Ñëîâàðü ïðîãðàììû H = ' + IntToStr(num + TOperators.RowCount - 2));
  221.         Add('Äëèíà ïðîãðàììû N = ' + IntToStr(SpenOfOPerators + SpenOfOperands));
  222.         if  num + j - 1 > 0 then
  223.             Add('Îáúåì ïðîãðàììû V = ' + IntToStr(SpenOfOPerators + SpenOfOperands) + ' log2 ' + IntToStr(num + TOperators.RowCount - 2) + ' = ' + FloatToStrF(((SpenOfOPerators + SpenOfOperands)*(ln(num + TOperators.RowCount - 2) / ln(2))), ffGeneral, 4, 4));
  224.     end;
  225.  
  226.  
  227. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement