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 ((0),[]).
Validation Rules:
Ignore any characters that are not relevant (e.g., 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 2, char 12 does not match ) found at line 4, char 8
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 5, char 15
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 3, char 7 had no right delimiter.
Left delimiter ( at line 6, char 10 had no right delimiter.
Please refer to the following kind of expected interaction with a successful implementation:
User Input:
(3+5)*[2-(4/2]
1+2)*3
END
Program Output:
Mismatched operator ( found at line 1, char 8 does not match ) found at line 2, char 6
Right delimiter ] had no left delimiter found at line 2, char 13
Left delimiter [ at line 1, char 10 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 push(char, int, int);
void popExpression(char&, int&, int&);
bool isEmpty();
};
int main(){
MathExpressionStack expressionStack;
string currentLine;
int lineCount =0;
do {
lineCount++;
getline(cin, currentLine);
// Process each character in the current line
for (int i =0; i currentLine.length(); ++i){
char currentChar = currentLine[i];
// Handle opening and closing delimiters...
}
} while (currentLine != "END");
// Check for unmatched opening delimiters...
return 0;
}
Implementation Steps: Stack Class Creation:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!