Advertisement
gamesrsly

CheckCorrectBracketsUsingStack

Oct 8th, 2019
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main() {
  9.     // Variables:
  10.     // initializing stack of chars
  11.     std::stack<char> stackChar;
  12.    
  13.     // string for the input
  14.     std::string input = "";
  15.    
  16.     // temp char for comparison
  17.     char temp = ' ';
  18.    
  19.     // creating bool variables to validate different scenarios and use in the output as control
  20.     bool validate = true;
  21.  
  22.     // bool variable if any bracket is open
  23.     bool parOpen = false;
  24.     bool bracketsOpen = false;
  25.     bool bracesOpen = false;
  26.    
  27.     // Input:
  28.     // get input
  29.     getline(cin, input);
  30.    
  31.     // Main Logic:
  32.     // loop through the input string
  33.     for(int i = 0; i < input.size(); i++) {
  34.         // check if input char is opening brace and that no previous brackets or parentheses have been opened
  35.         if(input[i] == '{' && !bracketsOpen && !parOpen)
  36.         {
  37.             // bracesOpen equals true until closing brace is found
  38.             bracesOpen = true;
  39.            
  40.             // push the opening brace in the stack if everything is fine
  41.             stackChar.push(input[i]);
  42.         }
  43.         // check if input char is opening brace and if previous brackets or parentheses have been opened
  44.         if(input[i] == '{' && (bracketsOpen || parOpen)) {
  45.             // set validate to false if brackets or parentheses have been opened
  46.             validate = false;
  47.             // break the for-loop and go to the output
  48.             break;
  49.         }
  50.         // check if input char is opening bracket and that no previous parentheses have been opened
  51.         if(input[i] == '[' && !parOpen) {
  52.             // bracketsOpen equals true until closing bracket is found
  53.             bracketsOpen = true;
  54.             // push the opening bracket in the stack if everything is fine
  55.             stackChar.push(input[i]);
  56.         }
  57.         // check if input char is bracket and if previous parentheses have been opened
  58.         if(input[i] == '[' && parOpen) {
  59.             // set validate to false if parentheses have been opened
  60.             validate = false;
  61.             // break the for-loop and go to the output
  62.             break;
  63.         }
  64.         // check if input char is parentheses
  65.         if(input[i] == '(') {
  66.             // parOpen equals true until closing parentheses is found
  67.             parOpen = true;
  68.             // push the opening parentheses in the stack(here usually everything should be fine)
  69.             stackChar.push(input[i]);
  70.         }
  71.         // check if input is closing parentheses, closing bracket or closing brace
  72.         if(input[i] == ')' || input[i] == ']' || input[i] == '}') {
  73.             // check if the stack is empty at this point
  74.             if(stackChar.empty()) {
  75.                 // empty means immediate termination, because there won't be any suitable
  76.                 // input to compare (opening brackets, parentheses or braces)
  77.                 validate = false;
  78.                 break;
  79.                 // if the stack is not empty
  80.             } else {
  81.                 // set validate to true(just for testing)
  82.                 validate = true;
  83.                 // store the top element of the stack in the char temp
  84.                 temp = stackChar.top();
  85.                 // compare input if : closing parentheses vs the top of the stack
  86.                 if(input[i] == ')' && (temp == '{' || temp == '[')) {
  87.                     // false if temp is equal to opening braces or brackets
  88.                     validate = false;
  89.                 }
  90.                 // compare input if : closing brackets vs the top of the stack
  91.                 if(input[i] == ']' && (temp == '(' || temp == '{')) {
  92.                     // false if temp is equal to opening parentheses or braces
  93.                     validate = false;
  94.                 }
  95.                 // compare input if : closing braces vs the top of the stack
  96.                 if(input[i] == '}' && (temp == '(' || temp == '[')) {
  97.                     validate = false;
  98.                 }
  99.                 // if input is closing parentheses and matches the top of the stack, stored in temp
  100.                 if(input[i] == ')' && (temp == '(')) {
  101.                     // validate is true, because of match
  102.                     validate = true;
  103.                     // closing parentheses found, setting parOpen to false
  104.                     parOpen = false;
  105.                     // poping(removing) the top elemement from the stack
  106.                     stackChar.pop();
  107.                 }
  108.                 // if input is closing brackets and matches the top of the stack, stored in temp
  109.                 if(input[i] == ']' && temp == '[') {
  110.                     // validate is true, because of match
  111.                     validate = true;
  112.                     // closing brackets found, setting bracketsOpen to false
  113.                     bracketsOpen = false;
  114.                     // poping(removing) the top elemement from the stack
  115.                     stackChar.pop();
  116.                 }
  117.                 // if input is closing braces and matches the top of the stack, stored in temp
  118.                 if(input[i] == '}' && temp == '{') {
  119.                     // validate is true, because of match
  120.                     validate = true;
  121.                     // closing braces found, setting bracesOpen to false
  122.                     bracesOpen = false;
  123.                     // poping(removing) the top elemement from the stack
  124.                     stackChar.pop();
  125.                 }
  126.             }
  127.         }
  128.     }
  129.    
  130.     // Output:
  131.     // If the stack is not empty or the bool value for checking the different cases is false
  132.     if(!stackChar.empty() || validate == false) {
  133.         // output "invalid"
  134.         cout << "invalid" << endl;
  135.     }
  136.     // or if the stack is empty and the bool value for checking the different cases is true
  137.     else if(stackChar.empty() && validate == true) {
  138.         // output "valid"
  139.         cout << "valid" << endl;
  140.     }
  141.    
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement