Wątek przeniesiony 2014-05-13 12:42 z Delphi i Pascal przez ŁF.

Program liczący pola figur

0

Witam.
Miałem do napisania program liczący pola figur i w zasadzie jest skończony, ale mam pewien problem. Mam przycisk C który czyści pola po wpisaniu liczb, jest jednak pewien problem, gdy go wciskam to przy czyszczeniu każdego pola z liczbą pojawia się komunikat "is not valid integer value". Wie ktoś jak go usunąć?

Tutaj plik z programem

0

Tutaj ci powstaje ten bład(oczywiscie w kazdym edit'cie):

procedure TForm1.Edit1Change(Sender: TObject);
begin
  A:=StrToint (Edit1.Text);
end; 

najłatwiej bedzie dodac

 if Edit.Text <>'' then

i powinno byc ok.

Edit;
A i wywal

 procedure TForm1.Edit4Change(Sender: TObject);
begin
  Pole:=StrToint (Edit3.Text);
end;

procedure TForm1.Edit5Change(Sender: TObject);
begin
  Obwód:=StrToint (Edit3.Text);
end;

bo jest bezsensu:D( no moze sens jakis mialoby jakbyc potem uzywal do czegos zmiennych Obwód i Pole, ale nie używasz wiec to ci niepotrzebne);

1

Nie czyść tylko wstawiaj zera.
Może rozważ takie podejście:

object Form1: TForm1
  Left = 315
  Top = 211
  Width = 354
  Height = 284
  Caption = 'Pola Figur'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object LParam1: TLabel
    Left = 192
    Top = 25
    Width = 11
    Height = 21
    Alignment = taRightJustify
    Caption = 'A'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object LParam2: TLabel
    Left = 193
    Top = 73
    Width = 10
    Height = 21
    Alignment = taRightJustify
    Caption = 'B'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    Visible = False
  end
  object LParam3: TLabel
    Left = 193
    Top = 121
    Width = 10
    Height = 21
    Alignment = taRightJustify
    Caption = 'C'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -17
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    Visible = False
  end
  object CArea: TLabel
    Left = 14
    Top = 165
    Width = 38
    Height = 24
    Caption = 'Pole'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object CPerimeter: TLabel
    Left = 14
    Top = 211
    Width = 62
    Height = 24
    Caption = 'Obwód'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object EValue1: TEdit
    Left = 209
    Top = 22
    Width = 121
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 0
    Text = '0'
    OnChange = DataChange
  end
  object EValue2: TEdit
    Left = 209
    Top = 70
    Width = 121
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 1
    Text = '0'
    Visible = False
    OnChange = DataChange
  end
  object EValue3: TEdit
    Left = 209
    Top = 118
    Width = 121
    Height = 24
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    TabOrder = 2
    Text = '0'
    Visible = False
    OnChange = DataChange
  end
  object EArea: TEdit
    Left = 82
    Top = 163
    Width = 192
    Height = 24
    TabStop = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentColor = True
    ParentFont = False
    ReadOnly = True
    TabOrder = 3
  end
  object EPerimeter: TEdit
    Left = 82
    Top = 211
    Width = 192
    Height = 24
    TabStop = False
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentColor = True
    ParentFont = False
    ReadOnly = True
    TabOrder = 4
  end
  object Figure: TRadioGroup
    Left = 10
    Top = 12
    Width = 120
    Height = 130
    Caption = '&Figura'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ItemIndex = 0
    Items.Strings = (
      'Kwadrat'
      'Prostokąt'
      'Koło'
      'Trójkąt')
    ParentFont = False
    TabOrder = 5
    OnClick = FigureClick
  end
end
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Figure: TRadioGroup;
    EValue1: TEdit;
    EValue2: TEdit;
    EValue3: TEdit;
    LParam1: TLabel;
    LParam2: TLabel;
    LParam3: TLabel;
    CArea: TLabel;
    EArea: TEdit;
    CPerimeter: TLabel;
    EPerimeter: TEdit;
    procedure DataChange(Sender: TObject);
    procedure FigureClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    LParam:array[0..2]of TLabel;
    EParam:array[0..2]of TEdit;
    EResult:array[0..1]of TEdit;
  public
  end;

var Form1:TForm1;

implementation uses
  Math;

{$R *.dfm}

const OnpData:array[0..3,0..4]of String=
(
  ('Bok','','','AA*','4A*'),
  ('Bok A','Bok B','','AB*','2AB+*'),
  ('Promień','','','PAA**','PA2**'),
  ('Bok A','Bok B','Bok C','ABC++BC+A-*AC+B-*AB+C-*S4/','ABC++')
);

procedure TForm1.FormCreate(Sender:TObject);
begin
  LParam[0]:=LParam1;
  LParam[1]:=LParam2;
  LParam[2]:=LParam3;
  EParam[0]:=EValue1;
  EParam[1]:=EValue2;
  EParam[2]:=EValue3;
  EResult[0]:=EArea;
  EResult[1]:=EPerimeter;
  FigureClick(nil);
end;

procedure TForm1.FigureClick(Sender:TObject);
var Str:String;
var F,I:Integer;
begin
  F:=Figure.ItemIndex;
  for I:=0 to 2 do
  begin
    Str:=OnpData[F,I];
    if Length(Str)>0 then
    begin
      LParam[I].Caption:=Str;
      LParam[I].Visible:=true;
      EParam[I].Visible:=true;
    end
    else
    begin
      LParam[I].Visible:=false;
      EParam[I].Visible:=false;
    end;
  end;
  DataChange(nil);
end;

procedure TForm1.DataChange(Sender:TObject);
var Stack:array of Double;
var F,I,K:Integer;
var Eq,Msg:String;
var V:Double;
procedure Push(V:Double);
begin
  SetLength(Stack,Length(Stack)+1);
  Stack[Length(Stack)-1]:=V;
end;
function Pop:Double;
begin
  Result:=Stack[Length(Stack)-1];
  SetLength(Stack,Length(Stack)-1);
end;
function GetValue(V:Integer):Double;
begin
  try
    Result:=StrToFloat(EParam[V].Text);
    LParam[V].Font.Color:=clWindowText;
  except
    Result:=0;
    LParam[V].Font.Color:=clRed;
    Msg:='Niekompletne dane';
  end;
end;
begin
  F:=Figure.ItemIndex;
  for I:=0 to 1 do
  begin
    SetLength(Msg,0);
    SetLength(Stack,0);
    Eq:=OnpData[F,I+3];
    for K:=1 to Length(Eq) do
    begin
      case Eq[K] of
        'A': Push(GetValue(0));
        'B': Push(GetValue(1));
        'C': Push(GetValue(2));
        'P': Push(Pi);
        '+': begin V:=Pop; Push(Pop+V); end;
        '-': begin V:=Pop; Push(Pop-V); end;
        '*': begin V:=Pop; Push(Pop*V); end;
        '/': begin V:=Pop; Push(Pop/V); end;
        'S': try Push(Sqrt(Pop)); except Msg:='Niepoprawne dane'; end;
        '0'..'9': Push(Ord(Eq[K])-Ord('0'));
      end;
      if Length(Msg)>0 then Break;
    end;
    if Length(Msg)<=0 then Msg:=FloatToStr(Pop);
    EResult[I].Text:=Msg;
  end;
end;

end.

Najważniejszą zaletą jest to że dodanie kolejnej figury robi się szybko, sprawnie i w jednym miejscu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1