Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
paranthesis_matching.cpp File Reference

Perform paranthesis matching. More...

#include <iostream>
#include <cstring>
Include dependency graph for paranthesis_matching.cpp:

Macros

#define MAX   100
 

Functions

char opening (char ch)
 
int main ()
 
char stack [MAX]
 
int stack_idx = -1
 pointer to track stack index
 
void push (char ch)
 push byte to stack variable
 
char pop ()
 pop a byte out of stack variable
 

Detailed Description

Perform paranthesis matching.

Note
Do not know the application of this, however.
Implementation is C-type and does not utilize the C++ constructs
Todo
implement as a C++ class

Macro Definition Documentation

◆ MAX

#define MAX   100

check number

Function Documentation

◆ main()

int main ( void )
50 {
52 int valid = 1, i = 0;
53 std::cout << "Enter The Expression : ";
54 std::cin >> exp;
55
56 while (valid == 1 && i < exp.length()) {
57 if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') {
58 push(exp[i]);
59 } else if (stack_idx >= 0 && stack[stack_idx] == opening(exp[i])) {
60 pop();
61 } else {
62 valid = 0;
63 }
64 i++;
65 }
66
67 // makes sure the stack is empty after processsing (above)
68 if (valid == 1 && stack_idx == -1) {
69 std::cout << "\nCorrect Expression";
70 } else {
71 std::cout << "\nWrong Expression";
72 }
73
74 return 0;
75}
for std::invalid_argument
Definition stack.hpp:19
T exp(T... args)
char pop()
pop a byte out of stack variable
Definition paranthesis_matching.cpp:29
void push(char ch)
push byte to stack variable
Definition paranthesis_matching.cpp:26
char opening(char ch)
Definition paranthesis_matching.cpp:36
int stack_idx
pointer to track stack index
Definition paranthesis_matching.cpp:23

◆ opening()

char opening ( char ch)

return opening paranthesis corresponding to the close paranthesis

Parameters
[in]chclosed paranthesis character
36 {
37 switch (ch) {
38 case '}':
39 return '{';
40 case ']':
41 return '[';
42 case ')':
43 return '(';
44 case '>':
45 return '<';
46 }
47 return '\0';
48}

◆ pop()

char pop ( )

pop a byte out of stack variable

29{ return stack[stack_idx--]; }

◆ push()

void push ( char ch)

push byte to stack variable

26{ stack[++stack_idx] = ch; }

Variable Documentation

◆ stack

char stack[MAX]

-----------— stack -----------— global stack