Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <string>
- using namespace std;
- int main() {
- // Variables:
- // initializing stack of chars
- std::stack<char> stackChar;
- // string for the input
- std::string input = "";
- // temp char for comparison
- char temp = ' ';
- // creating bool variables to validate different scenarios and use in the output as control
- bool validate = true;
- // bool variable if any bracket is open
- bool parOpen = false;
- bool bracketsOpen = false;
- bool bracesOpen = false;
- // Input:
- // get input
- getline(cin, input);
- // Main Logic:
- // loop through the input string
- for(int i = 0; i < input.size(); i++) {
- // check if input char is opening brace and that no previous brackets or parentheses have been opened
- if(input[i] == '{' && !bracketsOpen && !parOpen)
- {
- // bracesOpen equals true until closing brace is found
- bracesOpen = true;
- // push the opening brace in the stack if everything is fine
- stackChar.push(input[i]);
- }
- // check if input char is opening brace and if previous brackets or parentheses have been opened
- if(input[i] == '{' && (bracketsOpen || parOpen)) {
- // set validate to false if brackets or parentheses have been opened
- validate = false;
- // break the for-loop and go to the output
- break;
- }
- // check if input char is opening bracket and that no previous parentheses have been opened
- if(input[i] == '[' && !parOpen) {
- // bracketsOpen equals true until closing bracket is found
- bracketsOpen = true;
- // push the opening bracket in the stack if everything is fine
- stackChar.push(input[i]);
- }
- // check if input char is bracket and if previous parentheses have been opened
- if(input[i] == '[' && parOpen) {
- // set validate to false if parentheses have been opened
- validate = false;
- // break the for-loop and go to the output
- break;
- }
- // check if input char is parentheses
- if(input[i] == '(') {
- // parOpen equals true until closing parentheses is found
- parOpen = true;
- // push the opening parentheses in the stack(here usually everything should be fine)
- stackChar.push(input[i]);
- }
- // check if input is closing parentheses, closing bracket or closing brace
- if(input[i] == ')' || input[i] == ']' || input[i] == '}') {
- // check if the stack is empty at this point
- if(stackChar.empty()) {
- // empty means immediate termination, because there won't be any suitable
- // input to compare (opening brackets, parentheses or braces)
- validate = false;
- break;
- // if the stack is not empty
- } else {
- // set validate to true(just for testing)
- validate = true;
- // store the top element of the stack in the char temp
- temp = stackChar.top();
- // compare input if : closing parentheses vs the top of the stack
- if(input[i] == ')' && (temp == '{' || temp == '[')) {
- // false if temp is equal to opening braces or brackets
- validate = false;
- }
- // compare input if : closing brackets vs the top of the stack
- if(input[i] == ']' && (temp == '(' || temp == '{')) {
- // false if temp is equal to opening parentheses or braces
- validate = false;
- }
- // compare input if : closing braces vs the top of the stack
- if(input[i] == '}' && (temp == '(' || temp == '[')) {
- validate = false;
- }
- // if input is closing parentheses and matches the top of the stack, stored in temp
- if(input[i] == ')' && (temp == '(')) {
- // validate is true, because of match
- validate = true;
- // closing parentheses found, setting parOpen to false
- parOpen = false;
- // poping(removing) the top elemement from the stack
- stackChar.pop();
- }
- // if input is closing brackets and matches the top of the stack, stored in temp
- if(input[i] == ']' && temp == '[') {
- // validate is true, because of match
- validate = true;
- // closing brackets found, setting bracketsOpen to false
- bracketsOpen = false;
- // poping(removing) the top elemement from the stack
- stackChar.pop();
- }
- // if input is closing braces and matches the top of the stack, stored in temp
- if(input[i] == '}' && temp == '{') {
- // validate is true, because of match
- validate = true;
- // closing braces found, setting bracesOpen to false
- bracesOpen = false;
- // poping(removing) the top elemement from the stack
- stackChar.pop();
- }
- }
- }
- }
- // Output:
- // If the stack is not empty or the bool value for checking the different cases is false
- if(!stackChar.empty() || validate == false) {
- // output "invalid"
- cout << "invalid" << endl;
- }
- // or if the stack is empty and the bool value for checking the different cases is true
- else if(stackChar.empty() && validate == true) {
- // output "valid"
- cout << "valid" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement