From cc9e8d0d17e6e4b84effe8b24a18ea274b0657d5 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Tue, 27 Mar 2018 22:51:40 +0200 Subject: [PATCH] improved the code --- Others/Paranthesis Matching.cpp | 37 ++++++++++++--------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/Others/Paranthesis Matching.cpp b/Others/Paranthesis Matching.cpp index 5d37a161e..0347f38d0 100644 --- a/Others/Paranthesis Matching.cpp +++ b/Others/Paranthesis Matching.cpp @@ -1,10 +1,14 @@ #include #include -#include +#include #include using namespace std; -char stack[100]; +const int MAX = 100; + +// -------------- stack -------------- + +char stack[MAX]; int top=0; void push(char ch) @@ -17,26 +21,14 @@ char pop() return stack[--top]; } -bool check(char x, char y) -{ - if ((x=='(' && y==')') || (x=='{' && y=='}') || (x=='[' && y==']') || (x=='<' && y=='>')) - { - return true; - } - else - { - return false; - } -} - - +// -------------- end stack ----------- int main() { - char exp[100]; + string exp; cout<<"Enter The Expression : "; - gets(exp); - for (int i = 0; i < strlen(exp); i++) + cin >> exp; + for (int i = 0; i < exp.length(); i++) { if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<') { @@ -44,14 +36,11 @@ int main() } else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>') { - if(!check(pop(), exp[i])) - { - cout<<"\nWrong Expression"; - exit(0); - } + pop(); } } + // makes sure the stack is empty after processsing (above) if(top==0) { cout<<"Correct Expression"; @@ -60,6 +49,6 @@ int main() { cout<<"\nWrong Expression"; } - + return 0; }