unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons, 

  Newton, FEvalLib, Spin;

type
  TMainForm = class(TForm)
    ButtonCount: TButton;
    Label4: TLabel;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    EditFunction: TEdit;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    EditValueA: TEdit;
    Label5: TLabel;
    EditValueB: TEdit;
    GroupBox3: TGroupBox;
    MemoHalf: TMemo;
    MemoNewton: TMemo;
    GroupBox4: TGroupBox;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    ButtonCheck: TButton;
    ButtonFuncHelp: TButton;
    LabelResHalf: TLabel;
    LabelResNewton: TLabel;
    GroupBox5: TGroupBox;
    Panel1: TPanel;
    EditEps: TEdit;
    Panel2: TPanel;
    Label9: TLabel;
    SpinEpsExp: TSpinEdit;
    RBRucne: TRadioButton;
    RBZvolit: TRadioButton;
    Label10: TLabel;
    Label11: TLabel;
    ButtonShowGraph: TButton;
    ButtonShowAbout: TButton;
    procedure EditValueAKeyPress(Sender: TObject; var Key: Char);
    procedure ButtonCheckClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ButtonFuncHelpClick(Sender: TObject);
    procedure ButtonCountClick(Sender: TObject);
    procedure RBRucneClick(Sender: TObject);
    procedure RBZvolitClick(Sender: TObject);
    procedure ButtonShowGraphClick(Sender: TObject);

    procedure ButtonShowAboutClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);

  public
    Newton: TNewton;

  private
    procedure GetValues(var a, b, Eps: extended);
  end;

var
  MainForm: TMainForm;

implementation
uses
  Math, Graf, Errors;

{$R *.dfm}

procedure TMainForm.EditValueAKeyPress(Sender: TObject; var Key: Char);
  //kontrola pro zadavani pouze cisla
var
  EditValue: TEdit;
begin
  EditValue:= TEdit(Sender);
  if (( (Key < '0') or (Key > '9') ) and
    not(Key in  [DecimalSeparator, chr(8), '-'])) then
    Key:=chr(0)
  else begin
    if (Key = DecimalSeparator)
        and (Pos(DecimalSeparator, EditValue.Text) <> 0) then
      Key:=chr(0);
    if (Key = '-')
        and (Pos('-', EditValue.Text) <> 0) then
      Key:=chr(0);
  end;
end;


procedure TMainForm.ButtonCheckClick(Sender: TObject);
var
  ErrorPos: integer;
begin
  ErrorPos:= Newton.Func.ReadExpr(EditFunction.Text);

  if ErrorPos <> 0 then
    Application.MessageBox(PChar(
      Format('Chyba pøi kontrole: %s, Pozice: %d',
              [GetErrorName(GetLastErrorCode), ErrorPos])), 'Chyba',
              MB_OK + MB_ICONWARNING)
  else
    Application.MessageBox('Výraz je syntakticky správnì.', 'V poøádku');
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin

  Newton:= TNewton.Create;

  SetLastErrorCode(ERR_OK);
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Newton.Close;
end;

procedure TMainForm.ButtonFuncHelpClick(Sender: TObject);
begin
  Application.MessageBox(
    '- Výraz se zapisuje tak jak jste zvyklí z programovacích jazykù, jedinou '
    + 'výjimkou je, že se na nìkterých místech nemusí psát znaménko násobení. '
    + '(tak, jak se zapisují na papír) ' +#13 + #10
    + '- Násobení se pøidává pro tyto typy zápisu: mezi závorky )(, pøed '
    + 'promìnnou nebo funkci 5x, 5sin(, pøed levou závorku x(, 5( '
    + 'a za závorku )x, )5 .' + #13 + #10
    + '- Ve výrazu mohou být použity tyto funkce: sin, cos, tan, cot, arctan, '
    + 'arccos, arctan, sinh, cosh, tanh, coth, arctanh, arccosh, arctanh, '
    + 'ln, log10.' + #13 + #10
    + '- Jsou pøeddefinovány tyto konstanty: pi, e.' + #13 + #10
    + '- Promìnná se znaèí vždy "x".' + #13 + #10
    + '- Pozor: U desetinných èísel se píše desetinný oddìlovaè podle nastavení '
    + 'lokalizace vašeho systému.',
    'Nápovìda k zadávání funkce',
    MB_OK + MB_ICONINFORMATION);
