Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given a text file, your program wi: emine if all the peren he es, curly braces,

ID: 3694598 • Letter: G

Question

Given a text file, your program wi: emine if all the peren he es, curly braces, and square brackets match, and are nested appropriately Your program should work for mathematical formulas and most computer programs Your program should read in the characters from the file, but ignore all characters except for the following:O The general algorithm is to use a stack to store the opening unmatched brackets. When a closing bracket is encountered, check it against the one on top of the stack (pop it off--make sure it matches. When you are finished there should be no unmatched brackets left on the stack. Your program should first implement a char stack. CharStack.h is provided on the website. You must supply the CharStack.cpp file that includes the implementations of the functions in the class declaration. Note that the stack elements will be stored in a string, and no variable named top is necessary. You can complete this assignment using these string functions: at(int), size), appendO or+, and substr(int, int) Note: Do NOT use these functions: push back, pop back, or back Input/Output Your Driver program must prompt the user to enter a filename. It should then try to open the file and then check it make sure the brackets all match appropriately. If they all match, the program should output a message saying so. If not, the program should output an appropriate crror message. There are three types of errors that can happen (and they can happen with any kind of bracket): missing if you reach the end of the file, and there is an opening that was never matched, like: int main x[size]-10; expected but found) : this is a wrong closing bracket, like: xli]-10. unmatched this occurs if there is a closing bracket but not an opening bracket (not even one of the wrong kind), like: int main 0 xi 10 NOTES: Your program must compile and run, otherwise you will receive a score of 0. There is NO Test Case 0 for this assignment. As soon as you encounter an error, your program should stop and print out the appropriate error message. Do NOT try to keep going and find n ind more errors It might be easier to store the expected closing character on the stack when an opening bracket is encountered. This simplifies the matching when a closing bracket is encountered. Beware of stack underflow! Do NOT try to pop an empty stack! Your pop function should include a check that will abort the program if the driver attempts to pop an empty stack. Your driver program should avoid attempting to pop an empty stack. In other words when I run your driver program, it should never abort/crash Follow the style guidelines from the class website Logistics: For this assignment you need to submit two files CharStack.cpp and your driver

Explanation / Answer

I had converted the java source code which I had earlier into cpp

main file:

#include "Parentheses.h"

namespace stackparanthesis
{

   bool Parentheses::Balanced(const std::wstring &s)
   {
       std::stack<wchar_t> stack;
       for (int i = 0; i < s.length(); i++)
       {

           if (s[i] == L_PAREN)
           {
               stack.push(L_PAREN);
           }

           else if (s[i] == L_BRACE)
           {
               stack.push(L_BRACE);
           }

           else if (s[i] == L_BRACKET)
           {
               stack.push(L_BRACKET);
           }

           else if (s[i] == R_PAREN)
           {
               if (stack.empty())
               {
                   return false;
               }
               if (stack.pop() != L_PAREN)
               {
                   return false;
               }
           }

           else if (s[i] == R_BRACE)
           {
               if (stack.empty())
               {
                   return false;
               }
               if (stack.pop() != L_BRACE)
               {
                   return false;
               }
           }

           else if (s[i] == R_BRACKET)
           {
               if (stack.empty())
               {
                   return false;
               }
               if (stack.pop() != L_BRACKET)
               {
                   return false;
               }
           }

           // ignore all other characters

       }
       return stack.empty();
   }

   void Parentheses::main(std::vector<std::wstring> &args) throw(IOException)
   {
       //String s = StdIn.readAll();
       BufferedReader *br = new BufferedReader(new InputStreamReader(System::in));
       std::wstring str = L"";
       Parentheses *p = new Parentheses();
       std::wcout << p->Balanced(str = br->readLine()) << std::endl;
   }
}

header file:

#pragma once

#include <string>
#include <vector>
#include <stack>
#include <iostream>

namespace stackparanthesis
{


   class Parentheses
   {
   private:
       static const wchar_t L_PAREN = L'(';
       static const wchar_t R_PAREN = L')';
       static const wchar_t L_BRACE = L'{';
       static const wchar_t R_BRACE = L'}';
       static const wchar_t L_BRACKET = L'[';
       static const wchar_t R_BRACKET = L']';

   public:
       static bool Balanced(const std::wstring &s);


       static void main(std::vector<std::wstring> &args) throw(IOException);

   };

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote