简体   繁体   English

优先队列排序不正确

[英]Priority queue not sorting correctly

I am trying to code a computer scheduling system with c++.我正在尝试用 C++ 编写一个计算机调度系统。 I am using an array-based priority queue.我正在使用基于数组的优先级队列。 This is the idea of the program这是程序的想法

Each input transaction will represent a new computer repair order to be scheduled or a “service” command.每个输入事务将代表一个新的计算机维修订单或一个“服务”命令。 A new computer repair order will be represented in three parts separated by hyphens: customer ID (variable length), computer model year (4 digits), warranty code (y or n).新的电脑维修订单将分为三个部分,由连字符分隔:客户 ID(可变长度)、电脑型号年份(4 位数字)、保修代码(y 或 n)。 Each repair order will be scheduled based on priority and the order in which the repair order was received (first-come-first-served).每个维修订单将根据优先级和收到维修订单的顺序(先到先得)进行安排。 A warranty repair order (ie, warranty code = 'y') is assigned priority 1 (highest).保修维修订单(即保修代码 = 'y')的优先级为 1(最高)。 A non-warranty repair for a computer model less than 6 years old is assigned priority 2. A non-warranty repair order for a computer model older than 5 years is assigned a priority 3 (lowest).使用时间少于 6 年的计算机型号的非保修维修被指定为优先级 2。使用超过 5 年的计算机型号的非保修维修订单指定为优先级 3(最低)。 Each repair order is placed in a priority queue and will be serviced by the next available repair technician.每个维修订单都放在一个优先队列中,并由下一个可用的维修技术人员提供服务。 A transaction that contains a “service” command results in a repair order being removed from the priority queue and the customer ID being displayed on the console.包含“服务”命令的事务会导致从优先队列中删除维修订单,并在控制台上显示客户 ID。 A transaction containing the string “end-of-file” will signal the end of the input.包含字符串“end-of-file”的事务将表示输入结束。 Display the number of repair orders remaining in the queue when the “end-of-file” transaction is processed.处理“文件尾”事务时,显示队列中剩余的维修订单数。

Here is the input I used:这是我使用的输入:

anna-2019-y
james-2012-y
jill-2008-y

The output when I enter the "service" command is当我输入“service”命令时的输出是

service
jill-2008-y
service
anna-2019-y
service
james-2012-y

but the correct output should be但正确的输出应该是

service
anna-2019-y
service
james-2012-y
service
jill-2008-y

This is my code:这是我的代码:

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

The problem is in your change function, which computes the priority and then constructs a new priority expression with this line:问题出在您的change函数中,该函数计算优先级,然后使用以下行构造一个新的优先级表达式:

newExpression = priority + "-" + sequence + "-" + name;

But you never assign it a value, so likely it's NULL .但是你从来没有给它赋值,所以它很可能是NULL Is your compiler issuing a warning about this?您的编译器是否对此发出警告? The output from this function will be a string of the form priority--name .这个函数的输出将是一个格式为priority--name的字符串。

Given your sample input (from comments), I've created this table to show the expressions you'll compute:鉴于您的示例输入(来自评论),我创建了此表来显示您将计算的表达式:

jim-2001-y      1--jim
jake-2012-y     1--jake
frank-2011-y    1--frank
james-2019-y    1--james

They're all the same priority and you don't have a sequence number, so they're going to be sorted alphabetically.它们都具有相同的优先级,而且您没有序列号,因此将按字母顺序对它们进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM