[英]Priority queue not sorting correctly
我正在嘗試用 C++ 編寫一個計算機調度系統。 我正在使用基於數組的優先級隊列。 這是程序的想法
每個輸入事務將代表一個新的計算機維修訂單或一個“服務”命令。 新的電腦維修訂單將分為三個部分,由連字符分隔:客戶 ID(可變長度)、電腦型號年份(4 位數字)、保修代碼(y 或 n)。 每個維修訂單將根據優先級和收到維修訂單的順序(先到先得)進行安排。 保修維修訂單(即保修代碼 = 'y')的優先級為 1(最高)。 使用時間少於 6 年的計算機型號的非保修維修被指定為優先級 2。使用超過 5 年的計算機型號的非保修維修訂單指定為優先級 3(最低)。 每個維修訂單都放在一個優先隊列中,並由下一個可用的維修技術人員提供服務。 包含“服務”命令的事務會導致從優先隊列中刪除維修訂單,並在控制台上顯示客戶 ID。 包含字符串“end-of-file”的事務將表示輸入結束。 處理“文件尾”事務時,顯示隊列中剩余的維修訂單數。
這是我使用的輸入:
anna-2019-y
james-2012-y
jill-2008-y
當我輸入“service”命令時的輸出是
service
jill-2008-y
service
anna-2019-y
service
james-2012-y
但正確的輸出應該是
service
anna-2019-y
service
james-2012-y
service
jill-2008-y
這是我的代碼:
#include <iostream>
#include <cmath>
using namespace std;
class heapType{
public:
bool empty();
void service();
void pop();
void push(string cus);
int remain();
heapType();
private:
int C1I;
int C2I;
int CI;
int done;
int EOL;
int count;
string heap[10];
int PI;
void swap(int I1, int I2);
};
bool heapType::empty() {
return (count == 0);
}
int heapType::remain() {
return count;
}
void heapType::swap(int I1, int I2) {
string T = heap[I1];
heap[I1] = heap[I2];
heap[I2] = T;
}
void heapType::service() {
if (count == 0) {
}
else {
string name = heap[0];
int pos1 = heap[0].find('-');
int pos2 = heap[0].find('-', pos1 + 1);
cout << "(output: " << heap[0].substr(pos2 + 1) << ")" << endl;
}
}
heapType::heapType() {
EOL = 0;
count = 0;
for (int i = 0; i < 10; i++)
heap[i] = "0";
}
void heapType::push(string cus){
if (count == 10){
cout << "Error: queue is full." << endl;
}
else {
count++;
heap[EOL] = cus;
CI = EOL;
EOL++;
done = 0;
while (!done) {
if (CI == 0)
done = 1;
else {
PI = (CI - 1) / 2;
if (heap[PI] <= heap[CI])
done = 1;
else{
swap(PI, CI);
CI = PI;
}
}
}
}
}
string change(string choice){
string newExpression;
int p1, p2, intYear;
string name, year, priority, warranty;
static int seq = 1;
string sequence;
p1 = choice.find('-');
p2 = choice.find('-', p1 + 1);
name = choice.substr(0, p1);
warranty = choice.substr(p2 + 1);
year = choice.substr(p1 + 1, p2 - p1 - 1);
intYear = atoi(year.c_str());
if (warranty == "y")
priority = "1";
else if (2019 - intYear < 6)
priority = "2";
else
priority = "3";
newExpression = priority + "-" + sequence + "-" + name;
seq++;
return newExpression;
}
void heapType::pop() {
if (count == 0)
cout << "Error: queue is empty.\n";
else {
if (count == 1) {
count = 0;
EOL = 0;
heap[0] = "0";
}
else {
if (count == 2) {
count = 1;
EOL = 1;
heap[0] = heap[1];
heap[1] = "0";
}
else {
count--;
EOL--;
heap[0] = heap[EOL];
heap[EOL] = "0";
done = 0;
PI = 0;
C1I = 1;
C2I = 2;
while (!done){
if (C2I >= EOL){
if (heap[PI] > heap[C1I])
swap(PI, C1I);
done = 1;
}
else {
if((heap[PI] <= heap[C1I]) && (heap[PI] <= heap[C2I]))
done = 1;
else {
if (heap[C1I] < heap[C2I]) {
swap(PI, C1I);
if ((C1I * 2 + 1) >= EOL)
done = 1;
else
PI = C1I;
} else {
swap(PI, C2I);
if ((C2I * 2 + 1) >= EOL)
done = 1;
else
PI = C2I;
}
C1I = PI * 2 + 1;
C2I = PI * 2 + 2;
}
}
}
}
}
}
}
int main(){
heapType iHeap;
string choice, second;
cout << "\nInput transaction(customerid-year-warrantycode)\n";
cout << "Service(service)\n";
cout << "Exit program(end-of-file)\n";
cin >> choice;
while (choice != "end-of-file"){
if (choice == "service"){
iHeap.service();
iHeap.pop();
}
else {
second = change(choice);
iHeap.push(second);
}
cout << "\nInput transaction(customerid-year-warrantycode)\n";
cout << "Service(service)\n";
cout << "Exit program(end-of-file)\n";
cin >> choice;
}
cout << "(output: There are " << iHeap.remain()
<< " remaining repair orders in the queue)";
return 0;
}
問題出在您的change
函數中,該函數計算優先級,然后使用以下行構造一個新的優先級表達式:
newExpression = priority + "-" + sequence + "-" + name;
但是你從來沒有給它賦值,所以它很可能是NULL
。 您的編譯器是否對此發出警告? 這個函數的輸出將是一個格式為priority--name
的字符串。
鑒於您的示例輸入(來自評論),我創建了此表來顯示您將計算的表達式:
jim-2001-y 1--jim
jake-2012-y 1--jake
frank-2011-y 1--frank
james-2019-y 1--james
它們都具有相同的優先級,而且您沒有序列號,因此將按字母順序對它們進行排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.