我正在做一个利用ADT队列(FIFO)和继承的任务。我设法对所有内容进行编码并修复编译器错误,但它没有按预期执行。
#include "queue.h"
#include <iostream>
#include <string>
using namespace std;
struct spaceship
{
int arrivalTime;
string shipName;
int fuelPurchase;
int waitTime;
};
int main()
{
MyQueue<spaceship> A;
MyQueue<spaceship> B;
MyQueue<spaceship> C;
int numShips;
spaceship* customer;
cin >> numShips;
customer = new spaceship[numShips];
for (int i = 0; i < numShips; i++)
{
cin >> customer[i].arrivalTime;
cin >> customer[i].shipName;
cin >> customer[i].fuelPurchase;
customer[i].waitTime = customer[i].arrivalTime + (3 * customer[i].fuelPurchase);
if ((A.size() <= B.size()) && (A.size() <= C.size()))
{
A.enqueue(customer[i]);
cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port A" << endl;
}
if ((B.size() < A.size()) && (B.size() <= C.size()))
{
B.enqueue(customer[i]);
cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port B" << endl;
}
if ((C.size() < A.size()) && (C.size() < B.size()))
{
C.enqueue(customer[i]);
cout << "At time " << customer[i].arrivalTime << " " << customer[i].shipName << "joins port C" << endl;
}
}
delete[] customer;
}
以下是派生类的实现文件:
#include <iostream>
using namespace std;
template<class T>
const MyQueue<T>& MyQueue<T>::operator = (const MyQueue<T>& rhs)
{
if (this != &rhs)
{
if (head != NULL)
clear();
head = NULL;
Node<T>* rhsPtr = rhs.head;
Node<T>* copyPtr = head = new Node<T>;
copyPtr->data = rhsPtr->data;
while (rhsPtr->next != NULL)
{
rhsPtr = rhsPtr->next;
copyPtr->next = new Node<T>;
copyPtr = copyPtr->next;
copyPtr->data = rhsPtr->data;
}
copyPtr->next = NULL;
}
return(*this);
}
template<class T>
MyQueue<T>::MyQueue(const MyQueue<T>& rhs)
{
head = tail = NULL;
*this = rhs;
}
template<class T>
bool MyQueue<T>::isEmpty() const
{
return(head == NULL);
}
template<class T>
const T& MyQueue<T>::front() const throw(Oops)
{
if (head != NULL)
return(head->data);
else
throw Oops("Error");
}
template<class T>
const T& MyQueue<T>::back() const throw(Oops)
{
if (tail != NULL)
return(tail->data);
else
throw Oops("Error");
}
template<class T>
void MyQueue<T>::enqueue(const T& x)
{
Node<T>* newNode = new Node<T>;
newNode->data = x;
newNode->next = NULL;
if (isEmpty())
{
tail = newNode;
head = tail;
}
else
{
tail->next = newNode;
tail = tail->next;
}
}
template<class T>
void MyQueue<T>::dequeue()
{
Node<T>* temp = head;
if (isEmpty())
return;
if (head == tail)
head = tail = NULL;
else
{
head = head->next;
delete temp;
}
}
template<class T>
void MyQueue<T>::clear()
{
Node<T>* del;
del = head;
while (head != NULL)
{
head = head->next;
delete del;
del = head;
}
}
template<class T>
int MyQueue<T>::size() const
{
Node<T>* temp;
temp = head;
int count = 0;
while (temp != NULL)
{
++count;
temp = temp->next;
}
return(count);
}
一切都还没有执行。我只是想确保我先得到一份工作程序。
无论如何,当我输入船的详细信息(到达时间,名称,购买的燃料)时,我希望它按照字母顺序排到最短的一条线(决胜局)并输出一份声明,宣布飞船到达哪个站。相反,宇宙飞船在所有3个同时停靠,输出队列大小表示该对象确实在所有三个队列中排队。
假设只有1艘船,3个站是空的。
逻辑上它应该只去A正确吗?因为在执行第一个if语句之后,其他两个if分支不再为true而且不应该执行,但这不是正在发生的事情。
注意:我没有包含基本纯虚拟类和派生类头,因为我认为没有必要。 任何见解都表示赞赏。多谢你们。