I have a project with priority queues and reqular queues.
I need to organize products by IDs from min to max using priority queues.
And then using reqular queues I need to put them into 3 categories:
(over-stock, under-stock, within limits); so the output should look like this:
UnderStock
14887 $10 14
15678 $1 298
OverStock
12565 $4 539
18967 $12 401
StockWithinLimits
19847 $2 220
I wrote this code, but something is off that I am not sure and my out put looks like:
OverStock
12565 $4 539
UnderStock
14887 $10 14
UnderStock
15678 $1 298
OverStock
18967 $12 401
StockWithInLimits
19847 $2 220
int main()
{
ifstream inFile; // file containing operations
ofstream outFile; // file containing output
string inFileName = "product.txt";
string outFileName = "result.txt";
inFile.open (inFileName.c_str());
outFile.open (outFileName.c_str());
ItemType item;//declare a temp item that trows into pQue
PQType<ItemType> pqueue(50);//priority queue that sorts items by ID
QueueADT <ItemType> que;
QueueADT <ItemType> lowQ;
QueueADT <ItemType> highQ;
QueueADT <ItemType> withinQ;
while ( item.readProduct (inFile) )
{
pqueue.Enqueue(item);
}
while (!pqueue.IsEmpty())
{
pqueue.Dequeue (item);
int tempcurinvent = item.getcurrentInventory ();
int tempmax = item.getMax ();
int tempmin =item.getMin ();
if ((tempcurinvent < tempmin) && (tempcurinvent < tempmax))//UnderStock
{
lowQ.Enqueue (item);
}
if ((tempcurinvent < tempmax) && ( tempcurinvent > tempmin)) //WithINLimits
{
withinQ.Enqueue (item);
}
else if ((tempcurinvent > tempmin) && (tempcurinvent > tempmax))//OverStock
{
highQ.Enqueue (item);
}
outFile << "UnderStock" << endl;
item.printProduct (outFile);
lowQ.Dequeue (item);
outFile << "WithINLimits:" << endl;
item.printProduct (outFile);
withinQ.Dequeue (item);
outFile << "OverStock" << endl;
item.printProduct (outFile);
highQ.Dequeue (item);
}
inFile.close ();
outFile.close ();
return 0;
}
An odd use for a priority queue but I suppose that is the requirements.
(1) You put everything in the pqueue. That’s good. You should then empty the pqueue and using the under/over/within logic put the items into one of the three regular queues. THEN read each of queues (printing the header “over”, “under”, “within” first) and print them out.
(2)
looks wrong. Why are you queing an int and not the item? Why do you have one queue and not three? This applies to que.Deque(n) statements as well.
(3) You don’t really need compound IF statements. Presumably if something is below the minimum it is also below the maximum.
So something like
Also be careful to make sure you want item < max rather than item <= max, depending on what the specs say.
EDIT
These neeed to be outside the current while loop: