我的讲师给了我一个任务,让我创建一个程序来使用 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 是使用此算法生成的:
- 将左括号 '(' 压入堆栈。
- 在中缀末尾附加一个右括号')'。
当堆栈不为空时,从左到右读取中缀并执行以下操作:
- 如果中缀中的当前字符是数字,则将其复制到后缀的下一个元素。
- 如果中缀中的当前字符是左括号,则将其压入堆栈。
如果中缀中的当前字符是运算符,
- 弹出操作符(如果有的话)在堆栈顶部,当它们具有与当前操作符相同或更高的优先级时,并将弹出的操作符插入到后缀中。
- 将中缀中的当前字符压入堆栈。
- 如果中缀中的当前字符是右括号
- 从堆栈顶部弹出运算符并将它们插入到后缀中,直到左括号位于堆栈顶部。
- 从堆栈中弹出(并丢弃)左括号。