DOWNLOAD FULL PROGRAME
CODING:
#include<stdio.h>
#include<conio.h>
#define max 50
char st[max];
int top=-1;
void push(char st[],char);
char pop(char st[]);
void intopostex(char source[],char target[]);
int getpriority(char);
void main()
{
char infix[50],postfix[50];
clrscr();
printf("\nENTER INFIX EXPRESSION:\n");
fflush(stdin);
gets(infix);
strcpy(postfix,"");
intopostex(infix,postfix);
printf("\nTHE POSTFIX EXPRESSION:\n");
puts(postfix);
getch();
}
void intopostex(char sourse[],char target[])
{
int i=0,j=0;
char temp;
strcpy(target,"");
while(sourse[i]!='\0')
{
if(sourse[i]=='(')
{
push(st,sourse[i]);
i++;
}
else if(sourse[i]==')')
{
while((top!=-1) && (st[top]!='('))
{
target[j]=pop(st);
j++;
}
if(top==-1)
{
printf("\nINCORRECT EXPRESSION..");
exit(1);
}
temp=pop(st);
i++;
}
else if(isdigit(sourse[i]) || isalpha(sourse[i]))
{
target[j]=sourse[i];
j++;
i++;
}
else if(sourse[i]=='+' || sourse[i]=='-' || sourse[i]=='*' || sourse[i]=='/' || sourse[i]=='%')
{
while((top!=-1) && (st[top]!='(') && (getpriority(st[top])) > (getpriority(sourse[i])))
{
target[j]=pop(st);
j++;
}
push(st,sourse[i]);
i++;
}
else
{
printf("\nINCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=-1) && (st[top]!='('))
{
target[j]=pop(st);
j++;
}
target[j]='\0';
}
int getpriority(char sign)
{
if(sign=='/' || sign=='*' || sign=='%')
return 1;
else if(sign=='+' || sign=='-')
return 0;
}
void push(char st[],char num)
{
if(top==max-1)
printf("\nSTACK IS OVERFLOW..");
else
{
top++;
st[top]=num;
}
}
char pop(char st[])
{
char num=' ';
if(top==-1)
printf("\nSTACK IS UNDERFLOW..");
else
{
num=st[top];
top--;
}
return num;
}
//IN THIS PROGRAM TWO WARNINGS,BUT IT NOT AFFECT TO PROGRAM & NOT EFFICIENT.
CODING:
#include<stdio.h>
#include<conio.h>
#define max 50
char st[max];
int top=-1;
void push(char st[],char);
char pop(char st[]);
void intopostex(char source[],char target[]);
int getpriority(char);
void main()
{
char infix[50],postfix[50];
clrscr();
printf("\nENTER INFIX EXPRESSION:\n");
fflush(stdin);
gets(infix);
strcpy(postfix,"");
intopostex(infix,postfix);
printf("\nTHE POSTFIX EXPRESSION:\n");
puts(postfix);
getch();
}
void intopostex(char sourse[],char target[])
{
int i=0,j=0;
char temp;
strcpy(target,"");
while(sourse[i]!='\0')
{
if(sourse[i]=='(')
{
push(st,sourse[i]);
i++;
}
else if(sourse[i]==')')
{
while((top!=-1) && (st[top]!='('))
{
target[j]=pop(st);
j++;
}
if(top==-1)
{
printf("\nINCORRECT EXPRESSION..");
exit(1);
}
temp=pop(st);
i++;
}
else if(isdigit(sourse[i]) || isalpha(sourse[i]))
{
target[j]=sourse[i];
j++;
i++;
}
else if(sourse[i]=='+' || sourse[i]=='-' || sourse[i]=='*' || sourse[i]=='/' || sourse[i]=='%')
{
while((top!=-1) && (st[top]!='(') && (getpriority(st[top])) > (getpriority(sourse[i])))
{
target[j]=pop(st);
j++;
}
push(st,sourse[i]);
i++;
}
else
{
printf("\nINCORRECT ELEMENT IN EXPRESSION");
exit(1);
}
}
while((top!=-1) && (st[top]!='('))
{
target[j]=pop(st);
j++;
}
target[j]='\0';
}
int getpriority(char sign)
{
if(sign=='/' || sign=='*' || sign=='%')
return 1;
else if(sign=='+' || sign=='-')
return 0;
}
void push(char st[],char num)
{
if(top==max-1)
printf("\nSTACK IS OVERFLOW..");
else
{
top++;
st[top]=num;
}
}
char pop(char st[])
{
char num=' ';
if(top==-1)
printf("\nSTACK IS UNDERFLOW..");
else
{
num=st[top];
top--;
}
return num;
}
//IN THIS PROGRAM TWO WARNINGS,BUT IT NOT AFFECT TO PROGRAM & NOT EFFICIENT.
Comments
Post a Comment