Published by
Jan 7, 2012 (last update: Jan 7, 2012)

paranthesis checker

Score: 3.7/5 (43 votes)
*****
A simple program which shows the use of the linked lists as a stack with LIFO access.
This program is based on the function iterator. The program will ask the user to enter a string. This function will take that string as an argument. The function will then, parse through the whole string. For each iteration, there will be three checks.

1. If there is a mismatch of brackets e.g. a '{' against a ']'
2. If there are only opening brackets
3. If there are only closing brackets

The flow of the process is that the program will read the string input character by character. If the character at the current iteration is an opening bracket i.e. '{','[' or '(', it will be pushed into the stack object list. Now, if the program encounters a closing bracket, it will call the pop function on list and the popped object will be checked. If the popped object is '\a', it is a flag which indicates that the stack is empty. This means that the string contains a closing bracket without a counterpart. This is a inconsistency so it will return false. Else, the popped object will be compared with the character at the current iteration for checking their match. If there is a match, the loop will continue, else it would return false. After the program has parsed the whole string, it will check that is the stack empty. At this point, if the stack is not empty, it would mean that there is some open bracket whose counter part was not there. So this will return false. Finally, if the above three checks are successful, the function will return true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <string>
#include <Windows.h>

struct node
{
 char c;
 node* next;
 node()
 {
  next = nullptr;
 }
};

class stack
{
 private:
	     node* head;
 public:
	     stack()
			: head(nullptr)
		 {}

	bool is_empty()
		 {
		  if(!this->head)
		    return true;
		  else
		    return false;
		 }
		 
		 void push(char c)
		 {
                  node* p = new node;
                  p->c = c;
	      if(this->is_empty())
	       this->head = p;
	      else
	      {
	       p->next = this->head;
	       this->head = p;
              }
		 }

		 char pop()
		 {
		  if(this->is_empty())
           return '\a';
          else
          {
           node* tmp = this->head;
           this->head = this->head->next;
		   char c = tmp->c;
		   delete tmp;
		   return c;
          }
		 }
};

//////////////////////////////////////////////////////////////////////////

inline bool iterator(std :: string& exp)
{
 stack list;
 for(int i=0;i<exp.size();i++)
 {
  if(exp[i] == '[' ||
     exp[i] == '{' ||
     exp[i] == '(')
   list.push(exp[i]);//when an opening bracket is found, push it in the stack
  if (exp[i] == ']' ||
	  exp[i] == '}' ||
	  exp[i] == ')')
  {
   char c = list.pop();
   if (c == '\a')
	return false; //if the expression has only closing brackets  
   else
   {
	if(c == '[' && exp[i] == ']' ||
	   c == '{' && exp[i] == '}' ||
	   c == '(' && exp[i] == ')');
	else
     return false; //if mismatch
   }
  }
 }
 if (!list.is_empty())
  return false; //if the expression has only opening brackets
 else
  return true;
}

int main()
{
 SetConsoleTitle("PARANTHESIS CHECKER");
 std :: string exp;
 std :: cout<<"Enter the expression: ";
 getline(std :: cin,
	     exp);
 if(iterator(exp))
   std :: cout<<"The expression "<<exp<<" is consistent in brackets.\n\n";
 else
   std :: cout<<"The expression "<<exp<<" is inconsistent in brackets.\n\n";
 system("pause");
 return 0;
}


Output:

Enter the expression: 3x+432[
The expression 3x+432[ is inconsistent in brackets.



Enter the expression: 3x+(4/7+[6/a])
The expression 3x+(4/7+[6/a]) is consistent in brackets.