TheAlgorithms/C++ 1.0.0
All the 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:

Go to the source code of this file.

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

Definition in file paranthesis_matching.cpp.

Macro Definition Documentation

◆ MAX

#define MAX   100

check number

Definition at line 16 of file paranthesis_matching.cpp.

Function Documentation

◆ main()

int main ( void )

Definition at line 50 of file paranthesis_matching.cpp.

50 {
51 std::string exp;
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
uint256_t exp(uint256_t number, uint256_t power, const uint256_t &mod)
This function calculates number raised to exponent power under modulo mod using Modular Exponentiatio...
char pop()
pop a byte out of stack variable
void push(char ch)
push byte to stack variable
char opening(char ch)
int stack_idx
pointer to track stack index

◆ opening()

char opening ( char ch)

return opening paranthesis corresponding to the close paranthesis

Parameters
[in]chclosed paranthesis character

Definition at line 36 of file paranthesis_matching.cpp.

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

Definition at line 29 of file paranthesis_matching.cpp.

29{ return stack[stack_idx--]; }

◆ push()

void push ( char ch)

push byte to stack variable

Definition at line 26 of file paranthesis_matching.cpp.

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

Variable Documentation

◆ stack

char stack[MAX]

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

Definition at line 20 of file paranthesis_matching.cpp.

◆ stack_idx

int stack_idx = -1

pointer to track stack index

Definition at line 23 of file paranthesis_matching.cpp.