Показать сообщение отдельно
  #2  
Старый 10.11.2013, 03:07
Vavilon11 Vavilon11 вне форума
Прохожий
 
Регистрация: 10.11.2013
Сообщения: 7
Версия Delphi: Delphi 7
Репутация: 10
По умолчанию

В коде могут быть не оптимизированные моменты, буду рад, если вы мне о них скажете и расскажите, как это исправить
Код:
unit TV1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellApi, StdCtrls, Grids, Math;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    StringGrid1: TStringGrid;
    Label3: TLabel;
    Label4: TLabel;
    Edit2: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Label8: TLabel;
    Edit6: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Button2: TButton;
    StringGrid2: TStringGrid;
    Label12: TLabel;
    Label13: TLabel;
    Label2: TLabel;
    Label14: TLabel;
    Edit10: TEdit;
    Button3: TButton;
    Label16: TLabel;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    OpenDialog: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure Edit4KeyPress(Sender: TObject; var Key: Char);
    procedure Edit5KeyPress(Sender: TObject; var Key: Char);
    procedure Edit6KeyPress(Sender: TObject; var Key: Char);
    procedure Edit7KeyPress(Sender: TObject; var Key: Char);
    procedure Edit8KeyPress(Sender: TObject; var Key: Char);
    procedure Edit9KeyPress(Sender: TObject; var Key: Char);
    procedure Edit10KeyPress(Sender: TObject; var Key: Char);
    procedure Edit1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button5Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
     PO : Array[1..10] of real;

  end;

Var
  Form1: TForm1;
  M,D: double;
  Mass:Array[1..100] of double;

implementation

Uses MO, Unit3;

{$R *.dfm}

//Открытие формы
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Visible:=false;
StringGrid2.Visible:=false;
Label12.Visible:=false;
Label13.Visible:=false;
Label2.Visible:=false;
Button4.Visible:=false;
Button5.Visible:=false;
Button6.Visible:=false;
Button3.Visible:=false;
Label16.Visible:=false;
FillChar(Mass, SizeOf(Mass), 0);
end;

procedure TForm1.Edit1Click(Sender: TObject);
begin
Edit1.Text:='';
end;

//чтение файла
procedure TForm1.Button1Click(Sender: TObject);
var
f: TextFile;
fName,buf: String[80];
i,j,p,res:integer;
answ: word;
Ms:Array[1..100] of String;
begin
  p:=1;
  fName:=Edit1.Text;
  AssignFile(f, fName);

  
  StringGrid1.Visible:=true;
  Button3.Visible:=true;
  Label16.Visible:=true;

  //Запись из файла в массив
  For i:=1 to 100 do Readln(f,Ms[i]);

  //Проверка и изменение целых чисел с точкой
  For i:=1 to 100 do
  Begin
      buf:=Ms[i];
      For j:=1 to 5 do //Буду надеяться, что в файле не будет числа с целой частью больше 5
      Begin
        if buf[j]='.' then begin
          buf[j]:=',';
          Ms[i]:=buf;
        end;
      End;
   End;

  //Запись в таблицу
  For i:= 0 To 9 do
    For j:= 0 To 9 do
    Begin
     StringGrid1.Cells[j,i]:=Ms[p];
     p:=p+1;
    End;

  //Превращение строкового массива в целый
  For i:=1 to 100 do Mass[i]:=StrToFloat(Ms[i]);

end;

