Tải bản đầy đủ (.docx) (11 trang)

Chuyển đổi trung tố sang hậu tố, tiền tố

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (458.65 KB, 11 trang )

Chuyển đổi Trung tố(Infix) sang Hậu tố(Postfix)
 Chuyển đổi từ trung tố sang hậu tố ” là một ví dụ cổ điển về cấu trúc dữ liệu
ngăn xếp. Ngăn xếp có thể được sử dụng để chuyển đổi biểu thức trung tố đã
cho thành biểu thức hậu tố tương ứng.
 Toán tử : Toán tử là những ký hiệu hướng dẫn máy tính thực hiện các tác vụ
đơn giản và đơn lẻ. Ví dụ về các tốn tử bao gồm + (Cộng), – (Phép trừ), *
(nhân),… và nhiều toán tử khác.
 Toán hạng : Toán hạng là các giá trị hoặc biến mà toán tử thực hiện các nhiệm
vụ của nó. Ví dụ về Tốn hạng bao gồm “a”, “b”, 23, 12,.. và nhiều hơn nữa.

Các bước chuyển đổi biểu thức trung tố thành
hậu tố
1.
2.
3.
4.
5.
6.
7.

Quét biểu thức trung tố hợp lệ từ trái sang phải.
Khởi tạo ngăn xếp ký tự trống và chuỗi hậu tố trống.
Nếu ký tự được quét là tốn hạng, hãy thêm nó vào chuỗi hậu
tố.
Nếu ký tự được quét là dấu ngoặc đơn mở, hãy đẩy nó vào ngăn
xếp.
Nếu ký tự được quét là dấu ngoặc đơn đóng, hãy bật tất cả các
ký tự của ngăn xếp và nối vào cuối chuỗi hậu tố cho đến khi xuất
hiện dấu ngoặc đơn mở.
Bật dấu ngoặc mở.
Nếu ký tự được qt là Tốn tử và ngăn xếp khơng trống, hãy so


