improved the code

This commit is contained in:
Christian Bender
2018-03-27 22:51:40 +02:00
parent 0256f9edd8
commit cc9e8d0d17

View File

@@ -1,10 +1,14 @@
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<stdio.h>
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;
}