【发布时间】:2013-09-18 23:26:40
【问题描述】:
对于我的数据结构课程,我必须创建一个从 .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 送入高优先级队列,即使它不应该这样做。
【问题讨论】:
-
number1 == 2 || 3 || 4 || 5不会按照你的想法去做。您必须比较每个数字(number1 == 2 || number1 == 3 .... -
谢谢你,我也错过了。它仍然在同一个地方崩溃,不知道会是什么!
-
它要求相当多的 SO 参与者完成你所有的代码(有很多)。为了让我们更轻松,您需要确保我们可以复制和粘贴并编译它。这是不可能的,因为例如,您遗漏了
arrayQueue声明。理想情况下,将所有内容复制并粘贴到一个文件中(显然首先是 h 文件),并添加一些额外的 cmets 来说明哪个文件是哪个文件。然后,任何想要提供帮助的人都可以简单地将您的帖子复制并粘贴到他们选择的 IDE 中。 更好的是让您隔离一小部分失败的代码。 -
另外,将您的代码缩小到您认为问题所在的 sn-ps。您发布的代码太多,难以阅读和理解。
-
听起来你还没有尝试过使用调试器。您可以在 gdb 中运行并查看堆栈(
bt命令),这可能会对您有所帮助。