شباب بالله عليكم انا محتاج شرح الكود هادا اليوم ضروري لأني انجدوني بأول مشاركة لإلي
هادا الكود تجويل الانفكس لبوستفكس على غرار الالة الحاسة وعاوز شرحه لو ما بدي اغلبكو
#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
//---------------------------------------------------