[英]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.