题目描述:
First of all, you are required to complete a Linked stack in stack.cpp.
Linked stack, as the name implies, is a linked structure by using pointer.
Then, we can use this stack to convert infix expression to postfix expression in the MidToLast class.
What is infix expression and postfix expression?
(1) Infix expression: operator is in the middle of the operand, as what you use in your daily life, such ass, 1+2, 3*5-1, 1+2-3*(2-1) and so on.
(2) Postfix expression: contain no bracket, operator is on the back of two operand.
Operation will performe by the order of appearance of operator, strictly from left to right.
For example, 1+2, will be changed to 12+, 3*5-1 will be 35*1-, 1+2-3*2-1 will be 12+32*-1-.
The method to convert the expression is very ingenious and using stack is a good choice.
In this problem, the number is between 0~9, and the symbols are +-*/().
大概的意思就是要你利用栈来实现中缀表达式转后缀表达式。
思路:一个中缀表达式可能包涵数字,运算符,括号。下面的方法就是分几种不同的情况来处理。
1、读到数字时,不放进栈中,直接放到要输出的string里面。
2、读到读到操作符时“+ - * / ”,如果此时栈顶操作符优先性大于或等于此操作符,弹出栈顶操作符放到string里面。直到发现优先级更低的元素或者发现(。
P.S.操作符中,+-优先级最低,()优先级最高。所以要注意的一点是,除非遇到了),不然不弹出(。
3、如果遇到一个右括号,那么就将栈元素弹出,将符号写入string中,直到遇到一个对应的左括号。但是这个左括号只被弹出,并不写入string。
4、如果读到输入的末尾,将栈元素弹出并写入string中,直到该栈变成空栈。
下面是相应的代码:
stack.h:
#ifndef STACK_H
#define STACK_H
#include
using namespace std;
struct Node {
char entry;
Node *next;
Node() {
next = NULL;
}
Node(char data, Node *add_on = NULL) {
entry = data;
next = add_on;
}
};
class Stack {
public:
Stack();
bool empty() const;
void push(const char item);
void pop();
char top() const;
int size() const;
protected:
Node *top_node;
int count;
};
#endif
stack.cpp:
#include "stack.h"
Stack::Stack() {
top_node = NULL;
count = 0;
}
bool Stack::empty() const {return count == 0;}
void Stack::push(const char item) {
Node* temp = new Node(item);
if (top_node == NULL) {
top_node = temp;
} else {
temp->next = top_node;
top_node = temp;
}
count++;
}
void Stack::pop() {
Node* temp = top_node;
top_node = top_node->next;
delete temp;
count--;
}
char Stack::top() const {return top_node->entry;}
int Stack::size() const {return count;}
MidToLast.h:
#ifndef MIDTOLAST_H
#define MIDTOLAST_H
#include
#include
using namespace std;
class MidToLast {
public:
string transfer(string str);
};
#endif
MidToLast.cpp:
#include "stack.h"
#include
#include "MidToLast.h"
string MidToLast::transfer(string str) {
string temp;
Stack s;
for (int i = 0; i < str.size(); ++i) {
if (str[i] <= '9' && str[i] >= '0') {
temp += str[i];
} else if (str[i] == '(') {
s.push(str[i]);
} else if (str[i] == ')') {
while (!s.empty() && s.top() != '(') {
temp += s.top();
s.pop();
}
s.pop();
} else if (str[i] == '+' || str[i] == '-') {
if (s.empty()) {
s.push(str[i]);
} else {
while (!s.empty() && s.top() != '(') {
temp += s.top();
s.pop();
}
s.push(str[i]);
}
} else if (str[i] == '*' || str[i] == '/') {
if (s.empty()) {
s.push(str[i]);
} else {
while (!s.empty() && (s.top() == '*' || s.top() == '/')) {
temp += s.top();
s.pop();
}
s.push(str[i]);
}
}
}
while (!s.empty()) {
temp += s.top();
s.pop();
}
return temp;
}
测试文件:
#include
#include
#include "stack.h"
#include "MidToLast.h"
using namespace std;
int main() {
string str;
string d;
Stack s;
getline(cin, d);
int len = d.length();
for (int i = len - 1; i > 1; i--) {
s.push(d[i]);
}
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
cout << endl;
cout << s.size() << endl;
getline(cin, str);
MidToLast mtl;
string strtemp = mtl.transfer(str);
cout << strtemp << endl;
return 0;
}
Sample Input:
This line is pointless, please ignore 10
1+2-3*2-1
Sample Output:
i s l i n e i s p o i n t l e s s , p l e a s e i g n o r e 1 0
0
12+32*-1-