
Эксперт
   
Профиль
Группа: Завсегдатай
Сообщений: 2205
Регистрация: 2.12.2005
Репутация: 1 Всего: 0
|
Привет всем. Поставил перед собой задачу написать продвинутый канкулятор. Тоесть функцию которая из одычной строки в которой находиться пример(например '2+2' или ''2+2+sin(90)' или '2+2+sin(90+30/20)' - и тому подобное) находила бы решение. Вот мои первые наброски этой функции. Код | function podciot (s:String): string; var resultat:Real; i:Integer; cisla: StrArr; znaki: StrArr;
function udalit_xlam(a:StrArr):StrArr; var count,k:Integer; begin count:=0; for k:=0 to Length(a)-1 do begin if k+count<Length(a) then begin a[k]:=a[k+count]; while (a[k]='')and(k+count<Length(a))do begin count:=count+1; if k+count<Length(a) then a[k]:=a[k+count]; // ShowMessage(a[k]);
end; end;
end; SetLength(a,Length(a)-count); Result:=a; end;
procedure cut_masiv; var k:Integer; begin
cisla:=udalit_xlam(cisla);
//for k:=0 to Length(znaki)-1 do ShowMessage(znaki[k]); znaki:=udalit_xlam(znaki); //for k:=0 to Length(cisla)-1 do ShowMessage(cisla[k]);
end;
function Cislo(As_Arg: Char): Boolean; begin Cislo := As_Arg in ['0','1','2','3','4','5','6','7','8','9',',']; end;
function znak(As_Arg: Char): Boolean; begin znak := As_Arg in ['+','-','*','/']; end;
procedure split_to_elements; var i,k:Integer; position:Integer; temp:String; begin if pos('+-',s)<>0 then Delete(s,pos('+-',s),1); if pos('--',s)<>0 then Delete(s,pos('--',s),1); for i:=1 to Length(s) do begin if s[i]='.' then s[i]:=','; end;
for i:=1 to Length(s) do begin ////////////////////Naxozdenijr cisel if Cislo(s[i]) then begin if Cislo(s[i-1]) then begin temp:=(cisla[Length(cisla)-1])+s[i]; cisla[Length(cisla)-1]:=(temp); end else begin SetLength(cisla,Length(cisla)+1); cisla[Length(cisla)-1]:=(s[i]); if (i=2) and (s[1]='-') then cisla[Length(cisla)-1]:='-'+cisla[Length(cisla)-1]; end; end; if znak(s[i])and(i<>1) then begin SetLength(znaki,Length(znaki)+1); znaki[Length(znaki)-1]:=(s[i]); end; ///////////////////naxozdenije znakov end; end;
function naiti_resenije:string; var i:Integer; dump:Integer; begin try
for i:=0 to Length(s)-1 do begin if pos('sin(',s)<>0 then begin dump:=StrToInt(podciot(copy(s,pos('sin(',s)+4,pos(')',copy(s,pos('sin(',s)+4,Length(s)))-1))); Insert(FloatToStr(sin(DegToRad(dump))),s,pos('sin(',s)); Delete(s,pos('sin(',s),pos(')',copy(s,pos('sin(',s),Length(s)))); end;
if pos('cos(',s)<>0 then begin dump:=StrToInt(podciot(copy(s,pos('cos(',s)+4,pos(')',copy(s,pos('cos(',s)+4,Length(s)))-1))); Insert(FloatToStr(cos(DegToRad(dump))),s,pos('cos(',s)); Delete(s,pos('cos(',s),pos(')',copy(s,pos('cos(',s),Length(s)))); end;
if pos('tan(',s)<>0 then begin dump:=StrToInt(podciot(copy(s,pos('tan(',s)+4,pos(')',copy(s,pos('tan(',s)+4,Length(s)))-1))); Insert(FloatToStr(Tan(DegToRad(dump))),s,pos('tan(',s)); Delete(s,pos('tan(',s),pos(')',copy(s,pos('tan(',s),Length(s)))); end;
if pos('cot(',s)<>0 then begin dump:=StrToInt(podciot(copy(s,pos('cot(',s)+4,pos(')',copy(s,pos('cot(',s)+4,Length(s)))-1))); Insert(FloatToStr(Cotan(DegToRad(dump))),s,pos('cot(',s)); Delete(s,pos('cot(',s),pos(')',copy(s,pos('cot(',s),Length(s)))); end;
if pos('sqr(',s)<>0 then begin dump:=StrToInt(podciot(copy(s,pos('sqr(',s)+4,pos(')',copy(s,pos('sqr(',s)+4,Length(s)))-1))); Insert(FloatToStr(Sqrt(dump)),s,pos('sqr(',s)); Delete(s,pos('sqr(',s),pos(')',copy(s,pos('sqr(',s),Length(s)))); end;
end;
//ShowMessage(s); split_to_elements; resultat:=0; //for i:=0 to Length(znaki)-1 do ShowMessage(znaki[i]); for i:=0 to Length(znaki)-1 do begin if znaki[i]='*' then begin //ShowMessage(cisla[i]+' '+cisla[i+1]); cisla[i+1]:=(FloatToStr(StrToFloat(cisla[i])*StrToFloat(cisla[i+1]))); cisla[i]:=''; znaki[i]:=''; end; end; cut_masiv;
for i:=0 to Length(znaki)-1 do begin if znaki[i]='/' then begin cisla[i+1]:=(FloatToStr(StrToFloat(cisla[i])/StrToFloat(cisla[i+1])));cisla[i]:='';znaki[i]:='';end;
end; cut_masiv; for i:=0 to Length(znaki)-1 do begin if znaki[i]='+' then begin cisla[i+1]:=(FloatToStr(StrToFloat(cisla[i])+StrToFloat(cisla[i+1])));cisla[i]:='';znaki[i]:=''; end; end; //for i:=0 to Length(cisla)-1 do ShowMessage(cisla[i]); cut_masiv; //for i:=0 to Length(cisla)-1 do ShowMessage(cisla[i]); for i:=0 to Length(znaki)-1 do begin if znaki[i]='-' then begin cisla[i+1]:=(FloatToStr(StrToFloat(cisla[i])-StrToFloat(cisla[i+1])));cisla[i]:='';znaki[i]:=''; end; end; cut_masiv; for i:=0 to Length(cisla)-1 do begin Result:=cisla[i];
end; SetLength(cisla,0); SetLength(znaki,0); except ShowMessage('Îøèáêà'); end; end;
begin ShowMessage(s); Result:=naiti_resenije; end;
|
Функция не всегда выдает правильный ответ. Хочю услышать любые советы по осуштвлению это функции. Я новичок в Delphi.
|