1368 表达式转换
2011-07-25 10:51
225 查看
描述 把中缀表达式转化为后缀表达式……
书上有详细讲解.
输入 第一行为T,表示有T组数据;
以下T行每行包含一个字符串,是中缀表达式。
输出 对于每组测试数据数据后缀表达式。
样例输入
经典问题,用栈实现#include <iostream>
#include <string>
#include <stack>
using namespace std;
void Convert(const string & Infix, string & Postfix);
bool IsOperand(char ch);
bool TakesPrecedence(char OperatorA, char OperatorB);
int main(void)
{
char Reply;
int number;
scanf("%d",&number);
while(number--)
{
string Infix, Postfix; // local to this loop
cin >> Infix;
Convert(Infix, Postfix);
cout << Postfix << endl;
}
return 0;
}
/* Given: ch A character.
Task: To determine whether ch represents an operand (here understood
to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false otherwise.
*/
bool IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}
/* Given: OperatorA A character representing an operator or parenthesis.
OperatorB A character representing an operator or parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.
Return: In the function name: true, if OperatorA takes precedence over
OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;
}
/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/
void Convert(const string & Infix, string & Postfix)
{
stack<char> OperatorStack;
char TopSymbol, Symbol;
int k;
for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(OperatorStack.top(), Symbol)))
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}
while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}
书上有详细讲解.
输入 第一行为T,表示有T组数据;
以下T行每行包含一个字符串,是中缀表达式。
输出 对于每组测试数据数据后缀表达式。
样例输入
1
A+(B-C/D)*E样例输出
ABCD/-E*+
经典问题,用栈实现#include <iostream>
#include <string>
#include <stack>
using namespace std;
void Convert(const string & Infix, string & Postfix);
bool IsOperand(char ch);
bool TakesPrecedence(char OperatorA, char OperatorB);
int main(void)
{
char Reply;
int number;
scanf("%d",&number);
while(number--)
{
string Infix, Postfix; // local to this loop
cin >> Infix;
Convert(Infix, Postfix);
cout << Postfix << endl;
}
return 0;
}
/* Given: ch A character.
Task: To determine whether ch represents an operand (here understood
to be a single letter or digit).
Return: In the function name: true, if ch is an operand, false otherwise.
*/
bool IsOperand(char ch)
{
if (((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')))
return true;
else
return false;
}
/* Given: OperatorA A character representing an operator or parenthesis.
OperatorB A character representing an operator or parenthesis.
Task: To determine whether OperatorA takes precedence over OperatorB.
Return: In the function name: true, if OperatorA takes precedence over
OperatorB.
*/
bool TakesPrecedence(char OperatorA, char OperatorB)
{
if (OperatorA == '(')
return false;
else if (OperatorB == '(')
return false;
else if (OperatorB == ')')
return true;
else if ((OperatorA == '^') && (OperatorB == '^'))
return false;
else if (OperatorA == '^')
return true;
else if (OperatorB == '^')
return false;
else if ((OperatorA == '*') || (OperatorA == '/'))
return true;
else if ((OperatorB == '*') || (OperatorB == '/'))
return false;
else
return true;
}
/* Given: Infix A string representing an infix expression (no spaces).
Task: To find the postfix equivalent of this expression.
Return: Postfix A string holding this postfix equivalent.
*/
void Convert(const string & Infix, string & Postfix)
{
stack<char> OperatorStack;
char TopSymbol, Symbol;
int k;
for (k = 0; k < Infix.size(); k++)
{
Symbol = Infix[k];
if (IsOperand(Symbol))
Postfix = Postfix + Symbol;
else
{
while ((! OperatorStack.empty()) &&
(TakesPrecedence(OperatorStack.top(), Symbol)))
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
if ((! OperatorStack.empty()) && (Symbol == ')'))
OperatorStack.pop(); // discard matching (
else
OperatorStack.push(Symbol);
}
}
while (! OperatorStack.empty())
{
TopSymbol = OperatorStack.top();
OperatorStack.pop();
Postfix = Postfix + TopSymbol;
}
}
相关文章推荐
- 使用正则表达式将Html转换为纯文本
- 栈和队列6|中缀表达式转换为后缀表达式 - 数据结构和算法28
- PTA 3.11表达式转换
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- 练习2-10 重新编写将大写字母转换为小写字母的函数lower,并用条件表达式替代其中的if-else结构。
- SDUT2132算术表达式转换成后缀式(表达式树)
- 无法确定表达式的类型,因为<null>和int之间没有隐式转换
- 3-06. 表达式转换
- 中缀表达式转换成后缀表达式的实现(VB)
- PHP一些常用的正则表达式字符的一些转换
- Javascript-数据类型转换 、 运算符和表达式
- 中缀表达式转换成后缀表达式
- 中缀表达式转换为前缀表达式
- 中缀表达式转换成后缀表达式
- 中缀表达式转换成前缀表达式和后缀表达式
- 将表达式转换成逆波兰式
- 浅析JS中常用类型转换及运算符表达式
- 中缀表达式转换成后缀表达式以及逆波兰表示法求值
- PHP数据类型转换和运算符表达式
- 中缀表达式到后缀表达式的转换