# Algorithms_in_C++: others/paranthesis_matching.cpp File Reference

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 More...
char | pop ()
| pop a byte out of stack variable More...

## 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 |  | ) |

{
std::string exp;
int valid = 1, i = 0;
std::cout << "Enter The Expression : ";
std::cin >> exp;
while (valid == 1 && i < exp.length()) {
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') {
push(exp[i]);
} else if (stack_idx >= 0 && stack[stack_idx] == opening(exp[i])) {
pop();
} else {
valid = 0;
}
i++;
}
// makes sure the stack is empty after processsing (above)
if (valid == 1 && stack_idx == -1) {
std::cout << "\nCorrect Expression";
} else {
std::cout << "\nWrong Expression";
}
return 0;
}
std::cin
std::cout
std::string
stack
Definition: stack.h:26
std::exp
T exp(T... args)
pop
char pop()
pop a byte out of stack variable
Definition: paranthesis_matching.cpp:29
push
void push(char ch)
push byte to stack variable
Definition: paranthesis_matching.cpp:26
opening
char opening(char ch)
Definition: paranthesis_matching.cpp:36
stack_idx
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] | ch | closed paranthesis character

{
switch (ch) {
case '}':
return '{';
case ']':
return '[';
case ')':
return '(';
case '>':
return '<';
}
return '\0';
}

## pop()

char pop | ( |  | ) |

pop a byte out of stack variable

{ return stack[stack_idx--]; }

## push()

void push | ( | char | ch | ) |

push byte to stack variable

{ stack[++stack_idx] = ch; }

## Variable Documentation

## stack

char stack[MAX]

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