0
与えられた条件に基づいてスタックを使用したpostfixとその評価の式変換のためのC++プログラムの実装 オペランドと演算子両方とも1文字でなければなりません。 入力Postfix式は、必要な形式でなければなりません。 関数 'evaluate'にpush(po [i] - '0')文の重要性が分かりませんpostfixの式を評価するための以下のスニペットのpush(po [i] - '0')の重要性
#include<iostream>
using namespace std;
class stack
{
char st[20],in[20],po[20];
int TOP,k;
public:
stack()
{
TOP=-1;
k=0;
}
void infixToPostfix();
void evaluate();
private:
void push(char);
char pop();
int precedence(char);
};
void stack::push(char ch)
{
if(TOP==19)
{
cout<<"Stack overflow"<<endl;
}
else
{
TOP++;
st[TOP]=ch;
}
}
char stack::pop()
{
if(TOP==-1)
{
cout<<"Stack underflow"<<endl;
return 0;
}
else
{
int m=st[TOP];
TOP--;
return m;
}
}
void stack::evaluate()
{
cout<<"The postfix expression is"<<endl<<po<<endl;;
int a,b,res,temp;
TOP=-1;
for(int i=0;po[i]!='\0';i++)
{
if(isdigit(po[i])==1)
{
push(po[i]-'0');
}
else
{
a=pop();
b=pop();
switch(po[i])
{
case '+': res=b+a;
break;
case '-': res=b-a;
break;
case '*': res=b*a;
break;
case '/': res=b/a;
break;
}
push(res);
}
}
temp=pop();
cout<<"The answer is "<<temp<<endl;
}
void stack::infixToPostfix()
{
int m;
char left='(',right=')';
cout<<"Enter infix expression"<<endl;
cin>>in;
for(int i=0;in[i]!='\0';i++) //if operand add it to postfix
{
if(isalpha(in[i])==1 || isdigit(in[i]==1))
{
po[k]=in[i];
k++;
}
else if(in[i]==left) //if left parenthesis then push it to stack;
{
push(left);
}
else if(in[i]==right) //if right parenthesis encountered then pop from stack until left parenthesis
{
while((m=pop())!=left)
{
po[k]=m;
k++;
}
}
else //if operator is encounterd pop from the stack the operands having equal or higher precedence
{
while(precedence(st[TOP])>=precedence(in[i]))
{
int m=pop();
po[k]=m;
k++;
}
push(in[i]);
}
}
while(TOP>=0)
{
po[k]=pop();
k++;
}
po[k]='\0';
cout<<"The postfix expression is"<<endl;
cout<<po;
}
int stack::precedence(char ch)
{
if(ch=='+' || ch=='-')
{
return 1;
}
else if(ch=='*' || ch=='/')
{
return 2;
}
else if(ch=='(')
{
return 0;
}
}
int main()
{
stack s;
int op;
do
{
cout<<"\n____________________________"<<endl;
cout<<"1 Postfix to infix conversion"<<endl;
cout<<"2 Evaluation of postfix"<<endl;
cout<<"3 Exit"<<endl;
cout<<"______________________________"<<endl;
cin>>op;
switch(op)
{
case 1: s.infixToPostfix();
break;
case 2: s.evaluate();
break;
case 3:break;
default: cout<<"Enter correct option"<<endl;
}
}while(op!=3);
return 0;
}
コード例から不要な深い字下げを削除しました。これにより、読みやすくなります。また、関数の後にいくつかの改行を追加して、どこで開始と終了を見やすいようにしました。質問/回答を投稿するときにプレビューを使用して、投稿する前に改善できる点を確認してください。 – Hayt