Calcular pesos moleculares

2598 vistas

CalcularMW es una función que se encarga de calcular el peso molecular de un compuesto indicado en un texto, la estructura de escritura es: todo número que se encuentre antes del compuesto multiplica el compuesto completo, y todo numero que venga despues de un elemento multiplica al elemento en si



delphi
  1. interface
  2.  
  3.   function CalcularMW(Texto : string ) : Double;
  4.   function BuscarProximoElemento(Texto : string ; var inicio : Integer ; var
  5.   Numero, Error : boolean) : string;
  6.   function BuscarElemento( Texto : string ): integer;
  7.   function EsNumero(Texto : string) : boolean;
  8.  
  9.  
  10. const
  11.  
  12.   Elementos : array [1..77] of string =
  13.     ('AC','AG','AL','AR','AS','AT','AU','B' ,'BA','BE','BI','BR','C'
  14. ,'CA','CD','CL','CO','CR','CS','CU','FE','FE',
  15.     'FR','GA','GE','H' ,'HA','HE','HF','HG','IN','IN','IR','K'
  16. ,'KR','KU','LA','LI','MG','MN','MO','N' ,'NA','NB',
  17.     'NE','NI','O' ,'OS','P'
  18. ,'PB','PD','PO','PT','RA','RB','RE','RH','RN','RU','S'
  19. ,'SB','SC','SE','SI','SN','SR',
  20.     'TA','TC','TE','TI','TI','V' ,'W' ,'XE','Y' ,'ZN','ZR');
  21.  
  22.   ValElemen : array [1..77] of Double = (
  23.     227, 107.868, 26.98154, 39.948, 74.9216, 210, 196.9665, 10.81, 137.34,
  24. 9.01218,
  25.     208.9804, 79.904, 12.01115, 40.08, 112.4, 35.453, 58.9332, 51.996,
  26. 132.9054,
  27.     63.546, 55.847, 18.9984, 223, 69.72, 72.59, 1.0797, 260, 4.0026, 178.49,
  28. 200.59,
  29.     114.82, 126.9045, 192.22, 39.098, 83.8, 261, 138.9055, 6.941, 24.305,
  30. 54.938,
  31.     95.94, 14.0067, 22.98977, 92.9064, 20.179, 58.71, 15.9994, 190.2,
  32. 30.97376,
  33.     207.19, 106.4, 210, 195.09, 226.0254, 85.4678, 186.2, 102.9055, 222,
  34. 101.07,
  35.     32.06, 121.75, 44.9559, 78.96, 28.086, 118.69, 87.62, 180.9479, 98.9063,
  36. 127.6,
  37.     47.9, 204.37, 50.9414, 183.85, 131.3, 88.9059, 65.38, 91.22);



Y ahora la implementación de estas funciones



delphi
  1. implementation
  2.  
  3. function CalcularMW(Texto : string ) : Double;
  4. var
  5.   Inicio,
  6.   A            : Integer;
  7.   Variado      : String;
  8.   Numero,
  9.   Error        : Boolean;
  10.   Suma,
  11.   UltElemento,
  12.   Multiplicador : Double;
  13. begin
  14.   Inicio        := 1;
  15.   Multiplicador := 1;
  16.   Texto        := UpperCase(Trim(Texto));
  17.   Variado      := BuscarProximoElemento(Texto, Inicio, Numero, Error);
  18.   Suma          := 0;
  19.   UltElemento  := 0;
  20.   repeat
  21.     if Numero then
  22.     begin
  23.       if UltElemento <> 0 then Suma := Suma +
  24. UltElemento*StrToFloat(Variado)
  25.       else Multiplicador := StrToFloat(Variado);
  26.       Variado := BuscarProximoElemento(Texto, Inicio, Numero, Error);
  27.     end else
  28.     begin
  29.       A := BuscarElemento(Variado);
  30.       if A > 0 then
  31.         UltElemento := ValElemen[A];
  32.  
  33.       Variado := BuscarProximoElemento(Texto, Inicio, Numero, Error);
  34.       if not Numero then
  35.         Suma := Suma + UltElemento;
  36.     end;
  37.  
  38.   until Variado = '';
  39.  
  40.   Result := Multiplicador*Suma;
  41. end;
  42.  
  43. function BuscarProximoElemento(Texto : string ; var inicio : Integer ; var
  44. Numero, Error : boolean) : string;
  45. var
  46.   Listo : boolean;
  47.   A : integer;
  48. begin
  49.   Error := false;
  50.   Result := '';
  51.   Numero := false;
  52.  
  53.   if Inicio <= 0 then Inicio := 1;
  54.   if Inicio > Length(Texto) then Exit;
  55.  
  56.   if EsNumero(Texto[Inicio]) then
  57.   begin
  58.     Numero := true;
  59.     while (Inicio <= Length(Texto)) and ( EsNumero(Texto[Inicio]) ) do
  60.     begin
  61.       Result := Result + Texto[Inicio];
  62.       Inc(Inicio);
  63.     end;
  64.   end else
  65.   begin
  66.     while (Inicio <= Length(Texto)) and not ( EsNumero(Texto[Inicio]) ) do
  67.     begin
  68.       Result := Result + Texto[Inicio];
  69.       Inc(Inicio);
  70.     end;
  71.   end;
  72.  
  73.   if not Numero then
  74.   begin
  75.     Listo := false;
  76.  
  77.     for A := Length(Result) downto 1 do
  78.     begin
  79.       if BuscarElemento(Copy(Result, 1, A)) > 0 then
  80.       begin
  81.         Inicio := Inicio - (Length(Result) - A);
  82.         Result := Copy(Result, 1, A);
  83.         Listo := true;
  84.         Break;
  85.       end;
  86.     end;
  87.  
  88.     if not Listo then
  89.     begin
  90.       Inicio := Inicio - Length(Result);
  91.       Result := '';
  92.       Error := true;
  93.     end;
  94.   end;
  95. end;
  96.  
  97. function BuscarElemento( Texto : string ): integer;
  98. var
  99.   A : Integer;
  100. begin
  101.   Result := 0;
  102.   for A := Low(Elementos) to High(Elementos) do
  103.   begin
  104.     if Elementos[A] = Texto then
  105.     begin
  106.       Result := A;
  107.       break;
  108.     end;
  109.   end;
  110. end;
  111.  
  112. function EsNumero(Texto : string) : boolean;
  113. var
  114.   A: integer;
  115. begin
  116.   Texto := Trim(Texto);
  117.  
  118.   Result := true;
  119.   for A := 1 to Length(Texto) do
  120.   begin
  121.     if Pos(Texto[A],'0123456789,.+-E') <= 0 then
  122.     begin
  123.       Result := false;
  124.       break;
  125.     end;
  126.   end;
  127.  
  128.   if Result then
  129.   begin
  130.     try
  131.       StrTOFloat(Texto);
  132.     except
  133.       Result := false;
  134.     end;
  135.   end;
  136. end;