Perform paranthesis matching.
More...
#include <iostream>
#include <cstring>
|
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
|
|
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
◆ MAX
◆ main()
50 {
52 int valid = 1, i = 0;
55
56 while (valid == 1 && i <
exp.length()) {
57 if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') {
61 } else {
62 valid = 0;
63 }
64 i++;
65 }
66
67
70 } else {
72 }
73
74 return 0;
75}
for std::invalid_argument
Definition stack.hpp:19
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()
return opening paranthesis corresponding to the close paranthesis
- Parameters
-
[in] | ch | closed 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()
pop a byte out of stack variable
◆ push()
push byte to stack variable
◆ stack
-----------— stack -----------— global stack