end;

procedure TMainForm.GetValues(var a, b, Eps: extended);
begin
{krajni body}
  if (EditValueA.Text <> '') and (EditValueB.Text <> '') then begin
    a:= StrToFloat(EditValueA.Text);
    b:= StrToFloat(EditValueB.Text);
  end
  else
    SetLastErrorCode(ERR_M_BORDERS_NOT_SET);

{presnost}
  if GetLastErrorCode = ERR_OK then
    if RBRucne.Checked then
      if EditEps.Text <> '' then
        Eps:= StrToFloat(EditEps.Text)
      else
        SetLastErrorCode(ERR_M_EPS_NOT_SET)
    else
      Eps:= 0.5 * Power(10, -SpinEpsExp.Value);
end;

{**********************  vypocet  *************************************}
procedure TMainForm.ButtonCountClick(Sender: TObject);
var
  bOk: boolean;
  a, b, Eps: extended;
  NewtonRes: extended;
  i: integer;

  AproxNewton: TArrResult;
  CountNewton: integer;


begin
  SetLAstErrorCode(ERR_OK);

  MemoNewton.Lines.Clear;
  MemoHalf.Lines.Clear;


  if EditFunction.Text = '' then
    SetLastErrorCode(ERR_M_EXPR_NOT_SET)
  else
    GetValues(a, b, Eps);

  FormGraph.SetBorderPoints(a, b); {nastaveni grafu }

  if GetLastErrorCode = ERR_OK then begin


  {Newtonova metoda}
    NewtonRes:= Newton.Aproxime(EditFunction.Text, a, b, Eps, AproxNewton,
                                  CountNewton, bOk);

    if bOk then begin
     {zobrazeni vysledku}
      LabelResNewton.Caption:= FloatToStr(NewtonRes);
      for i:= 0 to CountNewton do
        MemoNewton.Lines.Add(
          IntToStr(i) + ': ' + FloatToStr(AproxNewton[i]));

      FormGraph.SetNewton(AproxNewton, CountNewton); {nastaveni grafu }

    end;

  end;


  if GetLastErrorCode <> ERR_OK then
    Application.MessageBox(PChar(GetErrorName(GetLastErrorCode)),
                                  'Nastala chyba', MB_OK + MB_ICONWARNING)
  else
    if FormGraph.Visible then begin
      FormGraph.SetZoom(1);
      FormGraph.DrawGraph;
    end;


  SetLastErrorCode(ERR_OK);
end;
{**********************  vypocet  *************************************}


procedure TMainForm.RBRucneClick(Sender: TObject);
begin
  EditEps.Enabled:= true;
  SpinEpsExp.Enabled:= false;
end;

procedure TMainForm.RBZvolitClick(Sender: TObject);
begin
  EditEps.Enabled:= false;
  SpinEpsExp.Enabled:= true;
end;

procedure TMainForm.ButtonShowGraphClick(Sender: TObject);
begin
  if not FormGraph.Visible then
    FormGraph.Show
  else
    FormGraph.Hide;
end;



procedure TMainForm.ButtonShowAboutClick(Sender: TObject);
begin
  Application.MessageBox('Projekt do NMP' +#13+#10 +
                         'Vytvoøili : Adam Husár, xhusar01' + #13 + #10 +
                         '                Marek ..............' + #13 + #10 +
                         'Dne : 20. 4. 2003',
                         'About...',
                         MB_OK + MB_ICONINFORMATION);

end;

procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Screen.Cursor:= crDefault;
end;

end.


