Question: Implementation Steps: Stack Class Creation: Define a stack class that stores nodes containing a character ( char ) , a line number ( int )
Implementation Steps:
Stack Class Creation:
Define a stack class that stores nodes containing a character char a line number int and a position int within the line. Use a structure to encapsulate these attributes.
Input Handling:
The program will accept lines of mathematical expressions from the user until the line "END" is entered.
Operator and Parentheses Validation:
As each line is processed, the stack will be used to verify the syntax of the mathematical expressions, focusing on operators and parentheses
Validation Rules:
Ignore any characters that are not relevant eg letters, spaces
For each opening parenthesis or bracket encountered, push it onto the stack along with the line number and character position.
For each closing parenthesis or bracket, pop from the stack and verify that it matches the last opening delimiter. If there's a mismatch, output an error message indicating the discrepancy.
If a closing delimiter is found when the stack is empty, output an error message indicating that no matching opening delimiter exists.
At the end of the input, if any opening delimiters remain in the stack, output their details to indicate unmatched delimiters.
Expected Output:
Mismatched Operators:
If a closing parenthesis or bracket does not match the last opening one, the output will look like this:
Mismatched operator found at line char does not match found at line char
Unmatched Closing Delimiters:
If a closing delimiter is encountered while the stack is empty, the output will be:
Right delimiter had no left delimiter found at line char
Unmatched Opening Delimiters:
After processing all input, if any unmatched opening delimiters remain in the stack, the output will be formatted as follows:
Left delimiter at line char had no right delimiter.
Left delimiter at line char had no right delimiter.
Please refer to the following kind of expected interaction with a successful implementation:
User Input:
END
Program Output:
Mismatched operator found at line char does not match found at line char
Right delimiter had no left delimiter found at line char
Left delimiter at line char had no right delimiter.
Here is a program structure for you to begin:
#include
#include
using namespace std;
class MathExpressionStack
private:
struct ExpressionNode
char character;
int lineNumber;
int position;
ExpressionNode next; Pointer to the next node
;
ExpressionNode top; Pointer to the top of the stack
public:
MathExpressionStack; Constructor
~MathExpressionStack; Destructor
void pushchar int, int;
void popExpressionchar& int& int&;
bool isEmpty;
;
int main
MathExpressionStack expressionStack;
string currentLine;
int lineCount ;
do
lineCount;
getlinecin currentLine;
Process each character in the current line
for int i ; i currentLine.length; i
char currentChar currentLinei;
Handle opening and closing delimiters...
while currentLine "END";
Check for unmatched opening delimiters...
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
