Balanced Multiple Delimiters modified from program challenge 18-15 (p.1100). The
ID: 3539892 • Letter: B
Question
Balanced Multiple Delimiters modified from program challenge 18-15 (p.1100).
The purpose of this assignment is to detect whether the delimiters are balanced, i.e. the counting up and down of the opening and closing (of a specific delimiter) is balanced. This assignment is not intended as a full exercise for a parser. The delimiters have no semantic meaning as in a programming language.
A string may use more than one type of delimiter to bracket information into blocks. For example, A string may use braces { }, parentheses ( ), angle brackets <> and square brackets [ ] as delimiters. A string is properly delimited if each right delimiter is matched with a preceding left delimiter of the same type in such a way that either the resulting blocks of information are disjoint, or one of them is completely nested within the other.
Write a program that uses multiple stacks to check whether a file containing braces, parentheses, and brackets is properly delimited or not. Input the test pattern from the test.txt file to check the delimiters inside are balanced or not. Report any unbalanced delimiter types, with the indication on which of the opening or closing delimiter is unbalanced.
For example,
{ while(a==(3+b)){ b = c+d; if (b == c){cout << b;} else cout << c;}}}
The above code (from the selected input file) is not closing brace balanced, and not opening angle bracket balanced.
Provide a simple menu, so the user can apply the balanced test to different files without having to invoke the program for different test.
i need the code in c++
Explanation / Answer
string s = input char c = null int i=0 do if s[i] isAOpenChar() c = s[i] else if c = isACloseChar() if closeMatchesOpen(s[i],c) erase s[i] while s[--i] != c ; erase s[i] c == null i = 0; // Not optimal! It would be better to back up until you find an opening character else return fail end if while (s[++i] != EOS) if c==null return pass else return fail
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.