#include "iostream.h"
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
template
class stack
{
private:
T *arr;
int TOP,MAXSTK;
public:
stack() //Default Constructor
{
MAXSTK = 31;
arr = new T[MAXSTK];
TOP = 0;
}
stack(int n) //Parameterized Constructor
{
MAXSTK = n + 1;
arr = new T[MAXSTK];
TOP = 0;
}
friend void push(stack &,T);
friend T pop(stack &);
friend int empty(stack);
~stack()
{
delete arr;
}
};
template
void push(stack
{
if(s.TOP != s.MAXSTK)
{
s.TOP += 1;
s.arr[s.TOP] = item;
}
else
cout << "\nData Overflow.";
}
template
T pop(stack
{
if(s.TOP != 0)
{
T temp = s.arr[s.TOP];
s.TOP -= 1;
return temp;
}
else
cout << "\nData Underflow.";
}
template
int empty(stack
{
if(s.TOP == 0)
return 1;
else
return 0;
}
int main()
{
clrscr();
const int True = 1,False = 0;
int valid = True,i = 0;
stack
char str[100];
cout << "Enter an Expression\n";
while(str[i-1] != '\n')
{
str[i] = getchar();
if(str[i] == '(' || str[i] == '{' || str[i] == '[') //If (,{ or [ is encountered
push(s,str[i]); //Push to Stack
if(str[i] == ')' || str[i] == '}' || str[i] == ']') //If ),} or ] is encountered
{
if(i == 0)
valid = False;
else
{
char ch = pop(s); //Pop Stack
if(str[i] == ')' && ch != '(')
valid = False;
if(str[i] == '}' && ch != '{')
valid = False;
if(str[i] == ']' && ch != '[')
valid = False;
}
}
i++; //Update i
}
if(!empty(s)) //If the Stack is not Empty at Last
valid = False; //Set valid = false
if(valid)
cout << "\nCorrect String Format";
else
cout << "\nIncorrect String Format";
getch();
return 0;
}
No comments:
Post a Comment