76 lines
2.4 KiB
Plaintext
76 lines
2.4 KiB
Plaintext
*process source xref attributes;
|
|
KNAPSACK_CONTINUOUS: Proc Options(main);
|
|
/*--------------------------------------------------------------------
|
|
* 19.09.2014 Walter Pachl translated from FORTRAN
|
|
*-------------------------------------------------------------------*/
|
|
Dcl (divide,float,hbound,repeat) Builtin;
|
|
Dcl SYSPRINT Print;
|
|
|
|
Dcl maxweight Dec Fixed(15,3);
|
|
maxweight = 15.0;
|
|
Dcl (total_weight,total_value) Dec Fixed(15,3) Init(0);
|
|
Dcl vpu Dec Float(15);
|
|
Dcl (i,j) Bin Fixed(31);
|
|
|
|
Dcl 1 item(9),
|
|
2 name Char(7),
|
|
2 weight Dec Fixed(15,3),
|
|
2 value Dec Fixed(15,3);
|
|
Dcl temp Like item;
|
|
|
|
Call init_item(1,'beef', 3.8, 36.0);
|
|
Call init_item(2,'pork', 5.4, 43.0);
|
|
Call init_item(3,'ham', 3.6, 90.0);
|
|
Call init_item(4,'greaves', 2.4, 45.0);
|
|
Call init_item(5,'flitch', 4.0, 30.0);
|
|
Call init_item(6,'brawn', 2.5, 56.0);
|
|
Call init_item(7,'welt', 3.7, 67.0);
|
|
Call init_item(8,'salami', 3.0, 95.0);
|
|
Call init_item(9,'sausage', 5.9, 98.0);
|
|
|
|
/* sort item in descending order of their value per unit weight */
|
|
do i = 2 To hbound(item);
|
|
j = i - 1;
|
|
temp = item(i);
|
|
do while(j>=1&item(j).value/item(j).weight<temp.value/temp.weight);
|
|
item(j+1) = item(j);
|
|
j = j - 1;
|
|
end;
|
|
item(j+1) = temp;
|
|
end;
|
|
Do i=1 To hbound(item);
|
|
Put Edit(i,item(i))(Skip,f(2),x(2),a(7),2(f(8,3)));
|
|
End;
|
|
i = 0;
|
|
Put Skip;
|
|
Put Edit('Item Weight Value')(Skip,a);
|
|
do i=1 By 1 while(i < hbound(item) & total_weight < maxweight);
|
|
if total_weight+item(i).weight < maxweight then Do;
|
|
total_weight = total_weight + item(i).weight;
|
|
total_value = total_value + item(i).value;
|
|
Put Edit(item(i))(Skip,a(7),2(f(8,3)));
|
|
End;
|
|
Else Do;
|
|
vpu=divide(item(i).value,item(i).weight,15,8);
|
|
item(i).weight=maxweight-total_weight;
|
|
item(i).value=float(item(i).weight)*vpu;
|
|
total_value = total_value + item(i).value;
|
|
total_weight = total_weight + item(i).weight;
|
|
Put Edit(item(i).name, item(i).weight, item(i).value)
|
|
(Skip,a(7),2(f(8,3)));
|
|
Leave Loop;
|
|
end;
|
|
end;
|
|
Put Edit(repeat('-',22))(Skip,a);
|
|
Put Edit('total',total_weight, total_value)(Skip,a(6),f(9,3),f(8,3));
|
|
|
|
init_item: Proc(i,name,weight,value);
|
|
Dcl i Bin Fixed(31);
|
|
Dcl name Char(*);
|
|
Dcl (weight,value) Dec Fixed(15,3);
|
|
item(i).name = name;
|
|
item(i).weight = weight;
|
|
item(i).value = value;
|
|
End;
|
|
End;
|