unit expresion;

interface
uses SysUtils,dialogs;

function CalculaExpresionReal(cad:string):string;
function CalculaExpresionCadena(cad:string):string;
function CalculaParentesis(cad:string):string;
implementation

function Multiplica(cad:string):string;
var
i:integer;
aux,palabra,siguiente:string;
//ope:char;
resu:real;
begin
if cad<>'' then
begin
aux:='';
siguiente:='';
if (cad[1]='-')and(length(cad)=1) then cad:='-1';

if cad[1]<>'-' then
begin
//resu:=1
end
else
begin

//resu:=0;
end;

palabra:='';
//ope:=' ';
//ShowMessage('va amultiplicar '+cad);
for i:=length(cad) downto 1 do
begin

if (cad[i]='/')or(cad[i]='*')or(cad[i]='-')or(cad[i]='+')or (i=1)or(cad='%')or(cad='#') then
begin
//ShowMessage(palabra);
if (cad[i]='/') then
begin
//if palabra<>'' then
//if cad[i+1]='-' then
//palabra:='-'+palabra;
//ShowMessage(palabra);
if StrToFloat(palabra)<>0 then
resu:=1/StrToFloat(palabra)
else
resu:=0;

palabra:=FloatToStr(resu);
aux:='*'+palabra+aux;
//ShowMessage(aux);
//cuenta:=cuenta+1;
//aux:='';

//if cad[i]='/' then
//aux:='-'+aux;
end
else
begin
//ShowMessage(palabra);

aux:=cad[i]+palabra+aux;
end;//if esle
//ShowMessage(palabra);
//if (cad[i+1]<>'-')and(cad[i]='/') then
palabra:='';

end
else
begin

palabra:=cad[i]+palabra;
end;

end;// for

//ShowMessage('final '+aux);
cad:=aux;
//ShowMessage('final '+aux);
aux:='';


cad:=cad+';';
resu:=1;

for i:=1 to length(cad) do
begin

if (cad[i]='*')or(cad[i]=';') then
begin
resu:=resu*StrToFloat(aux);
//cuenta:=cuenta+1;
aux:='';

end
else
begin
aux:=aux+cad[i];
end;

end;//for


Str(resu:15:5,aux);
Multiplica:=aux;
end;//if cad<>''
end;


function encontrado(cad1,cad2:PChar):boolean;
var
P:Pchar;
begin
 P := StrPos(cad2,cad1);
  if P = nil then
    encontrado:=False
{    Canvas.TextOut(10, 10, 'subcadena no encontrada')}
  else
  encontrado:=True;
{    Canvas.TextOut(10, 10, 'subcadena encontrada en ' + IntToStr(P - cad2));}

end;



function CalculaExpresionCadena(cad:string):string;
var
i{,cuenta}:integer;
aux,palabra:string;
//suma:real;
//operadores:array[1..50,1..5] of string;
//ope:char;
begin
aux:='';
palabra:='';
//cuenta:=1;
//cad:=Edit1.Text;
cad:=cad+';';
//ope:='*';

for i:=1 to  length(cad) do
begin

if ((cad[i]='+')or(cad[i]='-')or(cad[i]='>')or(cad[i]=';')) then
begin
if cad[i]<>'>' then
palabra:=palabra+aux
else
begin
//checar esta opcion
if not encontrado(PChar(aux),PChar(palabra)) then
palabra:=palabra+aux;
end;
aux:='';
end
else
begin
aux:=aux+cad[i];
end;//if

end;//for
CalculaExpresionCadena:=palabra;

//ShowMessage(palabra);
//Label1.Caption:=

end;


function CalculaExpresionReal(cad:string):string;
var
i,cuenta:integer;
aux,palabra:string;
suma:real;
operadores:array[1..50,1..5] of string;
ope:char;
begin
palabra:='';
cuenta:=1;
//cad:=Edit1.Text;
if cad[1]='-' then cad:='0'+cad;

