HP Forums

Full Version: Mixture Program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's a little mixture program for combining quantities of different things at different prices. It calculates the unknowns. Why? Why not?

Tom L

Code:
EXPORT MIXTURE()
BEGIN
  //HFormat:=1; HDigits:=7;
  LOCAL A,U,M,N1,N2,N3,C1,C2;
  LOCAL C3,L1,M1,K1,K2,X5,FLAG;
  REPEAT
    PRINT();
    INPUT({{U,[2]},{M,[2]}},"UNITS AND DENOMINATION",{"UNITS","DENOM"},{"LBS, OZ, ETC.","DOLLARS, CENTS, ETC."}); 
    INPUT({N1,C1},U+","+M+" PER "+U+"-FIRST ITEM",{"N1","C1"},{"IF A QUANTITY IS UNKNOWN, ENTER ZERO","IF A QUANTITY IS UNKNOWN, ENTER ZERO"});
    INPUT({N2,C2},U+","+M+" PER "+U+"-SECOND ITEM",{"N2","C2"},{"IF A QUANTITY IS UNKNOWN, ENTER ZERO","IF A QUANTITY IS UNKNOWN, ENTER ZERO"});
    INPUT({N3,C3},U+","+M+" PER "+U+"-MIXTURE");
    FLAG:=0;
    A:=(C1+C2)/2;
    IF N1==0 AND N2==0 THEN
      IF A<=C3 THEN
        L1:=N3/2;
        M1:=N3;
      ELSE
        L1:=0;
        M1:=N3/2;
      END;
      K2:=N3*C3;
      FOR X:=L1 TO M1 STEP .1 DO
        IF X*C1+(N3-X)*C2==N3*C3 THEN 
          N1:=X;
          N2:=N3-X;
          FLAG:=1;
          BREAK;
        END;
        K1:=N3*C3-X*C1-(N3-X)*C2;
        IF ABS(K1)<=ABS(K2) THEN
          K2:=K1;
          X5:=X;
        END;
      END;    
      IF FLAG==0 THEN
        IF IP(X5+.0001)==IP(X5+.001) THEN N1:=IP(X5+.001) ELSE N1:=X5;END;
        N2:=N3-N1;
        FLAG:=1;                
      END;
    ELSE 
      IF N1==0 AND N3==0 OR N2==0 AND N3==0 THEN 
        IF N2:=0 THEN
          C1:=C2;
          N1:=N2;
        END;        
        K2:=N2*C2*100;
        IF A<=C3 THEN
          L1:=0;
          M1:=N2;
        ELSE
          L1:=N2;
          M1:=(100*N2);
        END;
        FOR X:=L1 TO M1 STEP .1 DO
          IF X*C1+N2*C2==(N2+X)*C3 THEN
            N1:=X;
            N3:=N1+N2;
            FLAG:=1;              
            BREAK;
          END;
          K1:=(N2+X)*C3-X*C1-N2*C2;
          IF ABS(K1)<=ABS(K2) THEN
            K2:=K1;
            X5:=X;
          END;
        END;  
        IF FLAG==0 THEN
          IF IP(X5+.0001)==IP(X5+.001) THEN N1:=IP(X5+.001);ELSE N1:=X5;END;
          N3:=N2+N1;
          FLAG:=1;     
        END;
      END;  
    END;    
    IF FLAG==0 THEN
      IF N1==0 THEN N1:=(N3*C3-N2*C2)/C1;END;
      IF N2==0 THEN N2:=(N3*C3-N1 *C1)/C2;END;
      N3:=N1+N2;
      IF C3==0 THEN C3:=(N1*C1+N2*C2)/N3;END;
    END;
  
    PRINT("MIX "+N1+" "+U+" AT "+C1+" "+M+" PER "+U);
    PRINT("WITH "+N2+" "+U+" AT "+C2+" "+M+" PER "+U);
    PRINT("TO GET A MIXTURE OF "+N3+" "+U+" AT "+C3+" "+M+" PER "+U);
    WAIT;
    INPUT({{A,[2]}},"ANYTHING ELSE (YES OR NO)?");
  UNTIL UPPER(A)=="NO";
END;
Reference URL's