السلام عليكم
انا استخدمت برنامج للتحويل الى infix بس ما يقبل يعمل تنفيذ
- include<iostream>
- #include<string>
- using namespace std
- const char max=30
- class polish
- {
- private:
- int topstack
- char entry[max]
- public:
- polish()
- {
- topstack=-1
- }
- int Topstack()
- {return topstack}
- bool full()
- {
- return(topstack==max-1)
- }
- bool empty()
- {
- return(topstack==-1)
- }
- char push(char value)
- {
- if (!full())
- {
- topstack++
- int x= entry[topstack]=value
- return x
- }
- else
- cout<<"stack is full n"
- return-99
- }
- char pop()
- {
- if(!empty())
- {
- char x=entry[topstack]
- topstack--
- return x
- }
- else
- {
- cout<<"stack is empty n"
- return 'z'
- }
- }
- bool operand(char x)
- {
- switch(x)
- {
- case 0:'0'
- case 1:'1'
- case 2:'2'
- case 3:'3'
- case 4:'4'
- case 5:'5'
- case 6:'6'
- case 7:'7'
- case 8:'8'
- case 9:'9'
- return true
- break
- default:
- return false
- }
- } /*** isoperand ***/
- bool isoperator(char y)
- {
- switch(y)
- {
- case 0:
- case 1:
- case 2:
- case 3:
- return true
- break
- default:
- return false
- }
- }
- bool leftbracket(char a)
- {
- return (a=='(')
- }
- bool rightbracket(char B)
- {
- return (b==')')
- }
- bool priorty(char inf, char top)
- {
- int x,y
- switch(inf)
- {
- case '+':
- case '-':
- x=1
- break
- case '*':
- case '/':
- x=2
- break
- }
- switch(top)
- {
- case '+':
- case '-':
- y=1
- break
- case '*':
- case '/':
- y=2
- break
- }
- if(x>y)
- return true
- else
- return false
- }
- }//end class
- int main()
- {
- polish st
- char postfix[50]
- char infix[50]
- cout<<"enter the infix"
- cin>>infix
- int counter=0
- int j=0
- //if infix is operand, save it on postfix
- for(int i=0 i!='\0' i++)
- { //for
- if(st.operand(infix))
- {
- postfix[counter]=infix
- j++
- counter++
- } //if
- }
- //if infix oprator
- if(st.isoperator(infix))
- if(st.empty())
- st.push(infix)
- else{
- if(st.leftbracket(st.Topstack()))
- st.push(infix)
- }
- else
- {
- if(st.isoperator(st.Topstack())&& st.priorty(infix,st.Topstack()))
- st.push(infix)
- else
- if(st.isoperator(st.Topstack()))
- {
- postfix[counter]= (st.pop())
- st.push(infix)
- j++
- }
- counter++
- j++
- } /*** end else ***/
- //if infix left bracket
- if(st.leftbracket(infix))
- st.push(infix)
- else
- {
- if(st.rightbracket(infix))
- for(int j=st.Topstack() j>=0 j--)
- {
- if(st.Topstack()=='(')
- {
- st.pop()
- break
- }
- else
- postfix[counter]=st.pop()
- }
- j++
- counter++
- }
- //if infix string is terminated
- if(infix=='\0')
- for(int j=max-1 j!=-1j--)
- st.pop()
- for(int g=0g!='\0'i++)
- j++
- counter++
- for(char k=0k='\0'k++)
- cout<<postfix[k]
- return 0
- }
