0% found this document useful (0 votes)
45 views4 pages

220010059

This C program converts an infix expression to postfix notation using a stack. It takes an infix expression as input, checks if it is valid and has balanced parentheses. If valid, it iterates through the expression, pushes operands and parentheses to the stack and pops to append operators. It outputs the postfix expression and stack operations to a file. The program uses functions like isOperator(), precedence(), push(), pop() to evaluate the expression and convert to postfix.

Uploaded by

Abhinay Upamaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

220010059

This C program converts an infix expression to postfix notation using a stack. It takes an infix expression as input, checks if it is valid and has balanced parentheses. If valid, it iterates through the expression, pushes operands and parentheses to the stack and pops to append operators. It outputs the postfix expression and stack operations to a file. The program uses functions like isOperator(), precedence(), push(), pop() to evaluate the expression and convert to postfix.

Uploaded by

Abhinay Upamaka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//Name: Upamaka S V B S B Abhinay

//Credits: I watched few youtube videos by code with harry and GATE Applied
Courses and got inspired by them. Also I learned formation of stack from TA in
DSA lab.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int stack[1000];
char postfix[200];
int k=0;
int stacksize=1000;
int top = -1;

void append(char i){


postfix[k] = i;

int isoperator(char n){


if(n == '+'||n == '-'||n == '*'||n == '/'||n == '%'||n == '^')
return 0;
else
return 1;
}

void push(char n)
{
if (top == stacksize-1){
printf("Overflow");
}
else{
top++;
stack[top] = n;
}
}
char pop()
{
char x;
if (top == -1){
printf("Underflow");
return '&';
}else{
x = stack[top];
top--;
return x;
}
}

/*void append(char x){

postfix[i] = x;
i++;
}*/

int precedency(char x)
{
if(x == '^')
return 3;
else if(x == '*' || x == '/' || x=='%')
return 2;
else if(x == '+' || x == '-')
return 1;
else if(x == '(' || x == ')')
return 4;
}

int Isempty()
{
if(top == -1)
return 1;
else
return 0;
}

int Isvalidpar(char* n)
{
for(int i=0; i<strlen(n); i++)
{
if(n[i] == '('){

push(n[i]);
}
else if(n[i] == ')'){
if (stack[top]=='(')
{pop();}
else{return 1;}
}
}
if (top == -1){
return 0;
}
else{
//printf("Invalid paranthesis");
return 1;
}
}

int Isvalid(char* n)
{
int c=0;

for(int i=0;i<strlen(n)-1;i++)
{
if (n[i] == '(' || n[i] == ')')
continue;
else if(isoperator(n[i]) == 0 && isoperator(n[i+1]) == 0)
return 1;

else if(isalnum(n[i]) != 0 && isalnum(n[i+1])!=0)


return 1;
else
continue;
}
return 0;
}

int main(int argc, char** argv){


if (argc<2)
{
printf("Invalid filename");
return 0;
}
char infix[200], *d, x;
int i=0,j=0;
FILE *ptr, *out;

ptr = fopen(argv[1], "r");


if(ptr ==NULL)
{
printf("File not found");
return 0;
}

out = fopen("output.txt", "w");


if (out == NULL)
{
printf("Unable to open output file");
return 0;
}
char y;
while((fscanf(ptr, "%c", &y)) == 1){
infix[i] = y;
//printf("%c",infix[i]);
i++;
}
d = infix;

if(Isvalid(infix) == 0 && Isvalidpar(infix) == 0){


while(*d != '\0')
{
if (isalnum(*d))
{
//printf("%c",postfix[k]);
append(*d);
//postfix[k] = *d;
//printf("%c%d\n",postfix[k],k);

k++;

}
else if(*d == '('){
push(*d);
fprintf(out, "%c %s", *d, "is pushed into stack.\n");

}
else if(*d == ')')
{

while(stack[top] != '(')
{
//printf("%c",x);
x = pop();
fprintf(out, "%c %s", x, "is popped from stack.\n");
append(x);
//postfix[k] = x;
k++;
}
pop();
}
else if(*d == '+' ||*d == '-' ||*d == '*' ||*d == '/' ||*d == '%' ||*d
== '^' )
{
while(precedency(stack[top]) >= precedency(*d) && stack[top]!
='(')
{

x = pop();

fprintf(out, "%c %s", x, "is popped from the stack.\n");


append(x);
//postfix[k] = x;
k++;
}

push(*d);
// printf("%c",stack[top]);
fprintf(out, "%c %s", *d, "is pushed into stack.\n");
}
d++;
}
while(top >= 0){
//fprintf(out, "%c", pop());
x = pop();
fprintf(out, "%c %s", x, "is popped from stack.\n");

append(x);
//postfix[k] = x;
k++;
}
append('\0');

fprintf(out, "%s", "Postfix expression is: ");


while(postfix[j] != '\0')
{
fprintf(out, "%c",postfix[j]);
j++;
}

else
{
if(Isvalid(infix) != 0){
fprintf(out, "%s", "Invalid expression");
}
if(Isvalidpar(infix) != 0){
fprintf(out, "%s", "Unequal Paranthesis");
}
return 0;
}

You might also like