procedure TForm1.Button2Click(Sender: TObject);
var
matr:array[1..2,1..8] of real;
i,j,kol:integer;
begin
  //Если поля пустые - заменяем их 0
  if Edit2.Text='' then Edit2.Text:='0';
  if Edit3.Text='' then Edit3.Text:='0';
  if Edit4.Text='' then Edit4.Text:='0';
  if Edit5.Text='' then Edit5.Text:='0';
  if Edit6.Text='' then Edit6.Text:='0';
  if Edit7.Text='' then Edit7.Text:='0';
  if Edit8.Text='' then Edit8.Text:='0';
  if Edit9.Text='' then Edit9.Text:='0';
  if Edit10.Text='' then Edit10.Text:='0';

  matr[1,1]:=StrToInt(Edit2.Text);
  matr[1,2]:=StrToInt(Edit3.Text);
  matr[1,3]:=StrToInt(Edit4.Text);
  matr[1,4]:=StrToInt(Edit5.Text);
  matr[1,5]:=StrToInt(Edit6.Text);
  matr[1,6]:=StrToInt(Edit7.Text);
  matr[1,7]:=StrToInt(Edit8.Text);
  matr[1,8]:=StrToInt(Edit9.Text);

  //проверка полей
  kol:=StrToInt(Edit10.Text);
  If kol=0 then
  Begin
    ShowMessage('Количество монет не может быть равно 0!');
    Exit;
  End;
  if kol<>StrToInt(Edit2.Text)+StrToInt(Edit3.Text)+StrToInt(Edit4.Text)+StrToInt(Edit5.Text)+StrToInt(Edit6.Text)
  +StrToInt(Edit7.Text)+StrToInt(Edit8.Text)+StrToInt(Edit9.Text) then begin
  ShowMessage('Количество монет не ровняется количеству монет, введёным в поля!');
  Exit;
  end;

  StringGrid2.Visible:=true;
  Label12.Visible:=true;
  Label13.Visible:=true;
  Label2.Visible:=true;
  Button4.Visible:=true;
  Button5.Visible:=true;
  Button6.Visible:=true;

  for i:=1 to 8 do matr[2,i]:=RoundTo(matr[1,i]/kol,-4);
  StringGrid2.Cells[0,0]:='10';
  StringGrid2.Cells[1,0]:='5';
  StringGrid2.Cells[2,0]:='2';
  StringGrid2.Cells[3,0]:='4';
  StringGrid2.Cells[4,0]:='3';
  StringGrid2.Cells[5,0]:='0,1';
  StringGrid2.Cells[6,0]:='0,05';
  StringGrid2.Cells[7,0]:='0,01';

  for i:=0 to 7 do StringGrid2.Cells[i,1]:=FloatToStr(matr[2,i+1]);
  M:=0;
  D:=0;

  For i:=0 to 7 do M:=M+StrToFloat(StringGrid2.Cells[i,0])*StrToFloat(StringGrid2.Cells[i,1]); //Мат ожидание
  For i:=0 to 7 do D:=D+exp(2*ln(StrToFloat(StringGrid2.Cells[i,0])))*StrToFloat(StringGrid2.Cells[i,1]); //Дисперсия
  D:=D-M;

end;

//тут происходит магия и блокируются все плохие буквы.
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit3.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit4.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit5.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit5KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit6.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit6KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit7.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit7KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit8.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit8KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : Edit9.SetFocus ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit9KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;

procedure TForm1.Edit10KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9': ; // цифра
    #8 : ; // клавиша <Back Space>
    #13 : ; // клавиша <Enter>
  else Key :=Chr(0); // символ не отображать
  if Key='' then Edit2.Text:='0';
  end;
end;


procedure TForm1.Button4Click(Sender: TObject);
var
i,p:integer;
begin
    if  Form2=nil then Form2:= TForm2.create( self );
     p:=0;
     Form2.M:=RoundTo(M,-4);
     for i:=1 to 100 do if Mass[i]<>0 then p:=p+1;
     if p<>0 then for i:=1 to 100 do Form2.Mass[i]:=Mass[i]
      else
      Begin
       ShowMessage('Вы не загрузили файл или все значения равны нулю!');
       Exit
      End;
     Form2.Show;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  OpenDialog.InitialDir := ExtractFilePath( Application.exename );
  if  OpenDialog.Execute then Edit1.Text:=OpenDialog.FileName;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
i,p:integer;
begin
if  Form3=nil then Form3:= TForm3.create( self );

     p:=0;
     Form3.D:=RoundTo(D,-4);
     ShowMessage('D= '+FloatToStr(D)+' '+'FD= '+FloatToStr(Form3.D));
     for i:=1 to 100 do if Mass[i]<>0 then p:=p+1;
     if p<>0 then for i:=1 to 100 do Form3.Mass[i]:=Mass[i]
      else
      Begin
       ShowMessage('Вы не загрузили файл или все значения равны нулю!');
       Exit
      End;
     p:=0;
     for i:=1 to 10 do Form3.PO[i]:=PO[i];

     Form3.Show;
end;

End.
Ответить с цитированием