TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
paranthesis_matching.cpp
Go to the documentation of this file.
1
8#include <iostream>
9#ifdef _MSC_VER
10#include <string> // Visual Studio C requires this include
11#else
12#include <cstring>
13#endif
14
16#define MAX 100
17
20char stack[MAX];
21
23int stack_idx = -1;
24
26void push(char ch) { stack[++stack_idx] = ch; }
27
29char pop() { return stack[stack_idx--]; }
30
32
36char opening(char ch) {
37 switch (ch) {
38 case '}':
39 return '{';
40 case ']':
41 return '[';
42 case ')':
43 return '(';
44 case '>':
45 return '<';
46 }
47 return '\0';
48}
49
50int main() {
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
int main()
Main function.
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...
#define MAX
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