الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

infix to postfix using stack

بدأه ab0od في 27 أكتوبر 2011 · 6 رد · 3,066 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

برنامج يقوم بتحويل infix to postfix using stack and evaluate

2 −2
#2

تفضل اخي هذا البرنامج وكذلك ارفقته لك بالمرفقات تستطيع تحميلة بالتوفيق ان شاء الله

infex to postfix.txt

#include<iostream>

#include<conio.h>

using namespace std;

class Stack

{

private:

	char s[100];

	int depth;

public:

	Stack()

	{

		depth=0;

	}

	void push(char x)

	{

		s[depth++]=x;

	}

	char pop()

	{

		if(depth<0)

		depth=0;

		if(depth>=1)

		return s[--depth];

		else

		return 0;

	}

	bool IsEmpty()

	{

		return (depth==0);

	}

	char head()

	{

		if(depth>0)

			return s[depth-1];

		else

			return 0;

	}

};

void main()

{

	Stack stack;

	char *infix;

	char expression[50];

	cin>>expression;

	infix=expression;

    while(*infix!=0)

	{

		switch(*infix)

		{

		case '+':

			if(stack.head() != 0)

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('+');

			break;

		case '-':

			if(stack.head() != 0)

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('-');

			break;

		case '*':

			if(stack.head()=='*'||stack.head()=='/')

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('*');

			break;

		case '/':

			if(stack.head()=='*'||stack.head()=='/')

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('/');

			break;

		default:

			cout<<(*infix);

		}

		*(infix++);

	}

	while(!stack.IsEmpty())

	cout<<stack.pop();

	getch();

	return;

}
1
#3

وهذا حل ثاني مع الشرح بأستخدام ستاكين

وكذلك نزلته لك كود وبالمرفقات ايضاً

بالتوفيق ربي يحفظك

New Text Document Using Tow Stack.txt

#include<iostream.h>
push(char st[30],int &top,char x)
{
if(top==30)
cout<<" over flow ";
else
top++;
st[top]=x;
}
char pop(char st[30],int &top)
{char x;
if(top==0)
cout<<" empty ";
else
x=st[top];
top--;
return x;}
int re(char x)
{if(x=='+'||x=='-')
return (1);
if(x=='/'||x=='*')
return (2);
else
return (3);}
end(char st[30],char st2[30],int &top,int &top2)
{
do
{push(st,top,pop(st2,top2));}
while(top2!=0);
}
push_stack(char st[30],char st2[30],int &top,int &top2,char x)
{
if(x>='a'&&x<='z')
{push(st,top,x);cout<<x<<"(is push to stack1)\n";}
else
if(top2==0)
{push(st2,top2,x);cout<<x<<"(is push to stack2)\n";}
else if(re(st2[top2])<re(x))
{push(st2,top2,x);cout<<x<<"(is push to stack2 because it larger than)"<<st2[top2-1]<<"\n";}
else
{push(st,top,pop(st2,top2));
cout<<st2[top2]<<"(is push to stack1 after pop from stack2 because it larger or equal)"<<x<<"\n";
push(st2,top2,x);cout<<"(then"<<x<<"push to stack2)\n";}
}
main()
{
char st[30],st2[30],g;int top=0,m,top2=0,i;
do{
cin>>m;
switch(m)
{
case 1:cout<<"enter your operand or opreator ";
cin>>g;push_stack(st,st2,top,top2,g);break;
case 2:for(i=1;i<=top;i++)cout<<st;cout<<"\n";break;
case 3:if(top2==0)
cout<<" you had done succefully \n ";
else
end(st,st2,top,top2);
}
}while(m!=4);
}
#4

وهذا حل ثاني ولكن بأستخدام ستاك واحد تفضل تم الرفع بطريقتين الكود والمرفقات ايضاً

بالتوفيق ان شاء الله

ربي يحفظك

infex to postfix Using Stack Onther Solustion.txt

#include<iostream.h>
#include<string.h>
push(char st[30],int &top,char x)
{
if(top==30-1)
cout<<" over flow ";
else
top++;
st[top]=x;}
char pop(char st[30],int &top)
{char x;
if(top==-1)
cout<<" empty ";
else
x=st[top];
top--;
return x;}
int re(char x)
{if(x=='+'||x=='-')
return (1);
if(x=='/'||x=='*')
return (2);
else
return (3);}
int top_cheak(int &top)
{if(top==-1)
return (0);
else
return (1);}
end_level(char st[30],char st2[30],int &top,int &top2)
{push(st,top,pop(st2,top2));}
push_stack(char st[30],char st2[30],int &top,int &top2,char x){char e;
if(x>='a'&&x<='z')
push(st,top,x);
else
{
if(top_cheak(top2)==0)
push(st2,top2,x);
else if(re(st2[top2])>=re(x))
{e=pop(st2,top2);
push(st,top,e);
push(st2,top2,x);}
else
push(st2,top2,x);
}
}
main()
{
char st[30],st2[30],g;int top=-1,m,top2=-1,i;
do{
cin>>m;
switch(m)
{
case 1:cout<<"enter your operand or opreator ";
cin>>g;push_stack(st,st2,top,top2,g);break;
case 2:for(i=0;i<strlen(st);i++)cout<<st;cout<<"\n";break;
case 3:if(top_cheak(top2)==0)
cout<<"the duty end \n";
else
end_level(st,st2,top,top2);
}
}while(m!=4);}
#5

تفضل اخي وهذا حل ثالث وايضاً تم الرفع بطريقتين

الكود والمرفقات

pos&&inf Using Stack 3Anthor Solustion.txt

 #include <iostream.h>
 #include <conio.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>

 void push(char [],int&,char);
 char pop(char[],int &);
 void main ()
 {
    char infex[30];
    int choice ,top=-1,j=0;
    char ch,value;
    char sk[15];
 char postfix[15];
    do {
         cout<<"1-convert from infex to postfix.\n"
             <<"2-exit.\n"
             <<"please enter your choice:";

         cin>>choice;

         if(choice==1)
            {
               cout<<"please enter the infex formula: ";
               gets(infex);

               for(int i=0;i<strlen(infex);i++)//loop for all infex formla
                  { //check if infex is operation or digit
                    if(infex=='+'||infex=='-'||infex=='*'||infex=='/'||infex=='('||infex==')')//check if infex is operation
                      {

                          if(infex=='*'||infex=='/'||infex=='(')
                            push(sk,top,infex);//push the operation on the
                                                  //stack
                           else if(infex=='+'||infex=='-')//check if the
                                                                //operation(+)or
                                                               //(-)for priotiry
                             {
                               if(sk[top]=='*'||sk[top]=='/')//the priotiry for
                                                        //(*)&(/) before(+)&(-)
                                 {
                                   while(sk[top]!='('&&top>-1)//I pop all
                                      // operation from stack then put them into
                                    //postfix until found '('.if top=-1,I cannot
                                    //pop
                                      {
                                        value=pop(sk,top);
                                        postfix[j]=value;
                                        j++;
                                      }
                                 push(sk,top,infex);//I push the oper. later
                                 }
                               else
                                  push(sk,top,infex);//I push the oper. if
                                                       //not found(*)or(/)
                             }
                          else if(infex==')')//the priority for'()' is major
                          {
                             while(sk[top]!='(')//I pop all
                                      // operation from stack then put them into
                                    //postfix until found '(' then skip it by
                                    //top--;
                             {
                               value=pop(sk,top);
                               postfix[j]=value;
                               j++;
                             }
                             top--;
                          }
                    }



                      else
                        {postfix[j]=infex;//if infex not operation put it
                                             //into postfix
                        j++;
                        }
               }
                while(top>-1)//pop all remainder operation from the stack then
                             //put it into postfix.
                {
                  value=pop(sk,top);
                  postfix[j]=value;
                  j++;
                }
                postfix[j]='\0';
                cout<<"the formula after its convert to postfix is= "<<postfix;
            }
            else if(choice==2)
               exit(0);

            else
            {

              cout<<"Your enter is invalid...\n"
                  <<"\nnAre you want try again? Y/N\n";
                  cin>>ch;
            }
    //Now I compute the formula
    for(int i=0;i<strlen(postfix);i++)
    {
      if(postfix=='+'||postfix=='-'||postfix=='*'||postfix=='/'||postfix=='('||postfix==')')
      {
       int a,b,c;
        value=pop(sk,top);
        a=value-48;
        value=pop(sk,top);
        b=value-48;
        if(postfix=='+')
        c=a+b;
        else if(postfix=='-')
        c=a-b;
        else if(postfix=='*')
        c=a*b;
        else
        c=a/b;
        value=c+48;
        push(sk,top,value);
      }
      else
      push(sk,top,postfix);
    }
    value=pop(sk,top);
    int result=value-48;
    cout<<"\nthe result= "<<result;
    cout<<"\nAre you want try again? Y/N\n";
                  cin>>ch;
            getch();
            clrscr();
         }while(ch=='Y'||ch=='y');
         }
