Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.13 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, StrUtils;
  8.  
  9. type
  10.    IntArr = array of Integer;
  11.    StrArr = array of string;
  12.  
  13. type
  14.   TMetrLaba_1 = class(TForm)
  15.     ReadUserFile: TButton;
  16.     OpenFile: TOpenDialog;
  17.     HolstedMatrix: TStringGrid;
  18.     procedure ReadUserFileClick(Sender: TObject);
  19.     function CheckOperatorsAmount(strFullCode: string): IntArr;
  20.     function DelExtraEnter(OperatorsAmount: IntArr): IntArr;
  21.     function DelExtraOpersWithEnter(OperatorsAmount: IntArr): IntArr;
  22.     function DelIncAndDec(OperatorsAmount: IntArr): IntArr;
  23.     function DeleteComment(strFullCode: string): string;
  24.     function FindOperands(Operands: StrArr; strTemp: string): StrArr;
  25.     procedure WriteResultInGrid(OperatorsAmount: IntArr);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31. const
  32.    Operators: array[1..44] of string = ('+', '-', '*', '/', '%', '>', '<', '!', '&', '|', '^',
  33.       '<<', '>>', '=', '+=', '-=', '*=', '/=', '%=', '>=', '<=', '!=', '&=', '|=', '^=', '<<=',
  34.          '>>=', '==', '~', '&&', '||', '++', '--', ' ?', 'sizeof', 'typeof', 'for', 'switch',
  35.             'while', 'break', 'continue', 'goto', 'if', '{');
  36.    Types: array[1..15] of string = ('sbyte', 'short', 'int', 'long', 'byte',
  37.       'ushort', 'uint', 'ulong', 'char', 'float', 'double', 'bool', 'decimal', 'string', 'var');
  38.    shiftConnectedOper = 14;
  39. var
  40.   MetrLaba_1: TMetrLaba_1;
  41.  
  42. implementation
  43.  
  44. {$R *.dfm}
  45.  
  46. // высчитывает кол-во  каждого оператора в коде
  47. function TMetrLaba_1.CheckOperatorsAmount(strFullCode: string): IntArr;
  48. var
  49.    i, TempPos: Integer;
  50.    OperatorsAmount: IntArr;
  51. begin
  52.    SetLength(OperatorsAmount, Length(Operators));
  53.    for i := 1 to Length(Operators) do
  54.    begin
  55.       TempPos := Pos(Operators[i], strFullCode);
  56.       while TempPos <> 0 do
  57.       begin
  58.          TempPos := PosEx(Operators[i], strFullCode, Length(Operators[i]) + TempPos);
  59.          Inc(OperatorsAmount[i - 1]);
  60.       end;
  61.    end;
  62.    CheckOperatorsAmount := OperatorsAmount;
  63. end;
  64.  
  65. // вычитаем все операторы '=' из совокупности операторов,
  66. // где оператор '=' задействован
  67. function TMetrLaba_1.DelExtraEnter(OperatorsAmount: IntArr): IntArr;
  68. const
  69.    StartOfOperWithEnter = 14;
  70.    EndOfOperWithEnter = 27;
  71.    NumOfDoubleEnter = 27;
  72.    NumOfEnter =  13;
  73. var
  74.    i, TempPos: Integer;
  75. begin
  76.    for i := StartOfOperWithEnter to EndOfOperWithEnter do
  77.    begin
  78.       if OperatorsAmount[i] <> 0 then
  79.       begin
  80.          if i <> NumOfDoubleEnter then
  81.             TempPos := TempPos + OperatorsAmount[i]
  82.          else
  83.             TempPos := TempPos + OperatorsAmount[i] * 2;
  84.       end;
  85.    end;
  86.    OperatorsAmount[NumOfEnter] := OperatorsAmount[NumOfEnter] - TempPos;
  87.    DelExtraEnter := OperatorsAmount;
  88. end;
  89.  
  90. //теперь разбираемся со знаками, которые рядом со знаком '='
  91. function  TMetrLaba_1.DelExtraOpersWithEnter(OperatorsAmount: IntArr): IntArr;
  92. const
  93.    StartOfOpersWinhoutEnter = 0;
  94.    EndOfOpersWinhoutEnter = 12;
  95. var
  96.    i: Integer;
  97. begin
  98.    for i := StartOfOpersWinhoutEnter to EndOfOpersWinhoutEnter do
  99.    begin
  100.       if (OperatorsAmount[i] <> 0) and (OperatorsAmount[i + shiftConnectedOper] <> 0) then
  101.          OperatorsAmount[i] := OperatorsAmount[i] - OperatorsAmount[i + shiftConnectedOper];
  102.    end;
  103.    DelExtraOpersWithEnter := OperatorsAmount;
  104. end;
  105.  
  106.  
  107. // удаляем из количества + - значения найденных ++ и --
  108. function TMetrLaba_1.DelIncAndDec(OperatorsAmount: IntArr): IntArr;
  109. const
  110.    PlusNum = 0;
  111.    MinusNum = 1;
  112.    IncNum = 31;
  113.    DecNum = 32;
  114. begin
  115.    // разбираемся c ++
  116.    if (OperatorsAmount[IncNum] <> 0) and (OperatorsAmount[PlusNum] <> 0) then
  117.    begin
  118.       OperatorsAmount[PlusNum] := OperatorsAmount[PlusNum] - OperatorsAmount[IncNum] * 2;
  119.    end;
  120.  
  121.    //разбираемся с --
  122.    if  (OperatorsAmount[DecNum] <> 0) and (OperatorsAmount[MinusNum] <> 0)  then
  123.    begin
  124.       OperatorsAmount[MinusNum] := OperatorsAmount[MinusNum] - OperatorsAmount[PlusNum] * 2;
  125.    end;
  126.    DelIncAndDec := OperatorsAmount;
  127. end;
  128.  
  129. procedure TMetrLaba_1.WriteResultInGrid(OperatorsAmount: IntArr);
  130. var
  131.    i: Integer;
  132. begin
  133.    HolstedMatrix.ColCount := 2;
  134.    for i := 0 to Length(Operators) - 1 do
  135.    begin
  136.       if OperatorsAmount[i] <> 0 then
  137.       begin
  138.          HolstedMatrix.Cells[0, HolstedMatrix.RowCount] := Operators[i + 1];
  139.          HolstedMatrix.Cells[1, HolstedMatrix.RowCount] := IntToStr(OperatorsAmount[i]);
  140.          HolstedMatrix.RowCount := HolstedMatrix.RowCount + 1;
  141.       end;
  142.    end;
  143. end;
  144.  
  145. function TMetrLaba_1.DeleteComment(strFullCode: string): string;
  146. begin
  147.  
  148.    DeleteComment := strFullCode;
  149. end;
  150.  
  151. function TMetrLaba_1.FindOperands(Operands: StrArr; strTemp: string): StrArr;
  152. const
  153.    AmountOfTypes = 15;
  154. var
  155.    i, j, TempPos: Integer;
  156.    strOperand: string;
  157. begin
  158.    for i := 1 to AmountOfTypes do
  159.    begin
  160.       TempPos := Pos(Types[i], strTemp);
  161.       while TempPos <> 0 do
  162.       begin
  163.          SetLength(Operands, Length(Operands) + 1);
  164.          j := TempPos + Length(Types[i]);
  165.          strOperand := '';
  166.          while strTemp[j] = ' ' do
  167.             j := j + 1;
  168.          while (strTemp[j] <> ' ') and (strTemp[j] <> '') do
  169.          begin
  170.             strOperand := strOperand + strTemp[j];
  171.             j := j + 1;
  172.          end;
  173.          Operands[Length(Operands) - 1] := strOperand;
  174.          TempPos := PosEx(Types[i], strTemp, TempPos + Length(Types[i]));
  175.       end;
  176.    end;
  177.    FindOperands := Operands;
  178. end;
  179.  
  180. procedure TMetrLaba_1.ReadUserFileClick(Sender: TObject);
  181. const
  182.    Comment = '//';
  183. var
  184.    strFullCode, strTemp: string;
  185.    OperatorsAmount: IntArr;
  186.    OperandsAmount: IntArr;
  187.    Operands: StrArr;
  188.    UserFile: TextFile;
  189.    i, TempPos: Integer;
  190. begin
  191.    SetLength(OperatorsAmount, Length(Operators));
  192.    //здесь считываю весь файл в строку, изначально  вынес в отдельную функцию
  193.    //но потом понял, что удобно использовать тут переменную strTemp
  194.    //для того, чтобы создать массив операндов, если бы это было в функции, то пришлось бы
  195.    //возвращать 2 значения, strFullCode и массив операндов Operands, что довольно плохо
  196.    if OpenFile.Execute then
  197.    begin
  198.       AssignFile(UserFile, OpenFile.FileName);
  199.       Reset(UserFile);
  200.       strFullCode := '';
  201.       while not EOf(UserFile) do
  202.       begin
  203.          Read(UserFile, strTemp);
  204.          //удаление обычного комментария в строку
  205.          TempPos := Pos(Comment, strTemp);
  206.          if TempPos <> 0 then
  207.          begin
  208.             SetLength(strTemp, TempPos - 1);
  209.          end;
  210.          Operands := FindOperands(Operands, strTemp);        // поиск объявлений операндов в строке
  211.          strFullCode := strFullCode + strTemp;
  212.       end;
  213.       CloseFile(UserFile);
  214.    end;
  215.  
  216.    //на стадии разработки
  217.    strFullCode := DeleteComment(strFullCode);
  218.  
  219.    //высчитывает кол-во  каждого оператора в коде
  220.    OperatorsAmount := CheckOperatorsAmount(strFullCode);
  221.  
  222.    //вычитаем все операторы '=' из совокупности операторов,
  223.    //где оператор '=' задействован
  224.    OperatorsAmount := DelExtraEnter(OperatorsAmount);
  225.  
  226.    //теперь разбираемся со знаками, которые рядом со знаком '='
  227.    OperatorsAmount := DelExtraOpersWithEnter(OperatorsAmount);
  228.  
  229.    //удаляем из количества + - значения найденных ++ и --
  230.    OperatorsAmount := DelIncAndDec(OperatorsAmount);
  231.  
  232.    //выводим результат в стринггрид
  233.    WriteResultInGrid(OperatorsAmount);
  234. end;
  235. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement