【发布时间】:2020-08-13 20:20:11
【问题描述】:
我正在处理一个项目,但无法克服几个错误。这是我的代码。
main()
#include <iostream>
#include <cstddef>
#include "src\DynStack.cpp"
#include "include\DynStack.h"
#include "src\DynQue.cpp"
#include "DynQue.h"
#include <fstream>
using namespace std;
int main ()
{
ifstream inputFile;
ofstream outputFile;
DynStack<char>cstack;
inputFile.open("input.txt");
if (!inputFile)
{
cout << "Error. No input file found." << endl;
}
else
{
char x;
while (!inputFile.eof())
{
inputFile.get(x);
cstack.push(x);
cout << x;
}
inputFile.close();
outputFile.open("output_reverse.txt");
char y;
int count = 1;
while (cstack.isEmpty() == NULL)
{
cstack.pop(y);
cout << y;
count++;
}
outputFile.close();
}
cout << "Program completed." << endl;
return 0;
}
DynStack.cpp
#include "DynStack.h"
#include <cstddef>
#include <iostream>
using namespace std;
template<typename T>
DynStack<T>::DynStack()
{
top = NULL;
}
template<typename T>
void DynStack<T>::push(T val)
{
StackNode *nodePtr; // ptr to traverse thru the stack
StackNode *previousNode; // ptr to connect higher node to node below it
StackNode *newNode;
newNode = new StackNode; // makes a new StackNode
newNode->value = val;
newNode->next = NULL;
if (top == NULL) // If the stack is empty
{
top = newNode; // Make newNode the first node;
}
else
{
nodePtr = top; // make our ptr = top
newNode->next = nodePtr; // make the our new top node, newNode, point to the node below it
top = newNode; // newNode is our new top of the stack
delete nodePtr;
}
}
template <typename T>
void DynStack<T>::pop(T& val)
{
StackNode *nodePtr; // makes a nodePtr to traverse the stack
nodePtr = top; // set nodePtr to point to top of stack
if (top == NULL) // If stack is empty
{
cout << "Error. Stack is empty." << endl; // Provide error message.
return;
}
else if (top->next == NULL) // If there is only one item in stack
{
val = top->value; // Make return value whatever the previous top was
delete top; // Delete top
top = NULL; // make top equal to null
}
else // If there is more than one item in stack
{
val = top->value; // Return value whatever previous top was
nodePtr = nodePtr->next; // nodePtr point to node below top node
delete top; // delete top node
top = nodePtr; // make nodePtr our new top
delete nodePtr; // delete the nodePtr
}
}
template <typename T>
bool DynStack<T>::isEmpty()
{
if (top == NULL) // If top node is null return true
{
return true;
}
if (top != NULL) // If top node is not null return false
{
return false;
}
}
template <typename T>
DynStack<T>::~DynStack()
{
//dtor
}
DynStack.h
#ifndef DYNSTACK_H
#define DYNSTACK_H
template <typename T>
class DynStack
{
private:
struct StackNode
{
T value;
StackNode *next;
};
public:
DynStack();
~DynStack();
void push(T);
void pop(T&);
bool isEmpty();
StackNode *top;
};
#endif // DYNSTACK_H
这个程序的想法是我们输入一个输入文件“input.txt”说“这是原始文件”。并将其反向输出到另一个文本文件。所以“.elif lanigiro eht si sihT”。
我的问题是:
首先,当我运行这段代码时,它会打印出 while(!inputFile.eof()) 就好了。当它运行第二个 while 循环时,while(cstack.isEmpty() == NULL),它会无限打印。我认为我的 pop() 或 isEmpty() 函数一定有问题,但我只是看不出我缺少什么。也许我在 pop() 中将 top 设置为 NULL 错误,但我看不出要更改什么。可能 isEmpty() 有问题,但是看起来很简单我不知道要添加什么。
更重要的是,当我尝试运行它时,第二个 while 循环的输出似乎每次都不同。有时它是“.e.e.e.”重复。有时它的“.el.el.el”重复。有时它是“.elif.elif.elif”重复。 “.elif.elif.elif”重复是我得到的最接近正确输出的结果,所以输出或保存空格可能有问题??
我不知道。如果有人能看出问题所在并向我解释,我将非常感激你。谢谢大家。
【问题讨论】:
-
isEmpty 是 bool 为什么要和 null 比较?
-
这段代码有很多问题。打开编译器警告,然后退出调试器。
标签: c++ function class linked-list stack