我猜它与内存分配/删除有关,但我是C ++类的新手。该程序输入一个后缀表达式(来自文件)并生成一系列单操作指令来评估表达式。
#include <iostream>
#include <stack>
#include <fstream>
using namespace std;
class postfixer
{
public:
postfixer(string line)
{
char token = 0,
Rside,
Lside,
upper = 'A';
stack<char>mystack;
for (int p = 0; p < line.length(); p++)
{
token = line[p];
if (token >= 'a' && token <= 'z')
{
mystack.push(token);
}
else if (token >= '*' && token <= '/')
{
Rside = mystack.top();
mystack.pop();
Lside = mystack.top();
mystack.pop();
cout << Lside << token << Rside << "=" << upper << endl;
mystack.push(upper);
upper++;
}
}
cout << "p = " << mystack.top() << endl;
cout << "----------------------------" << endl << endl;
} // constructor definition
};
int main() {
string line;
ifstream infile("postfix.txt");//open data file
if (infile.fail())
{ // check to see if the program can connect with the input file
cout << "can't open the input file";
//debug: else cout << "input file is open" << endl;
}
while (!infile.eof())
{
getline(infile, line);
cout << line << endl << endl;
new postfixer(line);
}
return 0;
}
postfix.txt
bced/+*
ab*c+de-/
abdef-/*+
gab/ce*f/-h+*
mc*c*
al*aw*+
lw*