
function data=readData(filename)
    f=mopen(filename)
    n=0
    data=struct()
    while (n<>-1),
        [n,nom,h,s]=mfscanf(f,"%s%f%f\n");
        if (n<>-1) then
            data(nom)=struct('H',h,'S',s); 
        end;
    end
    mclose(f)
endfunction    
            
function [H,S]=somme(data,som)
  sum = strsubst(som,"+}","@");
  sum = strsubst(sum,"^+","^@");
  c=tokens(som,'+');
  n=size(c);
  n=n(1);
  H=0;
  S=0;
  for i=1:n,
      x=tokens(c(i),"*");
      nx=size(x);
      nx=nx(1);
      if nx==1 then
        coef=1;
        nom=x(1);
      else
        coef=strtod(x(1));
        nom=x(2);
      end
      nom = strsubst(nom,"@","+}");
      nom = strsubst(nom,"^@","^+");
      H=H+coef*data(nom).H;
      S=S+coef*data(nom).S;
  end
endfunction
            
function [DH,DS]=reaction(data,equation)
  n=strindex(equation,"=");
  som=strsplit(equation,[n-1 n]);
  reactifs=som(1);
  produits=som(3);
  [DH,DS]=somme(data,produits);
  [h,s]=somme(data,reactifs);
  DH=DH-h;
  DS=DS-s;
endfunction
            
function K=constanteK(DH,DS,T)
  R=8.31;
  K=exp(-(DH*1e3-T*DS)/(R*T)) 
endfunction
            
function plotK(DH,DS,Tmin,Tmax,N)
    T=[Tmin:(Tmax-Tmin)/(N-1):Tmax];
    K=T;
    R=8.31;
    for i=1:N,
        K(i)=-(DH*1e3-T(i)*DS)/(R*T(i));
    end;
    plot2d(T,K)
    xtitle("Constante d''equilibre",'T (K)','ln(K)')
endfunction
            