version 1.12, 2005/02/16 10:27:53
|
version 1.13, 2005/02/16 10:44:24
|
|
|
#include<string> | #include<string> |
#include <queue> | #include <queue> |
#include <stack> | #include <stack> |
|
#include <vector> |
#include <stdio.h> | #include <stdio.h> |
// yes I could use just one i/o lib | // yes I could use just one i/o lib |
using namespace std; | using namespace std; |
|
|
else { printf("CANNOT FILL PALLET\n"); } | else { printf("CANNOT FILL PALLET\n"); } |
} | } |
| |
/* |
|
// have to deal with non int #s | // have to deal with non int #s |
| |
int intcomp (const void * a, const void * b) |
void super_increase_algo ( vector<ITEM_MASS> inventory, ITEM_MASS target_value , INDEX_TYPE size_inventory, int DEBUG_MODE ) { |
{ |
stack<ITEM_MASS> packages_to_load; |
return ( *(int*)a - *(int*)b ); |
|
}*/ |
if (DEBUG_MODE) { |
|
printf("Extracting values, highest to lowest, doing reduction from index %d\n",size_inventory); |
|
} |
|
for ( INDEX_TYPE store_index=size_inventory-1; store_index>=0;store_index-- ) { |
|
if (DEBUG_MODE) { |
|
printf("value %d, is it smaller than %d? If so, subtract and store.\n",inventory[store_index],target_value); |
|
} |
|
if ( inventory[store_index] <= target_value ) { |
|
target_value = target_value - inventory[store_index]; |
|
packages_to_load.push(inventory[store_index]); |
|
} |
|
} |
|
if ( target_value == 0 ) { |
|
int first = 1; |
|
while ( ! packages_to_load.empty() ) { |
|
if (first==0) { printf(","); } |
|
else {first = 0;} // there has GOT to be a better way! |
|
printf("%d",packages_to_load.top()); |
|
packages_to_load.pop(); |
|
} |
|
printf("\n"); |
|
} |
|
else { printf("CANNOT FILL PALLET\n"); } |
|
} |
|
|
| |
void syntaxmsg(){ | void syntaxmsg(){ |
printf("Usage:\n"); | printf("Usage:\n"); |
|
|
length = inputfs.tellg(); | length = inputfs.tellg(); |
inputfs.seekg (0, ios::beg); | inputfs.seekg (0, ios::beg); |
| |
max_inventory= 1 + length/2; // Elements cannot be more than this amount |
|
|
|
char * read_buffer = (char *) malloc (length); | char * read_buffer = (char *) malloc (length); |
ITEM_MASS * inventory = (ITEM_MASS *) malloc (length); |
vector<ITEM_MASS> inventory; |
// could use stack instead of array --- doesn't require malloc, more 'safe' avoids overflows, esp if | // could use stack instead of array --- doesn't require malloc, more 'safe' avoids overflows, esp if |
// input file is funky --- its better software engineering, but its slower. Given that this is a pretty | // input file is funky --- its better software engineering, but its slower. Given that this is a pretty |
// small system, I'll stick with the array |
// small system, I'll stick with the array --- oh even better -- a vector! |
inputfs.getline (read_buffer, length-1); | inputfs.getline (read_buffer, length-1); |
| |
char * theToken; INDEX_TYPE index = 0; | char * theToken; INDEX_TYPE index = 0; |
|
|
// assume 1+ items | // assume 1+ items |
while ( theToken != NULL ) { | while ( theToken != NULL ) { |
//printf("tokendebug %d %f\n",theToken,theToken); | //printf("tokendebug %d %f\n",theToken,theToken); |
inventory[index] = atol(theToken); |
inventory.push_back(atol(theToken)); |
//printf("index %d, token %s, value %d\n",index,theToken,inventory[index]); | //printf("index %d, token %s, value %d\n",index,theToken,inventory[index]); |
index++; | index++; |
theToken = strtok (NULL,","); | theToken = strtok (NULL,","); |
} | } |
| |
INDEX_TYPE size_inventory = index; //remember this is 1 based, not 0 |
INDEX_TYPE size_inventory = inventory.size(); //remember this is 1 based, not 0 |
| |
int SUPER_MODE = 0; | int SUPER_MODE = 0; |
if (inventory[0]==1) { SUPER_MODE=0;} |
if (inventory[0]==1) { SUPER_MODE=1;} |
| |
if (DEBUG_MODE) { | if (DEBUG_MODE) { |
printf("Line 1 read - %d products from warehouse\n", size_inventory); | printf("Line 1 read - %d products from warehouse\n", size_inventory); |
|
|
printf("Line 2 read - %d weight units can fit onto palette\n",palette_size); | printf("Line 2 read - %d weight units can fit onto palette\n",palette_size); |
} | } |
| |
|
if (SUPER_MODE) { |
|
super_increase_algo (inventory, palette_size , size_inventory, DEBUG_MODE ); |
|
return 0; |
|
} |
|
|
inputfs.close(); | inputfs.close(); |
free(read_buffer); | free(read_buffer); |
| |
if |
|
|
|
|
|
// ckeanup, release memeory, close files here |
|
|
|
|
|
// I could use a class to extrapolate out the file loading function like my 499 project.... | // I could use a class to extrapolate out the file loading function like my 499 project.... |
| |
backpack knapsackOne; | backpack knapsackOne; |
|
|
| |
} | } |
knapsackOne.store_item_array(); | knapsackOne.store_item_array(); |
free(inventory); |
inventory.clear(); |
knapsackOne.branch_and_bound(DEBUG_MODE,palette_size); | knapsackOne.branch_and_bound(DEBUG_MODE,palette_size); |
| |
printf("\n"); | printf("\n"); |
|
|
| |
| |
| |
void super ( ITEM_MASS target_value , INDEX_TYPE size_inventory, int DEBUG_MODE ) { |
|
// input, a super increasing set, lowest to highest. |
|
// the number of elements in the list [1-ordinal] |
|
// a target value. |
|
//debugmode |
|
|
|
// need to output values that sum to target |
|
// need another place to store. --- not another array, need LIFO structure. A stack of course. |
|
// we have to count downwards during super increasing, or we could qsort the list. (not going to do that) |
|
|
|
stack<ITEM_MASS> packages_to_load; |
|
|
|
if (DEBUG_MODE) { |
|
printf("Extracting values, highest to lowest, doing reduction\n"); |
|
} |
|
for ( INDEX_TYPE store_index=size_inventory-1; size_inventory<=0;store_index-- ) { |
|
if (DEBUG_MODE) { |
|
printf("value %d, is it smaller than %d? If so, subtract and store.\n",inventory[store_index],target_value); |
|
} |
|
if ( inventory[store_index] <= target_value ) { |
|
target_value = target_value - inventory[store_index]; |
|
packages_to_load.push(inventory[store_index]); |
|
} |
|
} |
|
if ( target_value == 0 ) { |
|
int first = 1; |
|
while ( ! packages_to_load.empty() ) { |
|
if (first==0) { printf(","); } |
|
else {first = 0;} // there has GOT to be a better way! |
|
printf("%d",packages_to_load.top()); |
|
packages_to_load.pop(); |
|
} |
|
else { printf("CANNOT FILL PALLET\n"); } |
|
|
|
|
|
} |
|
| |
| |
| |