Skip to content

Commit cc9e8d0

Browse files
author
Christian Bender
committed
improved the code
1 parent 0256f9e commit cc9e8d0

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

Others/Paranthesis Matching.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include<iostream>
22
#include<stdlib.h>
3-
#include<string.h>
3+
#include<string>
44
#include<stdio.h>
55
using namespace std;
66

7-
char stack[100];
7+
const int MAX = 100;
8+
9+
// -------------- stack --------------
10+
11+
char stack[MAX];
812
int top=0;
913

1014
void push(char ch)
@@ -17,41 +21,26 @@ char pop()
1721
return stack[--top];
1822
}
1923

20-
bool check(char x, char y)
21-
{
22-
if ((x=='(' && y==')') || (x=='{' && y=='}') || (x=='[' && y==']') || (x=='<' && y=='>'))
23-
{
24-
return true;
25-
}
26-
else
27-
{
28-
return false;
29-
}
30-
}
31-
32-
24+
// -------------- end stack -----------
3325

3426
int main()
3527
{
36-
char exp[100];
28+
string exp;
3729
cout<<"Enter The Expression : ";
38-
gets(exp);
39-
for (int i = 0; i < strlen(exp); i++)
30+
cin >> exp;
31+
for (int i = 0; i < exp.length(); i++)
4032
{
4133
if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<')
4234
{
4335
push(exp[i]);
4436
}
4537
else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>')
4638
{
47-
if(!check(pop(), exp[i]))
48-
{
49-
cout<<"\nWrong Expression";
50-
exit(0);
51-
}
39+
pop();
5240
}
5341
}
5442

43+
// makes sure the stack is empty after processsing (above)
5544
if(top==0)
5645
{
5746
cout<<"Correct Expression";
@@ -60,6 +49,6 @@ int main()
6049
{
6150
cout<<"\nWrong Expression";
6251
}
63-
52+
6453
return 0;
6554
}

0 commit comments

Comments
 (0)