Tuesday, 25 June 2013

source code: Polish notation validity check ( parenthesis check ) using C++ ( stack) [ the most simplest ]

#include<iostream>
using namespace std;

char a[100], i=0;


void push(char e)

{
a[++i]=e;
}

void pop()

{
i--;
}

int main()

{ cout<<"assign the equation: ";
char b[100];
gets(b);


for(int x=0; b[x]; x++) 
{
if(b[x]=='{' || b[x]=='(' || b[x]=='[') {push(b[x]);}

else if(b[x]==')' && a[i]=='(') {pop();}


else if(b[x]=='}' && a[i]=='{') {pop();}


else if(b[x]==']' && a[i]=='[') {pop();}

}


if(i==0) {cout<<"\nah, good this a Valid one !!\n\n";}


else {cout<<"\nahah no luck kiddo! this aint a Valid one\n\n";}
}

No comments:

Post a Comment