-3

プライオリティ キューとレギュラー キューを含むプロジェクトがあります。プライオリティ キューを使用して最小から最大までの ID で製品を整理する必要があります。

次に、通常のキューを使用して、それらを 3 つのカテゴリに分類する必要があります。(在庫過剰、在庫不足、制限内)。したがって、出力は次のようになります。

在庫不足

14887 $10 14

15678 $1 298

過剰在庫

12565 $4 539

18967 $12 401

StockWithinLimits

19847 $2 220

私はこのコードを書きましたが、よくわからない何かがずれており、出力は次のようになります。

過剰在庫

12565 $4 539

在庫不足

14887 $10 14

在庫不足

15678 $1 298

過剰在庫

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; 
} 
4

2 に答える 2

0

プライオリティ キューの奇妙な使い方ですが、それが要件だと思います。

(1) すべてを pqueue に入れます。それは良い。次に、pqueue を空にし、under/over/within ロジックを使用して、アイテムを 3 つの通常のキューのいずれかに配置する必要があります。次に、各キューを読み取り (ヘッダー「over」、「under」、「within」を最初に出力)、それらを出力します。

(2)

QueueADT <int> que;

que.Enqueue (n);

間違っているように見えます。アイテムではなく int をキューに入れているのはなぜですか? キューが 3 つではなく 1 つあるのはなぜですか? これは que.Deque(n) ステートメントにも当てはまります。

(3) 複合 IF ステートメントは実際には必要ありません。おそらく、何かが最小値を下回っている場合、それは最大値も下回っています。

だから何か

 if belowMin
     put in lowQ
     readagain

 if aboveMax
     put in highQ
     readagain

 put in withinQ

また、仕様に応じて、 item <= max ではなく item < max が必要になるように注意してください。

編集

これらは、現在の while ループの外にある必要があります。

outFile << "UnderStock" << endl;
while (!lowQ.IsEmpty())
{
    lowQ.Dequeue (item);
    item.printProduct (outFile);
}

outFile << "WithINLimits:" << endl;
while (!withinQ.IsEmpty())
{
    withinQ.Dequeue (item);
    item.printProduct (outFile);
}

outFile << "Overstock:" << endl;
while (!highQ.IsEmpty())
{
    highQ.Dequeue (item);
    item.printProduct (outFile);
}
于 2012-05-06T03:33:29.007 に答える