
18.01.2007, 20:48
|
 |
Новичок
|
|
Регистрация: 10.01.2007
Сообщения: 66
Репутация: 10
|
|
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
z: Char;
ipos: Integer;
implementation
{$R *.dfm}
function Calculate(SMyExpression: string; digits: Byte): string;
// Calculate a simple expression
// Supported are: Real Numbers, parenthesis
function StrToReal(chaine: string): Real;
var
r: Real;
Pos: Integer;
begin
Val(chaine, r, Pos);
if Pos > 0 then Val(Copy(chaine, 1, Pos - 1), r, Pos);
Result := r;
end;
function RealToStr(inreal: Extended; digits: Byte): string;
var
S: string;
begin
Str(inreal: 0: digits, S);
realToStr := S;
end;
procedure NextChar;
var
s: string;
begin
if ipos > Length(SMyExpression) then
begin
z := #9;
Exit;
end
else
begin
s := Copy(SMyExpression, ipos, 1);
z := s[1];
Inc(ipos);
end;
if z = ' ' then nextchar;
end;
function Expression: Real;
var
w: Real;
function Factor: Real;
var
ws: string;
begin
Nextchar;
if z in ['0'..'9'] then
begin
ws := '';
repeat
ws := ws + z;
nextchar
until not (z in ['0'..'9', '.']);
Factor := StrToReal(ws);
end
else if z = '(' then
begin
Factor := Expression;
nextchar
end
else if z = '+' then Factor := +Factor
else if Z = '-' then Factor := -Factor;
end;
function Term: Real;
var
W: Real;
begin
W := Factor;
while Z in ['*', '/'] do
if z = '*' then w := w * Factor
else
w := w / Factor;
Term := w;
end;
begin
w := term;
while z in ['+', '-'] do
if z = '+' then w := w + term
else
w := w - term;
Expression := w;
end;
begin
ipos := 1;
Result := RealToStr(Expression, digits);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sMyExpression: string;
begin
sMyExpression := Edit1.text;
Edit2.Text:=('= ' + Calculate(sMyExpression,0));
end;
end.
ответил на свой вопрос частично он делает действия не все а можно например если к ним написать % там к примеру другие действия как?
|