【发布时间】:2012-09-22 22:34:06
【问题描述】:
我的讲师给了我一个任务,让我创建一个程序来使用 Stacks 将表达式转换和中缀为后缀。我已经制作了堆栈类和一些函数来读取中缀表达式。
但是这个名为convertToPostfix(char * const inFix, char * const postFix) 的函数负责使用堆栈将数组inFix 中的inFix 表达式转换为数组postFix 中的后修复表达式,但它并没有做它应该做的事情。你们能帮帮我,告诉我我做错了什么吗?
以下是从 inFix 转换为 postFix 的函数的代码,convertToPostfix(char * const inFix, char * const postFix) 是我需要帮助修复的代码:
void ArithmeticExpression::inputAndConvertToPostfix()
{
char inputChar; //declaring inputChar
int i = 0; //inizalize i to 0
cout << "Enter the Arithmetic Expression(No Spaces): ";
while( ( inputChar = static_cast<char>( cin.get() ) ) != '\n' )
{
if (i >= MAXSIZE) break; //exits program if i is greater than or equal to 100
if(isdigit(inputChar) || isOperator(inputChar))
{
inFix[i] = inputChar; //copies each char to inFix array
cout << inFix[i] << endl;
}
else
cout << "You entered an invalid Arithmetic Expression\n\n" ;
}
// increment i;
i++;
convertToPostfix(inFix, postFix);
}
bool ArithmeticExpression::isOperator(char currentChar)
{
if(currentChar == '+')
return true;
else if(currentChar == '-')
return true;
else if(currentChar == '*')
return true;
else if(currentChar == '/')
return true;
else if(currentChar == '^')
return true;
else if(currentChar == '%')
return true;
else
return false;
}
bool ArithmeticExpression::precedence(char operator1, char operator2)
{
if ( operator1 == '^' )
return true;
else if ( operator2 == '^' )
return false;
else if ( operator1 == '*' || operator1 == '/' )
return true;
else if ( operator1 == '+' || operator1 == '-' )
if ( operator2 == '*' || operator2 == '/' )
return false;
else
return true;
return false;
}
void ArithmeticExpression::convertToPostfix(char * const inFix, char * const postFix)
{
Stack2<char> stack;
const char lp = '(';
stack.push(lp); //Push a left parenthesis ‘(‘ onto the stack.
strcat(inFix,")");//Appends a right parenthesis ‘)’ to the end of infix.
// int i = 0;
int j = 0;
if(!stack.isEmpty())
{
for(int i = 0;i < 100;){
if(isdigit(inFix[i]))
{
postFix[j] = inFix[i];
cout << "This is Post Fix for the first If: " << postFix[j] << endl;
i++;
j++;
}
if(inFix[i] == '(')
{
stack.push(inFix[i]);
cout << "The InFix was a (" << endl;
i++;
//j++;
}
if(isOperator(inFix[i]))
{
char operator1 = inFix[i];
cout << "CUrrent inFix is a operator" << endl;
if(isOperator(stack.getTopPtr()->getData()))
{
cout << "The stack top ptr is a operator1" << endl;
char operator2 = stack.getTopPtr()->getData();
if(precedence(operator1,operator2))
{
//if(isOperator(stack.getTopPtr()->getData())){
cout << "The stack top ptr is a operato2" << endl;
postFix[j] = stack.pop();
cout << "this is post fix " << postFix[j] << endl;
i++;
j++;
// }
}
}
else
stack.push(inFix[i]);
// cout << "Top Ptr is a: "<< stack.getTopPtr()->getData() << endl;
}
for(int r = 0;r != '\0';r++)
cout << postFix[r] << " ";
if(inFix[i] == ')')
{
while(stack.stackTop()!= '(')
{
postFix[j] = stack.pop();
i++;
j++;
}
stack.pop();
}
}
}
}
请注意,函数 convertToPostfix 是使用此算法生成的:
- 将左括号“(”压入堆栈。
- 在中缀末尾添加右括号“)”。
-
当堆栈不为空时,从左到右读取中缀并执行以下操作:
- 如果中缀中的当前字符是数字,则将其复制到后缀的下一个元素。
- 如果中缀中的当前字符是左括号,则将其压入堆栈。
-
如果中缀中的当前字符是运算符,
- 在栈顶弹出操作符(如果有的话),同时它们的优先级等于或高于当前操作符,并将弹出的操作符插入到后缀中。
- 将中缀中的当前字符推入堆栈。
- 如果中缀中的当前字符是右括号
- 从堆栈顶部弹出运算符并将它们插入到后缀中,直到左括号位于堆栈顶部。
- 从堆栈中弹出(并丢弃)左括号。
【问题讨论】:
-
除非您感到自虐,否则谷歌搜索“递归下降”或(尤其是)“调车场算法”可能会很有帮助(如果您已经尝试使用其中之一,我很抱歉-- 至少乍一看我也不认为它是在实现)。
-
我遵循了我刚刚在我的讲师给出的帖子中编辑的算法,但我似乎没有让它发挥作用。
-
你应该与this person合作。他和你同时问了同样的问题。
-
我们同班不同组。
-
那么,在调试器中单步执行这段代码,它告诉你什么?不要告诉我你不知道调试器是什么或如何使用它,或者你没有尝试使用它或尝试过但无奈失败。来吧。你不能只用你的大脑来做,使用这个工具。
标签: c++ stack infix-notation postfix-notation