使用堆栈C++后缀表达式
C++ Postfix Expressions Using Stacks
我正在开发一个程序,用于计算我的一门计算机科学课程的后缀表达式的结果。该程序使用堆栈 ADT 来实现此目的。
我已经编写了程序,但认为可能存在错误,因为某些表达式的结果不正确。我不确定我的错误在哪里。
此外,当输入文件为空时,程序将生成值 32767。这是从哪里来的?
表达式:3 4 + 3 *值 = 21。
表达式:5 4 3 2 1 - +/*值 = 0。(应为 5)
表达式:9 5 2 4 + - 2 * *值 = 18。(应为 -18)
头文件:
#ifndef STACK_H
#define STACK_H
#include <cstdlib>
#include <iostream>
class Stack
{
public:
typedef int Item;
static const int CAPACITY = 50;
// Constructor
Stack() { used = 0; }
// Modification Member Functions
void push(Item entry) {
data[used] = entry;
++used;
}
Item pop() {
if(!empty())
{
--used;
return data[used];
}
}
// Constant Member Functions
bool empty() const { return used == 0; }
int size() const { return used; }
private:
// Data Members
Item data[CAPACITY];
int used;
};
#endif
主程序:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "stack.h"
using namespace std;
int main()
{
Stack s;
string input;
ifstream infile;
int final, operand1, operand2, result;
char infile_name[80];
cout << "Enter input file name: ";
cin >> infile_name;
cout << endl;
infile.open(infile_name);
while(!infile)
{
cout << "Could not open file." << endl;
return 0;
}
while (!infile.eof())
{
getline(infile, input);
cout << "Expression: ";
for (int i = 0; i < input.length(); i++)
{
cout << input[i];
if(input[i] == '1' || input[i] == '2' || input[i] == '3' || input[i] == '4' || input[i] == '5' || input[i] == '6' || input[i] == '7' || input[i] == '8' || input[i] == '9')
s.push(input[i] - '0');
if(input[i] == '+')
s.push(s.pop() + s.pop());
if(input[i] == '-')
s.push(s.pop() - s.pop());
if(input[i] == '*')
s.push(s.pop() * s.pop());
if(input[i] == '/')
s.push(s.pop() / s.pop());
}
final = s.pop();
cout << endl << "Value = " << final << "." << endl << endl;
}
}
一个问题是,在语句s.pop() - s.pop()
(以及类似的除法)中,不能保证哪个s.pop()
首先被调用。 因此,从堆栈中删除内容的顺序并不一致。 您应该将其作为两个单独的调用来执行。
auto oper1 = s.pop();
auto oper2 = s.pop();
s.push(oper1-oper2);
在您的情况下,您的错误结果是由于这两个操作以错误的顺序执行。
像while (!infile.eof())
这样的代码几乎总是一个错误。它通常会读取最后一项输入两次(尽管即使这样也不可靠)。
首先更改:
while (!infile.eof())
{
getline(infile, input);
自:
while (getline(infile, input))
看看它是否效果更好。
另请注意,如果堆栈为空,您的stack.pop()
不会返回值:
Item pop() {
if (!empty())
{
--used;
return data[used];
}
}
例如,如果您有额外的运算符,您将尝试在实际未返回值时使用值,从而导致未定义的行为。
相关文章:
- 如何使用 ASCII 转换使用字符堆栈计算后缀表达式
- 多位数后缀表达式
- 检查字符串是否包含有效的后缀表达式
- 在C++中从后缀表达式字符串打印单个操作
- C++后缀表达式未定义与未指定行为
- 如何从后缀表达式生成表达式树
- 中缀到后缀表达式转换代码不起作用
- 我想看到一个在整个后缀表达式的上下文中查找转换类型 id 的示例
- 使用堆栈评估后缀表达式时出现分段错误
- 如何正确测试我的后缀表达式是否有效
- 从 txt 文件中将后缀表达式作为字符串读取
- 在C++中使用树计算后缀表达式
- 后缀表达式中的等号运算符
- 计算后缀表达式.pop() 返回不正确的值来计算函数
- 使用堆栈C++后缀表达式
- 计算具有两个以上操作数的后缀表达式时出现问题
- 在c++中计算后缀表达式时接收堆栈错误
- 后缀表达式求值
- 后缀表达式求值麻烦(PEET)
- 与后缀表达式和操作符的语法混淆