mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 06:26:05 +08:00
improved the code
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user