ope:='*';
aux:='';
i:=1;
cad:=cad+';';
while  i<length(cad) do
begin
if (cad[i]='-')and(cad[i+1]='-')then
begin
aux:=aux+'+';
i:=i+1;
end
else
aux:=aux+cad[i];
i:=i+1;
end;//while
cad:=aux;
//ShowMessage(cad);

cad:=cad+';';
aux:='';

for i:=1 to  length(cad) do
begin
if cad[i]<>' ' then
aux:=aux+cad[i];
end;
cad:=aux;
aux:='';
//ShowMessage('ddsfsd '+cad);

for i:=1 to  length(cad) do
begin

if ((cad[i]='+')or(cad[i]='-')or(cad[i]=';'))and(ope<>'*')and(ope<>'/')and(ope<>')') then
begin
Operadores[1,cuenta]:=aux;
cuenta:=cuenta+1;
aux:='';
if cad[i]='-' then aux:='-';

end
else
begin
aux:=aux+cad[i];
end;
ope:=cad[i];
end;///for

for i:=1 to cuenta-1 do
begin
try
Operadores[2,i]:=Multiplica(Operadores[1,i]);
except
//ShowMessage('error *'+Operadores[1,i]+'*');
end;
end;

suma:=0;
for i:=1 to cuenta-1 do
begin

try
suma:=suma+strToFloat(Operadores[2,i]);
except
//ShowMessage('error *'+Operadores[2,i]+'*');
end;


end;
Str(suma:15:4,aux);
CalculaExpresionReal:=aux;
//Label1.Caption:=

end;


function CalculaParentesis(cad:string):string;
var
i,cuenta:integer;
aux,palabra:string;
//suma:real;
operadores:array[1..50,1..5] of string;
begin
aux:='';
palabra:='';
cuenta:=1;
//cad:=Edit1.Text;
cad:=cad+';';



for i:=1 to  length(cad) do
begin
if (cad[i]='(')or(cad[i]=')')or(cad[i]=';') then
begin
if (cad[i]=')') then
begin
// aux:=aux+cad[i];
Operadores[2,cuenta]:='*';
end;

Operadores[1,cuenta]:=aux;

cuenta:=cuenta+1;
aux:='';
//if (cad[i]='(') then aux:=aux+cad[i];
end
else
begin
aux:=aux+cad[i];
end;
end;///for

for i:=1 to cuenta-1 do
begin
if Operadores[2,i]='*' then
Operadores[3,i]:=CalculaExpresionReal(Operadores[1,i])
else
Operadores[3,i]:=Operadores[1,i];

end;

aux:='';
for i:=1 to cuenta-1 do
begin

aux:=aux+Operadores[3,i];
end;

cad:='';
for i:=1 to length(aux) do
begin
if aux[i]<>' ' then
cad:=cad+aux[i];

end;

CalculaParentesis:=cad;

end;

function ChecaTablaOperadores(uno,dos:char):char;
var
valor:char;
begin
valor:=' ';
if (uno='+')and (dos='+') then
valor:='+';

if (uno='+')and (dos='-') then
valor:='-';

if (uno='-')and (dos='+') then
valor:='-';

if (uno='-')and (dos='-') then
valor:='+';

ChecaTablaOperadores:=valor;
end;

function CalculaOperadores(cad:string):string;
var
i{,cuenta}:integer;
aux,palabra:string;
//suma:real;
//operadores:array[1..50,1..5] of string;
ope:char;
begin
aux:='';
palabra:='';
//cuenta:=1;
//cad:=Edit1.Text;

aux:='';

i:=1;
if length(cad)>1 then
while i<=length(cad) do
begin
if i<>length(cad) then
begin
ope:=ChecaTablaOperadores(cad[i],cad[i+1]);
if ope<>' ' then
begin
aux:=aux+ope;
i:=i+1;
end
else
aux:=aux+cad[i];
end
else
aux:=aux+cad[i];

i:=i+1;
end;//while

CalculaOperadores:=aux;

end;


end.