//end main--------------------------------------------

//begin function push
   void push(char sk[15],int&top ,char value)
   {
     if(top<14)
       {
         top++;
         sk[top]=value;
       }
     else
        cout<<"Stack is FULL...";
   }//end push
//----------------------------------------------


//begin function pop
   char pop(char sk[15],int &top)
   { char v;
      if(top>-1)
      {
       v=sk[top];
       top--;
      }
      else
         cout<<"Stack is EMPTY....";
   return v;
   }//end function
//---------------------------------------------------

وهذا حل اخر ولكن بأستخدام Linked List

وكذلك تم الرفع عن طريق الكود والمرفقات

infix to positfix using stack with linked list.txt

وهذا الكود

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>

struct stack
{
	char c;
	stack *next;
};

void push(stack *&top,char);
char pop(stack *&top);

void main()
{
	char pos[80],inf[80],v;
	stack *top=NULL;
	int j;
   char ch;
	do{
      j=0;
		cout<<"Enter the equation you want conver it to postfix: ";
		gets(inf);
		for( int i=0;i<strlen(inf);i++)
		{
			if(inf>='0'&&inf<='9')
			{
				pos[j]=inf;
				j++;
			}


         else if(inf=='+'||inf=='-')
				{
					if(top!=NULL&&(top->c=='*'||top->c=='/'))
					{
						while(top!=NULL&&top->c!='(')
						{
							v=pop(top);
							pos[j]=v;
							j++;
						}
						push(top,inf);
					}
					else
						push(top,inf);
				}
				else if(inf==')')
				{
					while(top->c!='(')
					{
						v=pop(top);
						pos[j]=v;
						j++;
					}
					stack *temp=top;
					top=top->next;
					delete(temp);
				}
         else if(inf=='*'||inf=='/'||inf=='(')
					push(top,inf);
		}//end for
		while(top!=NULL)
		{
			v=pop(top);
			pos[j]=v;
			j++;
		}

		pos[j]='\0';
		cout<<"The equation after convert it to postfix: ";
		cout<<pos<<endl;


		for(int j=0;j<strlen(pos);j++)
			{
			   if(pos[j]>='0'&&pos[j]<='9')
				   push(top,pos[j]);
			   else
			   {
               int a,b,c;
				   v=pop(top);
				   a=v-48;
				   v=pop(top);
				   b=v-48;
				   if(pos[j]=='+')
					   c=a+b;
				   else if(pos[j]=='-')
					   c=a-b;
				   else if(pos[j]=='*')
					   c=a*b;
				   else if(pos[j]=='/')
					   c=a/b;
				   v=c+48;
				   push(top,v);
			   }
            }
			   v=pop(top);
			   int result=v-48;

			   cout<<"\nThe result:"<<result;


		cout<<"\nAre you want continue?Y/N ";
		cin>>ch;
	}while(ch=='Y'||ch=='y');
   getch();
}//end main
//----------------------------

void push(stack *&top,char a)
{
	stack *temp;
	temp=new stack;
	temp->c=a;
	temp->next=top;
	top=temp;
}//end function;

