Stack implementation for checking C ++ brackets

I am trying to implement a stack to check if the file (), [] and {} are balanced. It is assumed that the program should take the file and check if it is balanced and return a logical value. When I run the program, it only works for the last brackets in the file. How can I change the code so that it works in parentheses to the last pair. The input file is just a c file.

Side question:

If I wanted this program to work with an html file, do I just need to change the (), [], {} html tags?

This is the code that I have

#include "stdafx.h" #include <iostream> #include <fstream> #include <stack> #include <string> using namespace std; bool balanced(string A[], int n) { int i; stack <string> a; for (i = 0; i < n; i++) { if (A[i] == "(" || A[i] == "{" || A[i] == "["){ a.push(A[i]); } else if (A[i] == ")" || A[i] == "}" || A[i] == "]") { if (a.empty()) { return false; } else { a.pop(); return true; } } } } int main() { ifstream infile; infile.open ("Text.txt"); string A[1000]; int i = 0; int n = (sizeof(A) / sizeof(*A)); while (!infile.eof()) { getline(infile, A[i], '\n'); i++; } bool out; out = balanced(A, n); if (out == true) { cout << "legal"; } else { cout << "illegal"; } return 0; } 
+4
source share
3 answers

Your solution will only work for one kind of parentheses, even if you correct your mistakes as suggested by @bipll. This is a complete explanation of the solution that will help solve your problem. And here is the C ++ code:

 #include<bits/stdc++.h> using namespace std; // function to check if paranthesis are balanced bool areParanthesisBalanced(char expr[]) { stack<char> s; char a, b, c; // Traversing the Expression for (int i=0; i<strlen(expr); i++) { if (expr[i]=='('||expr[i]=='['||expr[i]=='{') { // Push the element in the stack s.push(expr[i]); } else { switch (expr[i]) { case ')': // Store the top element in a a = s.top(); s.pop(); if (a=='{'||a=='[') cout<<"Not Balancedn"; break; case '}': // Store the top element in b b = s.top(); s.pop(); if (b=='('||b=='[') cout<<"Not Balancedn"; break; case ']': // Store the top element in c c=s.top(); s.pop(); if (c=='('||c=='{') cout<<"Not Balancedn"; break; } } } // Check Empty Stack if (s.empty()) return true; else return false; } // Driver program to test above function int main() { char expr[]="{()}[]"; if(areParanthesisBalanced(expr)) cout<<"Balanced"; else cout<<"Not Balanced"; return 0; } 
0
source

In your file, the last line is the only trailing bracket } . is not it? You do not check characters, you only check strings, and when you look for initial parenthood, you expect this to be the only character on the line. So for your program to successfully count all parentheses, the input file should look like

 int main ( int argc, char **argv ) { 

etc.

You need to not only iterate A into balanced() , but iterate as well, but each element. For instance,

 for(std::size_t i{}; i < n; ++i) { for(char c: A[i]) { if(c == '(' || c == '[' || c == '{') { 
0
source

I could not resist making a kind of puzzle version. The task that I set, just for fun, was to write the right program for the task, which in itself is balanced. This largely excludes curly braces in literals like "[" or,

 if (A[i] == "(" || A[i] == "{" || A[i] == "["){ a.push(A[i]); } 

For fun, I pass the source code through myself, and it returns OK == true. Of course, it works for any file.

Note how reading a file and filtering uninteresting characters is discarded by the get function.

 #include <stack> #include <string> #include <fstream> #include <cassert> static std::string chars{ "(){}[]" }; static char get(std::ifstream &in) { char nxt; while (in >> nxt) { if (std::string::npos != chars.find(nxt)) { return nxt; } } return 0; } int main(int argc, char* argv[]) { std::ifstream in(argv[1]); std::stack<char> stk; bool OK = true; for (char nxt = get(in); nxt; nxt = get(in)) { auto pos = chars.find(nxt); if (pos % 2) { if (stk.empty() || stk.top() != chars[pos-1]) { OK = false; break; } else { stk.pop(); } } else { stk.push(nxt); } } return OK; } 
-1
source

Source: https://habr.com/ru/post/1276241/


All Articles