我正在尝试使用c ++编写计算机调度系统代码。我正在使用基于数组的优先级队列。 这是程序的想法
每个输入事务将表示要计划的新计算机维修订单或“服务”命令。新的计算机维修订单将由连字符分隔的三个部分表示:客户ID(可变长度),计算机型号年份(4位数字),保修代码(y或n)。将根据优先级和收到维修订单的顺序(先到先得)安排每个维修订单。保修维修订单(即保修代码=“ y”)的优先级为1(最高)。优先级低于2的计算机型号的非保修维修被分配了优先级2。优先级高于5的计算机型号的非保修维修订单被分配了优先级3(最低)。每个维修订单都放在优先队列中,并由下一位可用的维修技术人员提供服务。包含“服务”命令的事务会导致从优先级队列中删除维修订单,并在控制台上显示客户ID。包含字符串“文件结束”的事务将指示输入结束。显示在处理“文件结束”事务时队列中剩余的维修订单数量。
这是我使用的输入:
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;
}
答案 0 :(得分: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
它们都具有相同的优先级,并且您没有序列号,因此将按字母顺序对其进行排序。