char pop(stack *&top)
{
	stack *temp;
	char v;
      v=top->c;
      temp=top;
		top=top->next;
		delete(temp);
      return v;
}//end function;

هاهي اخي هذه كل الحلول التي لدي

لاتنساني انت ولا كل من يقرأ الموضوع من صالح دعائه عن صلاته وبظهر الغيب لي ولوالدي ولجميع المسلمين

لا اريد شي سوى الدعاء بنية صادقة جزاكم الله خير

وبالتوفيق اخي ان شاء الله

ربي يسهل لك

1
#6
مبرمج بلا حدود كتب:

وهذا حل ثاني مع الشرح بأستخدام ستاكين

وكذلك نزلته لك كود وبالمرفقات ايضاً

بالتوفيق ربي يحفظك

attachicon.gifNew Text Document Using Tow Stack.txt

 

#include<iostream.h>push(char st[30],int &top,char x){if(top==30)cout<<" over flow ";elsetop++;st[top]=x;}char pop(char st[30],int &top){char x;if(top==0)cout<<" empty ";elsex=st[top];top--;return x;}int re(char x){if(x=='+'||x=='-')return (1);if(x=='/'||x=='*')return (2);elsereturn (3);}end(char st[30],char st2[30],int &top,int &top2){do{push(st,top,pop(st2,top2));}while(top2!=0);}push_stack(char st[30],char st2[30],int &top,int &top2,char x){if(x>='a'&&x<='z'){push(st,top,x);cout<<x<<"(is push to stack1)\n";}elseif(top2==0){push(st2,top2,x);cout<<x<<"(is push to stack2)\n";}else if(re(st2[top2])<re(x)){push(st2,top2,x);cout<<x<<"(is push to stack2 because it larger than)"<<st2[top2-1]<<"\n";}else{push(st,top,pop(st2,top2));cout<<st2[top2]<<"(is push to stack1 after pop from stack2 because it larger or equal)"<<x<<"\n";push(st2,top2,x);cout<<"(then"<<x<<"push to stack2)\n";}}main(){char st[30],st2[30],g;int top=0,m,top2=0,i;do{cin>>m;switch(m){case 1:cout<<"enter your operand or opreator ";cin>>g;push_stack(st,st2,top,top2,g);break;case 2:for(i=1;i<=top;i++)cout<<st;cout<<"\n";break;case 3:if(top2==0)cout<<" you had done succefully \n ";elseend(st,st2,top,top2);}}while(m!=4);}

لو سمجتم اريد شرح الاكواد

#7
مبرمج بلا حدود كتب:

تفضل اخي هذا البرنامج وكذلك ارفقته لك بالمرفقات تستطيع تحميلة بالتوفيق ان شاء الله

attachicon.gifinfex to postfix.txt

 

#include<iostream>

#include<conio.h>

using namespace std;

class Stack

{

private:

	char s[100];

	int depth;

public:

	Stack()

	{

		depth=0;

	}

	void push(char x)

	{

		s[depth++]=x;

	}

	char pop()

	{

		if(depth<0)

		depth=0;

		if(depth>=1)

		return s[--depth];

		else

		return 0;

	}

	bool IsEmpty()

	{

		return (depth==0);

	}

	char head()

	{

		if(depth>0)

			return s[depth-1];

		else

			return 0;

	}

};

void main()

{

	Stack stack;

	char *infix;

	char expression[50];

	cin>>expression;

	infix=expression;

    while(*infix!=0)

	{

		switch(*infix)

		{

		case '+':

			if(stack.head() != 0)

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('+');

			break;

		case '-':

			if(stack.head() != 0)

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('-');

			break;

		case '*':

			if(stack.head()=='*'||stack.head()=='/')

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('*');

			break;

		case '/':

			if(stack.head()=='*'||stack.head()=='/')

				while(!stack.IsEmpty())

				cout<<stack.pop();

			stack.push('/');

			break;

		default:

			cout<<(*infix);

		}

		*(infix++);

	}

	while(!stack.IsEmpty())

	cout<<stack.pop();

	getch();

	return;

}

مواضيع مشابهة