私の講師は、スタックを使用して式を後置に変換および挿入するプログラムを作成するという課題を与えました。中置式を読み取るためのスタック クラスといくつかの関数を作成しました。
しかし、convertToPostfix(char * const inFix, char * const postFix)
スタックを使用して配列 inFix 内の inFix 式を配列 postFix 内の事後修正式に変換する役割を担うこの 1 つの関数は、想定どおりの動作をしていません。皆さんは私を助けて、私が間違っていることを教えてもらえますか?
以下は、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 は、このアルゴリズムを使用して作成されていることに注意してください。
- 左括弧 '(' をスタックにプッシュします。
- infix の末尾に右括弧 ')' を追加します。
スタックが空でない間、左から右に infix を読み取り、次のことを行います。
- infix の現在の文字が数字の場合、postfix の次の要素にコピーします。
- infix の現在の文字が左括弧である場合、それをスタックにプッシュします。
infix の現在の文字が演算子の場合、
- 現在の演算子と同等またはそれ以上の優先順位を持つスタックの一番上に演算子 (存在する場合) をポップし、ポップされた演算子を後置に挿入します。
- infix 内の現在の文字をスタックにプッシュします。
- infix の現在の文字が右括弧の場合
- スタックの一番上から演算子をポップし、左括弧がスタックの一番上になるまで後置に挿入します。
- スタックから左括弧をポップ (および破棄) します。