#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define Max 20
char stOper[Max][Max], Opertop=-1;
char stOptr[Max][Max], Optrtop=-1;
int st[Max], top=-1;
void push(int ch)
{
if (top == Max-1)
{
printf("Stack is full\n");
}
else
{
top++;
st[top]=ch;
}
}
int pop()
{
int ch;
if (top==-1)
{
printf("Stack is empty\n");
}
else
{
ch=st[top];
top--;
}
return ch;
}
void dispstack()
{
int k;
printf("stack Content: ");
for (k=top; k>=0; k--)
{
printf("%d, ", st[k]);
}
printf("\n");
}
void pushOperand(char ch[25])
{
if (Opertop == Max-1)
{
printf("Operand Stack is full\n");
}
else
{
Opertop++;
strcpy(stOper[Opertop],ch);
}
}
char* popOperand()
{
char ch[25];
if (Opertop==-1)
{
printf("Operand Stack is empty\n");
}
else
{
strcpy(ch,stOper[Opertop]);
Opertop--;
}
return ch;
}
void dispOperandstack()
{
int k;
printf("Operand stack Content: ");
for (k=Opertop; k>=0; k--)
{
printf("%s, ", stOper[k]);
}
printf("\n");
}
void pushOperator(char ch[25])
{
if (Optrtop == Max-1)
{
printf("Operator Stack is full\n");
}
else
{
Optrtop++;
strcpy(stOptr[Optrtop],ch);
}
}
char* popOperator()
{
char ch[25];
if (Optrtop==-1)
{
printf("Operator Stack is empty\n");
}
else
{
strcpy(ch,stOptr[Optrtop]);
Optrtop--;
}
return ch;
}
void dispOperatorstack()
{
int k;
printf("Operator stack Content: ");
for (k=Optrtop; k>=0; k--)
{
printf("%s, ", stOptr[k]);
}
printf("\n");
}
int ICP(char ch)
{
int pre,pre1;
pre=toascii(ch);
if (pre==43 || pre==45) pre1= 1; /* + and - */
else if (pre==42 || pre==47) pre1= 2; /* * and / */
else if (pre==33) pre1=-1; /* ! */
else if (pre==40) pre1=0; /* Closing parenthesis ) */
return pre1;
}
char * Prefix(char s[25])
{
char pre[25],preOpt[25],optr[25],op1[25],op2[25];
int i,j;
i=0; j=0;
Optrtop=-1;
Opertop=-1;
pushOperator("!");
pushOperand("!");
while (s[i]!='\0')
{
/*if operand is countered print it */
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) ||(s[i]>=48 && s[i]<=57) )
{
j=0;
pre[j]=s[i];
j++;
pre[j]='\0';
pushOperand(pre);
}
else if (s[i]=='(')
{
pushOperator("(");
}
else if (s[i]==')')
{
while (strcmp(stOptr[Optrtop],"(")!=0)
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pre,optr);
strcat(pre,op1);
strcat(pre,op2);
pushOperand(pre);
}
strcpy(optr,popOperator()); /* pop closing parenthsis but don't print it */
}
else if ( ICP(s[i]) > ICP(stOptr[Optrtop][0]) )
{
j=0;
preOpt[j]=s[i];
j++;
preOpt[j]='\0';
pushOperator(preOpt);
}
else
{
do
{
strcpy(optr,popOperator());
strcpy(op2,popOperand());
strcpy(op1,popOperand());
strcpy(pre,optr);
strcat(pre,op1);
strcat(pre,op2);
pushOperand(pre);
}
while (ICP(s[i])<= ICP(stOptr[Optrtop][0]) );
j=0;
preOpt[j]=s[i];
j++;
preOpt[j]='\0';
pushOperator(preOpt);
}
i++;
/* dispOperatorstack();
dispOperandstack();
getch(); */
}
while ( strcmp(stOptr[Optrtop],"!")!=0 )
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pre,optr);
strcat(pre,op1);
strcat(pre,op2);
pushOperand(pre);
}
/* dispOperatorstack();
dispOperandstack();*/
strcpy(pre,popOperand());
return pre;
}
char * Postfix(char s[25])
{
char pos[25],posOpt[25],optr[25],op1[25],op2[25];
int i,j;
i=0; j=0;
Optrtop=-1;
Opertop=-1;
pushOperator("!");
pushOperand("!");
while (s[i]!='\0')
{
/*if operand is countered print it */
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) ||(s[i]>=48 && s[i]<=57) )
{
j=0;
pos[j]=s[i];
j++;
pos[j]='\0';
pushOperand(pos);
}
else if (s[i]=='(')
{
pushOperator("(");
}
else if (s[i]==')')
{
while (strcmp(stOptr[Optrtop],"(")!=0)
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pos,op1);
strcat(pos,op2);
strcat(pos,optr);
pushOperand(pos);
}
strcpy(optr,popOperator()); /* pop closing parenthsis but don't print it */
}
else if ( ICP(s[i]) > ICP(stOptr[Optrtop][0]) )
{
j=0;
posOpt[j]=s[i];
j++;
posOpt[j]='\0';
pushOperator(posOpt);
}
else
{
do
{
strcpy(optr,popOperator());
strcpy(op2,popOperand());
strcpy(op1,popOperand());
strcpy(pos,op1);
strcat(pos,op2);
strcat(pos,optr);
pushOperand(pos);
}
while (ICP(s[i])<= ICP(stOptr[Optrtop][0]) );
j=0;
posOpt[j]=s[i];
j++;
posOpt[j]='\0';
pushOperator(posOpt);
}
i++;
/*dispOperatorstack();
dispOperandstack();
getch(); */
}
while ( strcmp(stOptr[Optrtop],"!")!=0 )
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pos,op1);
strcat(pos,op2);
strcat(pos,optr);
pushOperand(pos);
}
/* dispOperatorstack();
dispOperandstack();*/
strcpy(pos,popOperand());
return pos;
}
int PostEval(char s[25])
{
char temp[25];
int i,val=0,ch1,ch2,j=0;
i=0; top=-1;
while (s[i]!='\0')
{
/*if operand is countered print it*/
if ( (s[i]>=48 && s[i]<=57) )
{
j=0;
temp[j]=s[i];
j++;
temp[j]='\0';
push(atoi(temp));
}
else
{
ch1=pop();
ch2=pop();
switch(s[i])
{
case '+' :{
val=ch2+ch1;
break;
}
case '-' :{
val=ch2-ch1;
break;
}
case '*' :{
val=ch2*ch1;
break;
}
case '/' :{
val=ch2/ch1;
break;
}
}
push(val);
}
i++;
}
val=pop();
return val;
}
int PreEval(char s[25])
{
char temp[25];
int i,val=0,ch1,ch2,j=0;
i=0; top=-1;
while (s[i]!='\0')
{
/*if operand is countered print it*/
if ( (s[i]>=48 && s[i]<=57) )
{
j=0;
temp[j]=s[i];
j++;
temp[j]='\0';
push(atoi(temp));
}
else
{
ch2=pop();
ch1=pop();
switch(s[i])
{
case '+' :{
val=ch2+ch1;
break;
}
case '-' :{
val=ch2-ch1;
break;
}
case '*' :{
val=ch2*ch1;
break;
}
case '/' :{
val=ch2/ch1;
break;
}
}
push(val);
}
i++;
}
val=pop();
return val;
}
char* PosttoInfix(char s[25])
{
char ifx[25],ch1[25],ch2[25],ch3[25];
int i,j;
i=0; j=0; Opertop=-1;
while (s[i]!='\0')
{
/*if operand is countered print it */
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) || (s[i]>=48 && s[i]<=57) )
{
j=0;
ifx[j]=s[i];
j++;
ifx[j]='\0';
pushOperand(ifx);
}
else
{
strcpy(ch1,popOperand());
strcpy(ch2,popOperand());
j=0;
ch3[j]=s[i];
j++;
ch3[j]='\0';
strcpy(ifx,"(");
strcat(ifx,ch2);
strcat(ifx,ch3);
strcat(ifx,ch1);
strcat(ifx,")");
pushOperand(ifx);
}
i++;
}
return popOperand();
}
char* PretoInfix(char s[25])
{
char ifx[25],ch1[25],ch2[25],ch3[25];
int i,j;
i=0; j=0; Opertop=-1;
while (s[i]!='\0')
{
/*if operand is countered print it */
/*printf("s=%c\n",s[i]);*/
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) || (s[i]>=48 && s[i]<=57) )
{
j=0;
ifx[j]=s[i];
j++;
ifx[j]='\0';
pushOperand(ifx);
}
else
{
strcpy(ch1,popOperand());
strcpy(ch2,popOperand());
j=0;
ch3[j]=s[i];
j++;
ch3[j]='\0';
strcpy(ifx,ch1);
strcat(ifx,ch3);
strcat(ifx,ch2);
pushOperand(ifx);
}
i++;
}
return popOperand();
}
void main()
{
char s[25],pre[25],pos[25],PtoInf[25],PrtoInf[25],prerev[25];
int val,choice;
while(1)
{
clrscr();
printf("1. Infix to Postfix Expression\n");
printf("2. Infix to Prefix Expression \n");
printf("3. Postfix to Infix Expression \n");
printf("4. Prefix to Infix Expression \n");
printf("5. Evaluation of Postfix Expression \n");
printf("6. Evaluation of Prefix Expression \n");
printf("7. Exit\n");
printf("Enter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: {
printf("Enter Infix Expression\n");
scanf("%s",s);
strcpy(pre,Postfix(s));
printf("Postfix Expression = %s\n", pre);
break;
}
case 2: {
printf("Enter Infix Expression\n");
scanf("%s",s);
strcpy(pre,Prefix(s));
printf("Prefix Expression = %s\n", pre);
break;
}
case 3: {
printf("Enter Postfix Expression\n");
scanf("%s",s);
strcpy(PtoInf,PosttoInfix(s));
printf("Postfix to Infix =%s\n", PtoInf);
break;
}
case 4: {
printf("Enter prefix Expression\n");
scanf("%s",s);
strcpy(prerev, strrev(s));
strcpy(PrtoInf,PretoInfix(prerev));
printf("Prefix to Infix =%s\n", PrtoInf);
break;
}
case 5: {
printf("Enter Postfix Expression\n");
scanf("%s",s);
val= PostEval(s);
printf("Value of Postfix Expression=%d\n", val);
break;
}
case 6: {
printf("Enter Prefix Expression\n");
scanf("%s",s);
strcpy(prerev,strrev(s));
val=PreEval(prerev);
printf("Value of Prefix Expression = %d\n", val);
break;
}
case 7: {
exit(0);
}
}
getch();
}
}
#include<ctype.h>
#include<stdlib.h>
#define Max 20
char stOper[Max][Max], Opertop=-1;
char stOptr[Max][Max], Optrtop=-1;
int st[Max], top=-1;
void push(int ch)
{
if (top == Max-1)
{
printf("Stack is full\n");
}
else
{
top++;
st[top]=ch;
}
}
int pop()
{
int ch;
if (top==-1)
{
printf("Stack is empty\n");
}
else
{
ch=st[top];
top--;
}
return ch;
}
void dispstack()
{
int k;
printf("stack Content: ");
for (k=top; k>=0; k--)
{
printf("%d, ", st[k]);
}
printf("\n");
}
void pushOperand(char ch[25])
{
if (Opertop == Max-1)
{
printf("Operand Stack is full\n");
}
else
{
Opertop++;
strcpy(stOper[Opertop],ch);
}
}
char* popOperand()
{
char ch[25];
if (Opertop==-1)
{
printf("Operand Stack is empty\n");
}
else
{
strcpy(ch,stOper[Opertop]);
Opertop--;
}
return ch;
}
void dispOperandstack()
{
int k;
printf("Operand stack Content: ");
for (k=Opertop; k>=0; k--)
{
printf("%s, ", stOper[k]);
}
printf("\n");
}
void pushOperator(char ch[25])
{
if (Optrtop == Max-1)
{
printf("Operator Stack is full\n");
}
else
{
Optrtop++;
strcpy(stOptr[Optrtop],ch);
}
}
char* popOperator()
{
char ch[25];
if (Optrtop==-1)
{
printf("Operator Stack is empty\n");
}
else
{
strcpy(ch,stOptr[Optrtop]);
Optrtop--;
}
return ch;
}
void dispOperatorstack()
{
int k;
printf("Operator stack Content: ");
for (k=Optrtop; k>=0; k--)
{
printf("%s, ", stOptr[k]);
}
printf("\n");
}
int ICP(char ch)
{
int pre,pre1;
pre=toascii(ch);
if (pre==43 || pre==45) pre1= 1; /* + and - */
else if (pre==42 || pre==47) pre1= 2; /* * and / */
else if (pre==33) pre1=-1; /* ! */
else if (pre==40) pre1=0; /* Closing parenthesis ) */
return pre1;
}
char * Prefix(char s[25])
{
char pre[25],preOpt[25],optr[25],op1[25],op2[25];
int i,j;
i=0; j=0;
Optrtop=-1;
Opertop=-1;
pushOperator("!");
pushOperand("!");
while (s[i]!='\0')
{
/*if operand is countered print it */
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) ||(s[i]>=48 && s[i]<=57) )
{
j=0;
pre[j]=s[i];
j++;
pre[j]='\0';
pushOperand(pre);
}
else if (s[i]=='(')
{
pushOperator("(");
}
else if (s[i]==')')
{
while (strcmp(stOptr[Optrtop],"(")!=0)
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pre,optr);
strcat(pre,op1);
strcat(pre,op2);
pushOperand(pre);
}
strcpy(optr,popOperator()); /* pop closing parenthsis but don't print it */
}
else if ( ICP(s[i]) > ICP(stOptr[Optrtop][0]) )
{
j=0;
preOpt[j]=s[i];
j++;
preOpt[j]='\0';
pushOperator(preOpt);
}
else
{
do
{
strcpy(optr,popOperator());
strcpy(op2,popOperand());
strcpy(op1,popOperand());
strcpy(pre,optr);
strcat(pre,op1);
strcat(pre,op2);
pushOperand(pre);
}
while (ICP(s[i])<= ICP(stOptr[Optrtop][0]) );
j=0;
preOpt[j]=s[i];
j++;
preOpt[j]='\0';
pushOperator(preOpt);
}
i++;
/* dispOperatorstack();
dispOperandstack();
getch(); */
}
while ( strcmp(stOptr[Optrtop],"!")!=0 )
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pre,optr);
strcat(pre,op1);
strcat(pre,op2);
pushOperand(pre);
}
/* dispOperatorstack();
dispOperandstack();*/
strcpy(pre,popOperand());
return pre;
}
char * Postfix(char s[25])
{
char pos[25],posOpt[25],optr[25],op1[25],op2[25];
int i,j;
i=0; j=0;
Optrtop=-1;
Opertop=-1;
pushOperator("!");
pushOperand("!");
while (s[i]!='\0')
{
/*if operand is countered print it */
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) ||(s[i]>=48 && s[i]<=57) )
{
j=0;
pos[j]=s[i];
j++;
pos[j]='\0';
pushOperand(pos);
}
else if (s[i]=='(')
{
pushOperator("(");
}
else if (s[i]==')')
{
while (strcmp(stOptr[Optrtop],"(")!=0)
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pos,op1);
strcat(pos,op2);
strcat(pos,optr);
pushOperand(pos);
}
strcpy(optr,popOperator()); /* pop closing parenthsis but don't print it */
}
else if ( ICP(s[i]) > ICP(stOptr[Optrtop][0]) )
{
j=0;
posOpt[j]=s[i];
j++;
posOpt[j]='\0';
pushOperator(posOpt);
}
else
{
do
{
strcpy(optr,popOperator());
strcpy(op2,popOperand());
strcpy(op1,popOperand());
strcpy(pos,op1);
strcat(pos,op2);
strcat(pos,optr);
pushOperand(pos);
}
while (ICP(s[i])<= ICP(stOptr[Optrtop][0]) );
j=0;
posOpt[j]=s[i];
j++;
posOpt[j]='\0';
pushOperator(posOpt);
}
i++;
/*dispOperatorstack();
dispOperandstack();
getch(); */
}
while ( strcmp(stOptr[Optrtop],"!")!=0 )
{
strcpy(optr,popOperator());
strcpy(op2, popOperand());
strcpy(op1, popOperand());
strcpy(pos,op1);
strcat(pos,op2);
strcat(pos,optr);
pushOperand(pos);
}
/* dispOperatorstack();
dispOperandstack();*/
strcpy(pos,popOperand());
return pos;
}
int PostEval(char s[25])
{
char temp[25];
int i,val=0,ch1,ch2,j=0;
i=0; top=-1;
while (s[i]!='\0')
{
/*if operand is countered print it*/
if ( (s[i]>=48 && s[i]<=57) )
{
j=0;
temp[j]=s[i];
j++;
temp[j]='\0';
push(atoi(temp));
}
else
{
ch1=pop();
ch2=pop();
switch(s[i])
{
case '+' :{
val=ch2+ch1;
break;
}
case '-' :{
val=ch2-ch1;
break;
}
case '*' :{
val=ch2*ch1;
break;
}
case '/' :{
val=ch2/ch1;
break;
}
}
push(val);
}
i++;
}
val=pop();
return val;
}
int PreEval(char s[25])
{
char temp[25];
int i,val=0,ch1,ch2,j=0;
i=0; top=-1;
while (s[i]!='\0')
{
/*if operand is countered print it*/
if ( (s[i]>=48 && s[i]<=57) )
{
j=0;
temp[j]=s[i];
j++;
temp[j]='\0';
push(atoi(temp));
}
else
{
ch2=pop();
ch1=pop();
switch(s[i])
{
case '+' :{
val=ch2+ch1;
break;
}
case '-' :{
val=ch2-ch1;
break;
}
case '*' :{
val=ch2*ch1;
break;
}
case '/' :{
val=ch2/ch1;
break;
}
}
push(val);
}
i++;
}
val=pop();
return val;
}
char* PosttoInfix(char s[25])
{
char ifx[25],ch1[25],ch2[25],ch3[25];
int i,j;
i=0; j=0; Opertop=-1;
while (s[i]!='\0')
{
/*if operand is countered print it */
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) || (s[i]>=48 && s[i]<=57) )
{
j=0;
ifx[j]=s[i];
j++;
ifx[j]='\0';
pushOperand(ifx);
}
else
{
strcpy(ch1,popOperand());
strcpy(ch2,popOperand());
j=0;
ch3[j]=s[i];
j++;
ch3[j]='\0';
strcpy(ifx,"(");
strcat(ifx,ch2);
strcat(ifx,ch3);
strcat(ifx,ch1);
strcat(ifx,")");
pushOperand(ifx);
}
i++;
}
return popOperand();
}
char* PretoInfix(char s[25])
{
char ifx[25],ch1[25],ch2[25],ch3[25];
int i,j;
i=0; j=0; Opertop=-1;
while (s[i]!='\0')
{
/*if operand is countered print it */
/*printf("s=%c\n",s[i]);*/
if ( (s[i]>=97 && s[i]<=122) || (s[i]>=65 && s[i]<=90) || (s[i]>=48 && s[i]<=57) )
{
j=0;
ifx[j]=s[i];
j++;
ifx[j]='\0';
pushOperand(ifx);
}
else
{
strcpy(ch1,popOperand());
strcpy(ch2,popOperand());
j=0;
ch3[j]=s[i];
j++;
ch3[j]='\0';
strcpy(ifx,ch1);
strcat(ifx,ch3);
strcat(ifx,ch2);
pushOperand(ifx);
}
i++;
}
return popOperand();
}
void main()
{
char s[25],pre[25],pos[25],PtoInf[25],PrtoInf[25],prerev[25];
int val,choice;
while(1)
{
clrscr();
printf("1. Infix to Postfix Expression\n");
printf("2. Infix to Prefix Expression \n");
printf("3. Postfix to Infix Expression \n");
printf("4. Prefix to Infix Expression \n");
printf("5. Evaluation of Postfix Expression \n");
printf("6. Evaluation of Prefix Expression \n");
printf("7. Exit\n");
printf("Enter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: {
printf("Enter Infix Expression\n");
scanf("%s",s);
strcpy(pre,Postfix(s));
printf("Postfix Expression = %s\n", pre);
break;
}
case 2: {
printf("Enter Infix Expression\n");
scanf("%s",s);
strcpy(pre,Prefix(s));
printf("Prefix Expression = %s\n", pre);
break;
}
case 3: {
printf("Enter Postfix Expression\n");
scanf("%s",s);
strcpy(PtoInf,PosttoInfix(s));
printf("Postfix to Infix =%s\n", PtoInf);
break;
}
case 4: {
printf("Enter prefix Expression\n");
scanf("%s",s);
strcpy(prerev, strrev(s));
strcpy(PrtoInf,PretoInfix(prerev));
printf("Prefix to Infix =%s\n", PrtoInf);
break;
}
case 5: {
printf("Enter Postfix Expression\n");
scanf("%s",s);
val= PostEval(s);
printf("Value of Postfix Expression=%d\n", val);
break;
}
case 6: {
printf("Enter Prefix Expression\n");
scanf("%s",s);
strcpy(prerev,strrev(s));
val=PreEval(prerev);
printf("Value of Prefix Expression = %d\n", val);
break;
}
case 7: {
exit(0);
}
}
getch();
}
}
No comments:
Post a Comment