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.StdCtrls, Vcl.Grids, Vcl.Menus;
- type
- TForm1 = class(TForm)
- StringGrid1: TStringGrid;
- Edit1: TEdit;
- Edit2: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Button1: TButton;
- MainMenu1: TMainMenu;
- PopupMenu1: TPopupMenu;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N4: TMenuItem;
- N5: TMenuItem;
- procedure Button1Click(Sender: TObject);
- procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit2Change(Sender: TObject);
- procedure Edit1KeyPress(Sender: TObject; var Key: Char);
- procedure Edit2KeyPress(Sender: TObject; var Key: Char);
- procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- procedure Edit1Change(Sender: TObject);
- procedure N4Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure N2Click(Sender: TObject);
- procedure N3Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- Path, Result : String;
- IsFileOpen, Flag, FlagButton : Boolean;
- Const
- BAD = 100000;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- var
- X, N, B, I, Sum : Integer;
- Arr : Array of Integer;
- begin
- Try
- X := StrToInt(Edit1.Text);
- Except
- Application.MessageBox('Проверьте исходные данные.', 'Ошибка', MB_ICONSTOP);
- Edit1.Text := '';
- End;
- Try
- N := StrToInt(Edit2.Text);
- Except
- Application.MessageBox('Проверьте исходные данные.', 'Ошибка', MB_ICONSTOP);
- Edit2.Text := '';
- End;
- SetLength(Arr, N);
- Sum := 0;
- For I := Low(Arr) to High(Arr) do
- Begin
- If Length(StringGrid1.Cells[I, 0]) <> 0 then
- Arr[I]:= StrToInt(StringGrid1.Cells[I,0])
- else
- Arr[I] := 0;
- End;
- B := Arr[N - 1];
- For I := (N - 2) Downto 0 Do
- Begin
- B := Arr[I] + (B * X);
- End;
- Label3.Visible := true;
- Label3.Caption := 'Ответ: ' + IntToStr(B);
- Result := Label3.Caption;
- N3.Enabled := true;
- end;
- procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
- begin
- If ((Key = #13) And (Button1.Enabled)) Then
- Button1.Click;
- If (Not(Key In ['0'..'9', #08, #46, '-'])) Then
- Key := #0;
- If (Length(Edit1.Text) = 0) then
- If (Not(Key In ['1'..'9', #08, #46, '-'])) Then
- Key := #0;
- If (Length(Edit1.Text) <> 0) and (Key = '-')then
- Key := #0;
- If Key = '.' then
- Key := #0;
- end;
- procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
- const Value: string);
- Var
- I, J : Integer;
- begin
- N3.Enabled := False;
- Label3.Visible := False;
- FlagButton := true;
- for I := 0 to StringGrid1.RowCount - 1 do
- for J := 0 to StringGrid1.ColCount - 1 do
- If Length(StringGrid1.Cells[J, I]) = 0 Then
- FlagButton := false;
- If not(flag)then
- Button1.Enabled := False;
- If (Length(Edit1.Text) <> 0) and (Length(Edit2.Text) <> 0) and (FlagButton = true) then
- Button1.Enabled := True;
- end;
- procedure TForm1.Edit1Change(Sender: TObject);
- begin
- N3.Enabled := False;
- If (Length(Edit1.Text) = 0)then
- Button1.Enabled := False;
- If (Length(Edit1.Text) <> 0) and (Length(Edit2.Text) <> 0) and (FlagButton = true) then
- Button1.Enabled := True;
- Label3.Visible := False;
- end;
- procedure TForm1.Edit2Change(Sender: TObject);
- var
- N, I, J: Integer;
- const
- MAX = 3;
- STEP = 81;
- THREE_CELLS = 246;
- HEIGHT = 57;
- STEP_H = 35;
- LEFT = 10;
- STEP_L2 = 45;
- STEP_L1 = 83;
- begin
- if Edit2.Text <> '' then
- Begin
- N := StrToInt(Edit2.Text);
- StringGrid1.ColCount := N;
- StringGrid1.Width := THREE_CELLS;
- StringGrid1.Height := HEIGHT;
- StringGrid1.Left := LEFT;
- if N = 3 then
- StringGrid1.Height := STEP_H;
- if N = 2 then
- Begin
- StringGrid1.Width := THREE_CELLS - STEP;
- StringGrid1.Height := STEP_H;
- StringGrid1.Left := LEFT + STEP_L2;
- End;
- if N = 1 then
- Begin //нахуя я это делал, это не надо было. N никогда не равно 1..
- StringGrid1.Width := THREE_CELLS - STEP - STEP;
- StringGrid1.Height := STEP_H;
- StringGrid1.Left := LEFT + STEP_L1;
- End;
- StringGrid1.Visible := True;
- End
- Else
- StringGrid1.Visible := False;
- N3.Enabled := False;
- If (Length(Edit2.Text) = 0)then
- Button1.Enabled := False;
- If (Length(Edit2.Text) <> 0) and (Length(Edit2.Text) <> 0) and (FlagButton = true) then
- Button1.Enabled := True;
- Label3.Visible := False;
- for I := 0 to StringGrid1.RowCount - 1 do
- for J := 0 to StringGrid1.ColCount - 1 do
- StringGrid1.Cells[J, I] := '';
- end;
- procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
- begin
- If ((Key = #13) And (Button1.Enabled)) Then
- Button1.Click;
- If (Not(Key In ['0'..'9', #08, #46])) Then
- Key := #0;
- if (Length(Edit2.Text) = 0) then
- If (Not(Key In ['2'..'9', #08, #46])) Then
- Key := #0;
- If Key = '.' then
- Key := #0;
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := MessageBox(Form1.Handle, 'Вы уверены, что хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION)=ID_YES;
- end;
- procedure TForm1.N4Click(Sender: TObject);
- begin
- Application.MessageBox('Программа вычисляет многочлен А1*x + А2*x^2 + А3*x^3 + ... + АN*x^N.'#13#10'Диапазон X: -9999...99999'#13#10'Диапазон N: 2...99999'#13#10'Диапазон элементов массива: -9999...99999'#13#10'Внимание! Все вводимые числа целого типа.', 'Инструкция', 0);
- end;
- procedure TForm1.N5Click(Sender: TObject);
- begin
- Application.MessageBox('Арефин Владислав гр.251004', 'Разрабочик', 0);
- end;
- procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
- const // тут я сделал ввод онли integer цифр, тк мне впадлу делать real
- MAX = 4;
- begin
- If (Not(Key In ['0'..'9', #08, #46,',','-'])) Then
- Key := #0;
- With Sender As TStringGrid Do
- Begin
- If (Length(StringGrid1.Cells[Col, Row]) <> 0) and (Key = '-') then
- Key := Char(0);
- If (Length(StringGrid1.Cells[Col, Row]) > MAX) then
- If (Not(Key In [#08, #46])) Then
- Key := #0;
- If (Length(StringGrid1.Cells[Col, Row]) = 0) and (Key = '0') then
- Key := Char(0);
- If Key = '.' then
- Key := #0;
- End;
- end;
- Function CheckFileDataForXandArr(Num: String): Boolean;
- Const
- MAX_LIM = 99999;
- MIN_LIM = -9999;
- Var
- NewNum: Integer;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Num := Trim (Num);
- Try
- NewNum := StrToInt(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Не получилось преобразовать X к целочисленному типу данных. Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum < MIN_LIM))) Then
- Begin
- Num := IntToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('X вне разрешенного диапазона! Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileDataForXAndArr := IsCorrect;
- End;
- Function CheckFileDataForN(Num: String): Boolean;
- Const
- MAX_LIM = 99999;
- MIN_LIM = 2;
- Var
- NewNum: Integer;
- IsCorrect: Boolean;
- Begin
- NewNum := 0;
- IsCorrect := True;
- Num := Trim (Num);
- Try
- NewNum := StrToInt(Num);
- Except
- MessageBox(Form1.Handle, Pchar('Не получилось преобразовать N к целочисленному типу данных. Проверьте корректность данных.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- If(IsCorrect And ((NewNum > MAX_LIM) Or (NewNum < MIN_LIM))) Then
- Begin
- Num := IntToStr(NewNum);
- MessageBox(Form1.Handle, Pchar('N вне разрешенного диапазона! Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- End;
- CheckFileDataForN := IsCorrect;
- End;
- Function Open (): String;
- Begin
- With Form1 Do
- Begin
- If OpenDialog1.Execute Then
- Begin
- Path := OpenDialog1.FileName;
- IsFileOpen := True;
- End
- Else
- IsFileOpen := False;
- End;
- Open := Path;
- End;
- Function TakeDataFromFile1(Number1: String; Var FileOutput: TextFile): String;
- Var // x
- IsRight: Boolean;
- Begin
- IsRight := True;
- Try
- Readln(FileOutput, Number1);
- Number1 := Trim (Number1);
- IsRight := CheckFileDataForXAndArr(Number1);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile1 := ''
- Else
- TakeDataFromFile1 := Number1;
- End;
- Function TakeDataFromFile2(Number2: String; Var FileOutput: TextFile): String;
- Var //N
- IsRight : Boolean;
- Begin
- IsRight := True;
- Try
- Readln(FileOutput, Number2);
- Number2 := Trim (Number2);
- IsRight := CheckFileDataForN(Number2);
- Except
- End;
- If(Not(IsRight)) Then
- TakeDataFromFile2 := ''
- Else
- TakeDataFromFile2 := Number2;
- End;
- type
- TArray = Array of Integer;
- TStr = Array of String[5];
- function SeparateString (Str : String) : TStr;
- Var
- StrArr : TStr;
- I, K : Integer;
- Flag : Boolean;
- Begin
- K := 0;
- SetLength(StrArr, (Str.Length div 2) + 1);
- For I := 0 To Str.Length div 2 do
- StrArr[i] := '';
- I := 1;
- While I <= Str.Length Do
- begin
- Flag := true;
- While (Str[I] <> ' ') and (I <= Str.Length) Do
- Begin
- StrArr[K] := StrArr[K] + Str[I];
- Inc(I);
- Flag := False;
- End;
- If Not(Flag) then
- Inc(K);
- If Flag then
- Inc(I);
- end;
- I := 1;
- K := 0;
- While Str[I] <> '' Do
- Begin
- If Str[I] = ' ' then
- Inc(K);
- Inc(I);
- End;
- SetLength(StrArr, K + 1);
- Result := StrArr;
- End;
- function ConvertStringToArray (StringGridColCount : Integer; Var FileOutput: TextFile) : TArray;
- Var
- I: Integer;
- Arr : TArray;
- Str : String;
- StrArr : TStr;
- Const
- MIN = -9999;
- MAX = 99999;
- Begin
- Readln(FileOutput, Str);
- SetLength(Arr, StringGridColCount);
- for I := Low(Arr) to High(Arr) do
- Arr[I] := 0;
- Str := Trim(Str);
- StrArr := SeparateString (Str);
- If (High(StrArr) + 1 <> StringGridColCount) then
- begin
- SetLength (Arr, 1);
- Arr[0] := BAD;
- MessageBox(Form1.Handle, Pchar('Количество элементов массива не совпадает с заявленным. Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- ConvertStringToArray := Arr;
- Exit
- end;
- try
- For I := Low(Arr) to High(Arr) do
- Arr[I] := StrToInt (StrArr[I]);
- except
- SetLength (Arr, 1);
- Arr[0] := BAD;
- MessageBox(Form1.Handle, Pchar('Не удалось преобразовать исходные данные в целочисленный тип. Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- ConvertStringToArray := Arr;
- Exit
- end;
- For I := Low(Arr) to High(Arr) do
- If (Arr[I] > MAX) or (Arr[I] < MIN) then
- begin
- SetLength (Arr, 1);
- Arr[0] := BAD;
- MessageBox(Form1.Handle, Pchar('Исходные данные выходят за границы допустимых. Проверьте исходные данные.'), 'Ошибка', MB_ICONSTOP);
- ConvertStringToArray := Arr;
- end;
- ConvertStringToArray := Arr;
- End;
- procedure TForm1.N2Click(Sender: TObject);
- Var
- FileInput: TextFile;
- Num, I: Integer;
- Str1, Str2 : String;
- Arr : TArray;
- begin
- Num := 0;
- Path := Open;
- AssignFile(FileInput, Path);
- Reset(FileInput);
- If(IsFileOpen) Then
- Begin
- Str1 := TakeDataFromFile1(IntToStr(Num), FileInput);
- if Str1 <> '' then
- Str2 := TakeDataFromFile2(IntToStr(Num), FileInput);
- End;
- if Not((Str1 = '') or (Str2 = '')) then
- Begin
- Edit1.Text := Str1;
- Edit2.Text := Str2;
- End
- Else
- Begin
- Edit1.Text := #0;
- Edit2.Text := #0;
- End;
- if (Str1 <> '') and (Str2 <> '') then
- Begin
- Arr := ConvertStringToArray(StrToInt(Str2), FileInput);
- if (Arr[0] <> BAD) then
- Begin
- for I := Low(Arr) to High(Arr) do
- Begin
- StringGrid1.Cells[I, 0] := IntToStr(Arr[I]);
- Button1.Enabled := true;
- End;
- End
- else
- Begin
- for I := Low(Arr) to StrToInt(Str2) - 1 do
- Begin
- StringGrid1.Cells[I, 0] := '';
- End;
- Edit1.Text := '';
- Edit2.Text := '';
- End;
- End;
- CloseFile(FileInput);
- end;
- procedure TForm1.N3Click(Sender: TObject);
- Var
- FileOutput: TextFile;
- IsCorrect : Boolean;
- begin
- IsCorrect := True;
- Path := Open;
- If (IsFileOpen) Then
- Begin
- try
- AssignFile(FileOutput, Path);
- Rewrite(FileOutput);
- Write(FileOutput, Result);
- except
- IsCorrect := False;
- Application.MessageBox('Запись в файл не удалась.', 'Ошибка', MB_ICONSTOP);
- end;
- if IsCorrect then
- Begin
- Application.MessageBox('Запись файла выполнена успешно.', 'Результат', 0);
- CloseFile(FileOutput);
- Label2.Caption := '';
- Edit1.Text := '';
- Edit2.Text := '';
- End;
- End;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement