::: µ¨ÆÄÀÌ Tip&Trick :::

µ¨ÆÄÀÌ Tip&Trick ¼º°Ý¿¡ ¸ÂÁö ¾Ê´Â ±¤°í,ºñ¹æ,Áú¹®ÀÇ ±ÛÀº Áï½Ã »èÁ¦Çϸç
³»¿ëÀ» º¹»çÇÏ¿© »ç¿ëÇÒ °æ¿ì ¹Ýµå½Ã À̰÷(http://www.howto.pe.kr)À» Ãâó·Î ¸í½ÃÇÏ¿© ÁÖ¼¼¿ä


Category

  ±è¿µ´ë(2004-08-09 20:03:22, Hit : 6270, Vote : 1155
 http://www.howto.pe.kr
 ¼ö½Ä(Expression) °è»ê±â

unit Unit1;

interface

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

type
  TMathtype=(mtnil,mtoperator,mtlbracket,mtrbracket,mtoperand);
  TMathOperatortype=(monone,moadd,mosub,modiv,momul,mopow);
  pmathchar = ^Tmathchar;
  TMathChar = record
    case mathtype: Tmathtype of
      mtoperand:(data:extended);
      mtoperator:(op:TMathOperatortype);
  end;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
   input,output,stack: Array of tmathchar;
   fmathstring: String;
   function calculate(operand1,operand2,operator:Tmathchar):extended;
   function getoperator(c:char):TMathOperatortype;
   function getoperand(mid:integer;var len:integer):extended;
   procedure processstring;
   procedure convertinfixtopostfix;
   function isdigit(c:char):boolean;
   function isoperator(c:char):boolean;
   function getprecedence(mop:TMathOperatortype):integer;
  public
    { Public declarations }
   function MathResult(expr: String): extended;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.calculate(operand1,operand2,operator:Tmathchar):extended;
begin
  result:=0;
  case operator.op of
    moadd:
     result:=operand1.data + operand2.data;
    mosub:
     result:=operand1.data - operand2.data;
    momul:
     result:=operand1.data * operand2.data;
    modiv:
     if (operand1.data<>0) and (operand2.data<>0) then
       result:=operand1.data / operand2.data
     else
       result:=0;
    mopow: result:=power(operand1.data,operand2.data);
  end;
end;

function TForm1.MathResult(expr: String): extended;
var
  i:integer;
  tmp1,tmp2,tmp3:tmathchar;
begin
  fmathstring := expr;
  
  convertinfixtopostfix;
  setlength(stack,0);
  for i:=0 to length(output)-1 do
  begin
    if output[i].mathtype=mtoperand then
    begin
      setlength(stack,length(stack)+1);
      stack[length(stack)-1]:=output[i];
    end
    else if output[i].mathtype=mtoperator then
    begin
      tmp1:=stack[length(stack)-1];
      tmp2:=stack[length(stack)-2];
      setlength(stack,length(stack)-2);
      tmp3.mathtype:=mtoperand;
      tmp3.data:=calculate(tmp2,tmp1,output[i]);
      setlength(stack,length(stack)+1);
      stack[length(stack)-1]:=tmp3;
    end;
  end;
  result:=stack[0].data;
  setlength(stack,0);
  setlength(input,0);
  setlength(output,0);
end;

function TForm1.getoperator(c:char):TMathOperatortype;
begin
  result:=monone;
  if c='+' then
    result:=moadd
  else if c='*' then
    result:=momul
  else if c='/' then
    result:=modiv
  else if c='-' then
    result:=mosub
  else if c='^' then
    result:=mopow;
end;

function TForm1.getoperand(mid:integer;var len:integer):extended;
var
  i,j:integer;
  tmpnum:string;
begin
  j:=1;
  for i:=mid to length(fmathstring)-1 do
  begin
    if isdigit(fmathstring[i]) then
    begin
      if j<=20 then
        tmpnum:=tmpnum+fmathstring[i];
      j:=j+1;
    end
    else
      break;
  end;
  result:=strtofloat(tmpnum);
  len:=length(tmpnum);
end;

procedure TForm1.processstring;
var
  i:integer;
  numlen:integer;
begin
  i :=0;
  numlen:=0;
  setlength(output,0);
  setlength(input,0);
  setlength(stack,0);
  fmathstring:='('+fmathstring+')';
  setlength(input,length(fmathstring));
  while i<=length(fmathstring)-1 do
  begin
   if fmathstring[i+1]='(' then
    begin
     input[i].mathtype:=mtlbracket;
     i:=i+1;
    end
   else if fmathstring[i+1]=')' then
    begin
     input[i].mathtype:=mtrbracket;
     i:=i+1;
    end
   else if isoperator(fmathstring[i+1]) then
    begin
     input[i].mathtype:=mtoperator;
     input[i].op:=getoperator(fmathstring[i+1]);
     i:=i+1;
    end
   else if isdigit(fmathstring[i+1]) then
    begin
     input[i].mathtype:=mtoperand;
     input[i].data:=getoperand(i+1,numlen);
     i:=i+numlen;
    end;
  end;
end;


function TForm1.isoperator(c:char):boolean;
begin
  result:=false;
  if (c='+') or (c='-') or (c='*') or (c='/') or (c='^') then
    result:=true;
end;

function TForm1.isdigit(c:char):boolean;
begin
  result:=false;
  if ((integer(c)> 47) and (integer(c)< 58)) or (c='.') then
    result:=true;
end;

function TForm1.getprecedence(mop:TMathOperatortype):integer;
begin
  result:=-1;
  case mop of
    moadd: result:=1;
    mosub: result:=1;
    momul: result:=2;
    modiv: result:=2;
    mopow: result:=3;
  end;
end;

procedure TForm1.convertinfixtopostfix;
var
  i,j,prec:integer;
begin
  processstring;
  for i:=0 to length(input)-1 do
  begin
   if input[i].mathtype=mtoperand then
    begin
     setlength(output,length(output)+1);
     output[length(output)-1]:=input[i];
    end
   else if input[i].mathtype=mtlbracket then
    begin
     setlength(stack,length(stack)+1);
     stack[length(stack)-1]:=input[i];
    end
   else if input[i].mathtype=mtoperator then
    begin
     prec:=getprecedence(input[i].op);
     j:=length(stack)-1;
     if j>=0 then
      begin
       while(getprecedence(stack[j].op)>=prec) and (j>=0) do
        begin
         setlength(output,length(output)+1);
         output[length(output)-1]:=stack[j];
         setlength(stack,length(stack)-1);
         j:=j-1;
        end;
       setlength(stack,length(stack)+1);
       stack[length(stack)-1]:=input[i];
      end;
    end
   else if input[i].mathtype=mtrbracket then
    begin
     j:=length(stack)-1;
     if j>=0 then
      begin
       while(stack[j].mathtype<>mtlbracket) and (j>=0) do
        begin
         setlength(output,length(output)+1);
         output[length(output)-1]:=stack[j];
         setlength(stack,length(stack)-1);
         j:=j-1;
        end;
       if j>=0 then
        setlength(stack,length(stack)-1);
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text := FloatToStr(MathResult(Edit1.Text));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // ¼ö½Ä¿¡ °ø¹é(whitespace)Àº »ç¿ëÇÏÁö ¸¶¼¼¿ä
  Edit1.Text := '10/(2+3)*10';
end;

end.





171   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Ç¥ÁØ TListBox ¿¡ Radio ¹öư ¿Ã¸®±â  ±è¿µ´ë 2004/07/27 4237 1158
170   [½Ã½ºÅÛ] ½ÇÇàÁßÀÎ ¸ðµç ÇÁ·Î¼¼½ºÀÇ Domain, User ±¸ÇÏ±â  ±è¿µ´ë 2004/07/27 5279 1271
169   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] RichEdit ÀÇ ¸¶¿ì½º Ä¿¼­ ¾Æ·¡ÀÇ ±ÛÀÚ ±¸ÇÏ±â  ±è¿µ´ë 2004/07/27 4766 1109
168   [½Ã½ºÅÛ] Keyboard hook À» »ç¿ëÇÑ OnKeyDown ±¸Çö  ±è¿µ´ë 2004/07/27 5339 1362
167   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] TListBox ÀÇ ¸¶¿ì½º Ä¿¼­ ¾Æ·¡ÀÇ ¾ÆÀÌÅÛ ±¸ÇÏ±â  ±è¿µ´ë 2004/07/27 3934 1058
166   [COM/OLE] ƯÁ¤ À¥ÆäÀÌÁöÀÇ ¸ðµç Link URL ±¸ÇÏ±â  ±è¿µ´ë 2004/07/27 5874 1550
165   [COM/OLE] PDF ActiveX »ç¿ëÇÏ±â  ±è¿µ´ë 2004/08/02 5361 1253
164   [COM/OLE] ƯÁ¤ »çÀÌÆ®ÀÇ ³»¿ëÀ» JPG ·Î ÀúÀåÇÏ±â  ±è¿µ´ë 2004/08/02 5369 1198
163   [COM/OLE] ƯÁ¤ »çÀÌÆ®ÀÇ form À» °­Á¦ submit ÇÏ±â  ±è¿µ´ë 2004/08/02 6001 1479
162   [COM/OLE] IE ¿¡ Á÷Á¢ ÀÔ·ÂÇÑ URL ¸ñ·Ï ±¸ÇÏ±â  ±è¿µ´ë 2004/08/02 4544 1121
161   [½Ã½ºÅÛ] ¿ÜºÎ ÇÁ·Î±×·¥ Á¾·á ½ÃŰ±â  ±è¿µ´ë 2004/08/02 6671 1255
160   [½Ã½ºÅÛ] DOS (¸í·É ÇÁ·ÒÇÁÆ®) âÀÇ »ö»ó, È­¸é ¸ðµå ¹Ù²Ù±â  ±è¿µ´ë 2004/08/02 5726 1668
159   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Search and Select  ±è¿µ´ë 2004/08/03 5920 1636
158   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Zlib ¸¦ ÀÌ¿ëÇÑ ¾ÐÃà°ú ÇØÁ¦  ±è¿µ´ë 2004/08/03 5460 1258
157   [½Ã½ºÅÛ] ÀÏÁ¤½Ã°£ °æ°ú ÈÄ À©µµ¿ìÁî Á¾·áÇÏ±â  ±è¿µ´ë 2004/08/03 4729 1168
156   [½Ã½ºÅÛ] DOS ¸í·É¾î ½ÇÇàÇÏ°í °á°ú ¹Þ¾Æ¿À±â (Win2k,XP)  ±è¿µ´ë 2004/08/03 5818 1414
155   [½Ã½ºÅÛ] À©µµ¿ìÁî È­¸é Àá±×±â  ±è¿µ´ë 2004/08/03 5278 1345
154   [½Ã½ºÅÛ] À©µµ¿ìÁî »ç¿ëÀÚ °èÁ¤ Á¤º¸ ±¸ÇÏ±â  ±è¿µ´ë 2004/08/03 5444 1449
153   [½Ã½ºÅÛ] ÆÄÀÏÀÇ ¼ÒÀ¯ÀÚ¿Í µµ¸ÞÀÎ ±¸ÇÏ±â  ±è¿µ´ë 2004/08/04 4367 1209
152   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Hex Viewer  ±è¿µ´ë 2004/08/04 4405 1113
151   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] RichEdit ¿¡ URL link ¸¸µé±â  ±è¿µ´ë 2004/08/04 6049 1406
150   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] µ¿ÀûÀ¸·Î »ý¼ºÇÑ TLabel ¸¶¿ì½º·Î À̵¿½ÃŰ±â  ±è¿µ´ë 2004/08/04 6533 1920
149   [À©µµ¿ìÁî API] ¼¼·Î ŸÀÌÆ²¹Ù ¸¸µé±â  ±è¿µ´ë 2004/08/04 5211 1404
148   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] TPanel ·Î ¸¸µç ÈùÆ®  ±è¿µ´ë 2004/08/05 4636 1187
147   [½Ã½ºÅÛ] À©µµ¿ìÁî ºÎÆÃ ¸ðµå(Á¤»ó, ¾ÈÀü)  ±è¿µ´ë 2004/08/05 4292 1217
146   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] ListView ³»¿ëÀ» ÆÄÀÏ·Î ÀúÀåÇÏ°í ºÒ·¯¿À±â  ±è¿µ´ë 2004/08/05 4693 1128
145   [À©µµ¿ìÁî API] ÈÞÁöÅëÀÌ ºñ¾îÀÖ´ÂÁö È®ÀÎÇÏ±â  ±è¿µ´ë 2004/08/05 4993 1261
144   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] TreeView ÀÇ ³ëµå¸¦ º¼µå(Bold)·Î °­Á¶ÇÏ±â  ±è¿µ´ë 2004/08/05 7621 1739
143   [½Ã½ºÅÛ] ÇÁ·Î±×·¥ Á¦°Å(Uninstall) ¸ñ·Ï ±¸ÇÏ±â  ±è¿µ´ë 2004/08/05 4993 1319
142   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Adobe Acrobat ÀÌ ¼³Ä¡µÇ¾ú´ÂÁö °Ë»çÇÏ±â  ±è¿µ´ë 2004/08/06 4629 1158
141   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Shockwave Flash °¡ ¼³Ä¡µÇ¾ú´ÂÁö °Ë»çÇÏ°í ¹öÀüÁ¤º¸ Àоî¿À±â  ±è¿µ´ë 2004/08/06 6113 1551
140   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] Title bar ¿¡ ¹®ÀÚ ¿Ã¸®±â  ±è¿µ´ë 2004/08/06 4784 1290
139   [À©µµ¿ìÁî API] Type Library ¸ñ·Ï ±¸ÇÏ±â  ±è¿µ´ë 2004/08/06 4620 1248
138   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] º®¿¡ µé·¯ºÙ´Â ÀÚ¼®Æû ¸¸µé±â  ±è¿µ´ë 2004/08/06 4802 1208
137   [COM/OLE] Shockwave Flash ActiveX »ç¿ëÇϱ⠿¹Á¦  ±è¿µ´ë 2004/08/09 5128 1370
  [ÀϹÝ/ÄÄÆ÷³ÍÆ®] ¼ö½Ä(Expression) °è»ê±â  ±è¿µ´ë 2004/08/09 6270 1155
135   [³×Æ®¿÷/ÀÎÅͳÝ] Ping ¼Ò½º  ±è¿µ´ë 2004/08/09 7084 1300
134   [ÀϹÝ/ÄÄÆ÷³ÍÆ®] µµ³Ó(Doughnut) ¸ð¾çÀÇ Æû ¸¸µé±â  ±è¿µ´ë 2004/08/09 4733 1210
133   [½Ã½ºÅÛ] Sleep Áß¿¡µµ ŸÀÌ¸Ó À̺¥Æ® ¹ß»ý½ÃŰ±â  ±è¿µ´ë 2004/08/09 5998 1417
132   [À©µµ¿ìÁî API] IE Àӽà ÀÎÅÍ³Ý ÆÄÀÏ Æú´õ ºñ¿ì±â2  ±è¿µ´ë 2004/08/11 5020 1315

[ÀÌÀü 10°³] [1].. 21 [22][23][24][25]
 

Copyright 1999-2023 Zeroboard / skin by zero