Skip to content

Instantly share code, notes, and snippets.

@Pr3d4dor
Created July 7, 2015 02:26
Show Gist options
  • Save Pr3d4dor/205ff62bcba6f683d7f7 to your computer and use it in GitHub Desktop.
Save Pr3d4dor/205ff62bcba6f683d7f7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
//Stack.h is a header file that contains a abstract data type stack created to represent a stack, as the own name says
//Stack.h is here among the others gists
#include "Stack.h"
int main(){
int i;
Stack p;
char ex[MAX],aux=0,control=0;
iniStack(&p);
printf ("Enter the expression to be evaluated:\n");
scanf (" %sMAX[^\n]", ex);
for (i=0;ex[i]!='\0';i++){
if ((ex[i]=='(') || (ex[i]=='{') || (ex[i]=='[')){
push(&p,ex[i]);
}
}
for (i=0;ex[i]!='\0';i++){
if (ex[i]==')'){
aux=pop(&p);
if (aux=='(')
control=1;
else{
control=2;
break;
}
}
if (ex[i]=='}'){
aux=pop(&p);
if (aux=='{')
control=1;
else{
control=2;
break;
}
}
if (ex[i]==']'){
aux=pop(&p);
if (aux=='[')
control=1;
else{
control=2;
break;
}
}
if (emptyStack(&p)!=1)
control=2;
break;
}
if (control==1)
printf ("Valid expression!\n");
if (control==2)
printf("Invalid expression!\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment