对于我的数据结构课程,我必须创建一个从 .dat 文件中获取输入的队列,并根据高优先级(仅当它为 1 时)和低优先级(2 3 4 或 5)来组织它。必须有两个队列,* 表示要服务(或删除)多少。.dat 文件如下所示:
R 3
T 5
W 1
A 4
* 3
M 5
B 1
E 1
F 2
C 4
H 2
J 1
* 4
* 1
D 3
L 1
G 5
* 9
=
这是main.cpp
int main ()
{
arrayQueue myHigh; //creates object of arrayQueue
arrayQueue myLow; //creates another object of arrayQueue
while(previousLine != "=") //gets all the lines of file, ends program when it gets the line "="
{
getline(datfile, StringToChar);
if (StringToChar != previousLine)
{
previousLine=StringToChar; //sets previousline equal to a string
number = StringToChar[2]; //the number of the data is the third line in the string
istringstream ( number ) >> number1; //converts the string to int
character = StringToChar[0]; //the character is the first line in the string
}
if (number1 == 1) //if number is 1, sends to high priority queue
myHigh.addToQueue(number1);
else if (number1 == 2 || number1 == 3 || number1 == 4 || number1 == 5) //if number is 2 3 4 or 5 sends to low priority queue
myLow.addToQueue(number1);
}
datfile.close();
system ("pause");
}
这是数组类:
void arrayQueue::addToQueue(int x)
{
if (full() == true)
cout << "Error, queue full \n";
else {
fill = (fill+1)%maxSize;
queueArray[fill] = x;
cout << x << endl; //testing that number is actually being passed through
count++;
size++;
}
}
但是,我得到的输出只是:
3
5
然后它崩溃而没有错误。
我不确定我应该去哪里,我之前没有创建一个类的两个对象或使用一个文件在 C++ 中读取数据。我做对了吗?我认为它只是将 3 和 5 送入高优先级队列,即使它不应该这样做。