sánh mức độ ưu tiên của ký tự với phần tử ở đầu ngăn xếp. Nếu
phần tử trên cùng của Ngăn xếp có mức độ ưu tiên cao hơn ký tự
được quét, hãy bật ngăn xếp, nếu không sẽ đẩy ký tự được quét
vào ngăn xếp. Lặp lại bước này cho đến khi ngăn xếp không
trống và ngăn xếp trên cùng được ưu tiên hơn ký tự.
Toán tử
( + *
/ %
Mức ưu tiêu 1 2 2 3 3 3

8.

Lặp lại các bước từ 3 đến 7 cho đến khi tất cả các ký tự được
quét.
9. Nếu ngăn xếp trống, trả về chuỗi hậu tố.
10. Ngược lại, lấy các phần tử của ngăn xếp và thêm nó vào chuỗi
hậu tố, sau đó trả về chuỗi hậu tố.
Ví dụ (Chuyển đổi Infix sang Postfix): P = (a+(b*c)/(d-e)) = abc*de-/
+


Thuật toán:
#include<iostream>
#include<string>
using namespace std;
class stack
{
string item;
int top;
public:

stack()
{


top=-1;
}
void push(char ch)
{
top++;
item[top]=ch;
}
char pop()
{
if(isempty())
{
cout<<"stack underflow!!";
return '@';
}
int ele;
ele=item[top];
top--;
return ele;
}
int isempty()
{
if(top==-1)
return 1;
return 0;
}
char peep()

{
return(item[top]);
}
int priority(char ch)
{
if(ch=='(')
return 1;
else if(ch=='+' || ch=='-')
return 2;
else if(ch=='*' || ch=='/' || ch=='%')
return 3;
else
return 4;
}
};
int main()


{
string infix,postfix;
char token,token_out,ch2,ch;
int i,len;
stack st;
cout<<"Enter the infix expression: ";
cin>>infix;
len=infix.size();
for(i=0;i{
token=infix[i];
// If open parenthesis occurs, Push it into stack

if(token=='(')
{
st.push(token);
}
/* If Closing parenthesis occurs,
Pop the stack until Open parenthesis occurs*/
else if(token==')')
{
token_out=st.pop();
while(token_out!='(')
{
postfix.push_back(token_out);
token_out=st.pop();
}
}
// If character is operator
else if(infix[i]=='+' || infix[i]=='-' || infix[i]=='*' || infix[i]=='/' || infix[i]=='^')
{
ch2=st.peep();
while( !st.isempty() && st.priority(infix[i])<=st.priority(ch2))
{
ch=st.pop();
postfix.push_back(ch);
ch2=st.peep();
}
st.push(infix[i]);
}
// If character is operand, push it into stack
else
st.push(infix[i]);



}
/*Empty the stack and concatenate
the character to output String */
while(!st.isempty())
{
ch=st.pop();
postfix.push_back(ch);
}
cout<<"Postfix Expression: "<return 0;
}
Chuyển đổi biểu thức hậu tố sau thành biểu thức tiền tố

/* Program for Postfix to Prefix Conversion */
#include <iostream>
#include<string>
#define sizes 100
using namespace std;
class stack
{
string item[sizes];
int top;
public:
stack()
{
top=-1;
}



void push(string str)
{
if(top==sizes-1)
{
cout<<"stack overflow!!";
return;
}
top++;
item[top]=str;
}
string pop()
{
string temp;
if(top==-1)
{
cout<<"stack underflow!!";
return "abc";
}
temp= item[top];
top--;
return temp;
}
};
int main(int argc, char** argv) {
stack st;
string postfix;
int i;
cout<<"Enter the valid postfix string:\n";
cin>>postfix;

for(i=0;i{
if(postfix[i]=='+' || postfix[i]=='-' || postfix[i]=='*' || postfix[i]=='/' ||
postfix[i]=='^')
{
string op1,op2,temp;
op2=st.pop();
op1=st.pop();
temp=postfix[i]+op1+op2;
st.push(temp);
}
else
{


string flag;
flag=flag+postfix[i];
st.push(flag);
}
}
cout<<"The equivalent prefix expression:\n"<return 0;
}
Chuyển đổi biểu thức tiền tố sau thành hậu tố

/* Program for Prefix to Postfix Conversion */
#include <iostream>
#include <string>
#define sizes 100
using namespace std;

class stack
{
string item[sizes];
int top;
public:
stack()
{
top=-1;
}
void push(string str)
{
if(top==sizes-1)


{
cout<<"stack overflow!!";
return;
}
top++;
item[top]=str;
}
string pop()
{
if(top==-1)
{
cout<<"stack underflow!!";
return "abc";
}
string temp;
temp=item[top];

top--;
return temp;
}
};
int main(int argc, char** argv)
{
int i;
string prefix;
stack st;
cout<<"Enter valid prefix expression: ";
cin>>prefix;
for(i=prefix.size()-1;i>=0;i--)
{
if(prefix[i]=='+' || prefix[i]=='-' || prefix[i]=='*' || prefix[i]=='/' || prefix[i]=='^')
{
string op1,op2,temp;
op1=st.pop();
op2=st.pop();
temp=op1+op2+prefix[i];
st.push(temp);
}
else
{
string flag;
flag=flag+prefix[i];
st.push(flag);


}
}

cout<<"The equivalent postfix expression: "<return 0;
}
Chuyển đổi biểu thức tiền tố sau thành biểu thức trung tố

/* Program for converting Prefix to Infix Using Stack */
#include <iostream>
#include<string>
#define sizes 100
using namespace std;
class stack
{
string item[sizes];
int top;
public:
stack()
{
top=-1;
}
void push(string st)
{
if(top==sizes-1)
{
cout<<"stack overflow!!";


return;
}
top++;
item[top]=st;

}
string pop()
{
int i;
string temp;
if(top==-1)
{
cout<<"stack underflow!!\n";
return "abc";
}
temp = item[top];
top--;
return temp;
}
};
int main(int argc, char** argv)
{
int i;
stack st;
string prefix;
cout<<"Enter valid prefix string: ";
cin>>prefix;
for(i=prefix.size()-1;i>=0;i--)
{
if(prefix[i]=='+' || prefix[i]=='-' || prefix[i]=='*' || prefix[i]=='/' || prefix[i]=='^')
{
string op1,op2,temp;
op1=st.pop();
op2=st.pop();
temp='('+op1+prefix[i]+op2+')';

st.push(temp);
}
else
{
string flag;
flag=flag+prefix[i];
st.push(flag);
}


}
cout<<"The resultant infix string is: "<return 0;
}



×