Hello, and thank you for taking the time to help me! I am working on a program that reads a string after the option "E" (enter a string) is chosen, and checks if parentheses (), brackets [], and braces {} are matched in it. There are two files, the main driver file, called Assignment8.cpp, and the utility file, MatchChecker.h. The header file has one class, the MatchChecker class, described below.
The MatchChecker class is a utility class that will be used to check if parentheses (), brackets [], and braces {} are matched in a given string. The MatchChecker class object will never be instantiated. It must have the following public function:
static string matchChecker(string lineToCheck)
The matchChecker function's argument will be a string that can contain parenthesis, brackets, and braces. Other characters or numbers can appears before/after/between them.
If all of them are matched, then the function should return the string:
"Everything is matched!"
If there is a closing parenthesis, bracket, or brace that does not have its corresponding opening parenthesis, bracket, or brace, then for the first such character, return a string with its position such as:
") at the position 15 does not match."/"] at the position 12 does not match."/"} at the position 28 does not match."
The first character of the string is seen as the position 0.
If there is no matching closing parenthesis, bracket, and brace when you reach the end of the string after checking each character, return a string notifying the last opening parenthesis, bracket, or brace that did not have its matching closing one as:
") is missing."/"] is missing."/"} is missing."
Requirements:
You need to implement this function using the stack using stack.h in C++ STL(Standard Template Library).
You can use the string "A" as a token for parentheses, "B" as a token for brackets, "C" as a token for braces.
So, as you can see, its not a very complex program, but I have never worked with stacks before, so its rough for me. I have a good amount of the code written, and everything is compiling fine, but when I enter a string and then press the Enter key, the program has an error and Windows forces me to terminate the program.
Here my files, and they are also attached so that you can run them if you want.
Code:
// Assignment #: 8
// Name: Your name
// EmailAddress:
// Description: It displays a menu of choices to a user
// and performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is entered.
#include <iostream>
#include "MatchChecker.h"
using namespace std;
void printMenu();
int main()
{
char input1;
string line;
string inputInfo;
MatchChecker checker1;
printMenu();
do // will ask for user input
{
cout << "What action would you like to perform?" << endl;
cin >> input1;
cin.ignore(30, '\n');
// matches one of the case statements
switch (input1)
{
case 'E': //Enter String
cout << "Please enter a string.\n";
getline(cin, inputInfo, '\n');
checker1.matchChecker(inputInfo);
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action\n";
break;
}
} while (input1 != 'Q' && input1 != 'q');
return 0;
}
/** The method printMenu displays the menu to a user**/
void printMenu()
{
cout << "Choice\t\tAction\n";
cout << "------\t\t------\n";
cout << "E\t\tEnter String\n";
cout << "Q\t\tQuit\n";
cout << "?\t\tDisplay Help\n\n";
}
Code:
#include <stdio.h>
#include <sstream>
#include <string>
#include <stdexcept>
// protections
#ifndef MatchChecker_H
#define MatchChecker_H
using namespace std;
class MatchChecker
{
public:
static string matchChecker(string lineToCheck)
{
Stack<Character> myStack = new Stack<Character>();
for (int j = 0; j < input.length(); j++)
{
char ch = input.charAt(j);
switch (ch)
{
case '{':
case '[':
case '(':
theStack.push(ch);
break;
case '}':
case ']':
case ')':
if (!theStack.isEmpty())
{
char chx = theStack.pop();
if ((ch == '}' && chx != '{') || (ch == ']' && chx != '[') || (ch == ')' && chx != '('))
cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
}
else
cout << "Error: " << ch << " at the position " << j << " does not match" << endl;
break;
default:
break;
}
}
if (!theStack.isEmpty()){
cout << "Error: missing right delimiter" << endl;
}
};
#endif