Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, StrUtils;
- type
- IntArr = array of Integer;
- StrArr = array of string;
- type
- TMetrLaba_1 = class(TForm)
- ReadUserFile: TButton;
- OpenFile: TOpenDialog;
- HolstedMatrix: TStringGrid;
- procedure ReadUserFileClick(Sender: TObject);
- function CheckOperatorsAmount(strFullCode: string): IntArr;
- function DelExtraEnter(OperatorsAmount: IntArr): IntArr;
- function DelExtraOpersWithEnter(OperatorsAmount: IntArr): IntArr;
- function DelIncAndDec(OperatorsAmount: IntArr): IntArr;
- function DeleteComment(strFullCode: string): string;
- function FindOperands(Operands: StrArr; strTemp: string): StrArr;
- procedure WriteResultInGrid(OperatorsAmount: IntArr);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- const
- Operators: array[1..44] of string = ('+', '-', '*', '/', '%', '>', '<', '!', '&', '|', '^',
- '<<', '>>', '=', '+=', '-=', '*=', '/=', '%=', '>=', '<=', '!=', '&=', '|=', '^=', '<<=',
- '>>=', '==', '~', '&&', '||', '++', '--', ' ?', 'sizeof', 'typeof', 'for', 'switch',
- 'while', 'break', 'continue', 'goto', 'if', '{');
- Types: array[1..15] of string = ('sbyte', 'short', 'int', 'long', 'byte',
- 'ushort', 'uint', 'ulong', 'char', 'float', 'double', 'bool', 'decimal', 'string', 'var');
- shiftConnectedOper = 14;
- var
- MetrLaba_1: TMetrLaba_1;
- implementation
- {$R *.dfm}
- // высчитывает кол-во каждого оператора в коде
- function TMetrLaba_1.CheckOperatorsAmount(strFullCode: string): IntArr;
- var
- i, TempPos: Integer;
- OperatorsAmount: IntArr;
- begin
- SetLength(OperatorsAmount, Length(Operators));
- for i := 1 to Length(Operators) do
- begin
- TempPos := Pos(Operators[i], strFullCode);
- while TempPos <> 0 do
- begin
- TempPos := PosEx(Operators[i], strFullCode, Length(Operators[i]) + TempPos);
- Inc(OperatorsAmount[i - 1]);
- end;
- end;
- CheckOperatorsAmount := OperatorsAmount;
- end;
- // вычитаем все операторы '=' из совокупности операторов,
- // где оператор '=' задействован
- function TMetrLaba_1.DelExtraEnter(OperatorsAmount: IntArr): IntArr;
- const
- StartOfOperWithEnter = 14;
- EndOfOperWithEnter = 27;
- NumOfDoubleEnter = 27;
- NumOfEnter = 13;
- var
- i, TempPos: Integer;
- begin
- for i := StartOfOperWithEnter to EndOfOperWithEnter do
- begin
- if OperatorsAmount[i] <> 0 then
- begin
- if i <> NumOfDoubleEnter then
- TempPos := TempPos + OperatorsAmount[i]
- else
- TempPos := TempPos + OperatorsAmount[i] * 2;
- end;
- end;
- OperatorsAmount[NumOfEnter] := OperatorsAmount[NumOfEnter] - TempPos;
- DelExtraEnter := OperatorsAmount;
- end;
- //теперь разбираемся со знаками, которые рядом со знаком '='
- function TMetrLaba_1.DelExtraOpersWithEnter(OperatorsAmount: IntArr): IntArr;
- const
- StartOfOpersWinhoutEnter = 0;
- EndOfOpersWinhoutEnter = 12;
- var
- i: Integer;
- begin
- for i := StartOfOpersWinhoutEnter to EndOfOpersWinhoutEnter do
- begin
- if (OperatorsAmount[i] <> 0) and (OperatorsAmount[i + shiftConnectedOper] <> 0) then
- OperatorsAmount[i] := OperatorsAmount[i] - OperatorsAmount[i + shiftConnectedOper];
- end;
- DelExtraOpersWithEnter := OperatorsAmount;
- end;
- // удаляем из количества + - значения найденных ++ и --
- function TMetrLaba_1.DelIncAndDec(OperatorsAmount: IntArr): IntArr;
- const
- PlusNum = 0;
- MinusNum = 1;
- IncNum = 31;
- DecNum = 32;
- begin
- // разбираемся c ++
- if (OperatorsAmount[IncNum] <> 0) and (OperatorsAmount[PlusNum] <> 0) then
- begin
- OperatorsAmount[PlusNum] := OperatorsAmount[PlusNum] - OperatorsAmount[IncNum] * 2;
- end;
- //разбираемся с --
- if (OperatorsAmount[DecNum] <> 0) and (OperatorsAmount[MinusNum] <> 0) then
- begin
- OperatorsAmount[MinusNum] := OperatorsAmount[MinusNum] - OperatorsAmount[PlusNum] * 2;
- end;
- DelIncAndDec := OperatorsAmount;
- end;
- procedure TMetrLaba_1.WriteResultInGrid(OperatorsAmount: IntArr);
- var
- i: Integer;
- begin
- HolstedMatrix.ColCount := 2;
- for i := 0 to Length(Operators) - 1 do
- begin
- if OperatorsAmount[i] <> 0 then
- begin
- HolstedMatrix.Cells[0, HolstedMatrix.RowCount] := Operators[i + 1];
- HolstedMatrix.Cells[1, HolstedMatrix.RowCount] := IntToStr(OperatorsAmount[i]);
- HolstedMatrix.RowCount := HolstedMatrix.RowCount + 1;
- end;
- end;
- end;
- function TMetrLaba_1.DeleteComment(strFullCode: string): string;
- begin
- DeleteComment := strFullCode;
- end;
- function TMetrLaba_1.FindOperands(Operands: StrArr; strTemp: string): StrArr;
- const
- AmountOfTypes = 15;
- var
- i, j, TempPos: Integer;
- strOperand: string;
- begin
- for i := 1 to AmountOfTypes do
- begin
- TempPos := Pos(Types[i], strTemp);
- while TempPos <> 0 do
- begin
- SetLength(Operands, Length(Operands) + 1);
- j := TempPos + Length(Types[i]);
- strOperand := '';
- while strTemp[j] = ' ' do
- j := j + 1;
- while (strTemp[j] <> ' ') and (strTemp[j] <> '') do
- begin
- strOperand := strOperand + strTemp[j];
- j := j + 1;
- end;
- Operands[Length(Operands) - 1] := strOperand;
- TempPos := PosEx(Types[i], strTemp, TempPos + Length(Types[i]));
- end;
- end;
- FindOperands := Operands;
- end;
- procedure TMetrLaba_1.ReadUserFileClick(Sender: TObject);
- const
- Comment = '//';
- var
- strFullCode, strTemp: string;
- OperatorsAmount: IntArr;
- OperandsAmount: IntArr;
- Operands: StrArr;
- UserFile: TextFile;
- i, TempPos: Integer;
- begin
- SetLength(OperatorsAmount, Length(Operators));
- //здесь считываю весь файл в строку, изначально вынес в отдельную функцию
- //но потом понял, что удобно использовать тут переменную strTemp
- //для того, чтобы создать массив операндов, если бы это было в функции, то пришлось бы
- //возвращать 2 значения, strFullCode и массив операндов Operands, что довольно плохо
- if OpenFile.Execute then
- begin
- AssignFile(UserFile, OpenFile.FileName);
- Reset(UserFile);
- strFullCode := '';
- while not EOf(UserFile) do
- begin
- Read(UserFile, strTemp);
- //удаление обычного комментария в строку
- TempPos := Pos(Comment, strTemp);
- if TempPos <> 0 then
- begin
- SetLength(strTemp, TempPos - 1);
- end;
- Operands := FindOperands(Operands, strTemp); // поиск объявлений операндов в строке
- strFullCode := strFullCode + strTemp;
- end;
- CloseFile(UserFile);
- end;
- //на стадии разработки
- strFullCode := DeleteComment(strFullCode);
- //высчитывает кол-во каждого оператора в коде
- OperatorsAmount := CheckOperatorsAmount(strFullCode);
- //вычитаем все операторы '=' из совокупности операторов,
- //где оператор '=' задействован
- OperatorsAmount := DelExtraEnter(OperatorsAmount);
- //теперь разбираемся со знаками, которые рядом со знаком '='
- OperatorsAmount := DelExtraOpersWithEnter(OperatorsAmount);
- //удаляем из количества + - значения найденных ++ и --
- OperatorsAmount := DelIncAndDec(OperatorsAmount);
- //выводим результат в стринггрид
- WriteResultInGrid(OperatorsAmount);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement