Question: my code in process feedback in C + + #include #include #include using namespace std; class MathExpressionStack { private: struct ExpressionNode { char character;

my code in process feedback in C++"#include
#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(){
top = nullptr;
}// Constructor
~MathExpressionStack(){
while (!isEmpty()){
char c;
int l, p;
popExpression(c, l, p);
}
}// Destructor
void push(char ch, int line , int pos){
ExpressionNode* newNode = new ExpressionNode;
newNode-> character = ch;
newNode-> lineNumber = line;
newNode-> position = pos;
newNode->next = top;
top = newNode;
}
bool popExpression(char& ch, int& line, int& pos){
if (!isEmpty()){
ch = top -> character;
line = top -> lineNumber;
pos = top -> position;
ExpressionNode*temp = top;
top = top -> next;
delete temp;
return true;
}else {
ch ='\0';
line = pos =-1;
return false;
}
// return true;
}
bool isEmpty() const{
return top == nullptr;
}
};
bool isMatchingPair(char opening, char closing){
return (opening =='(' && closing ==')')||( opening =='[' && closing ==']');
}
int main(){
MathExpressionStack expressionStack;
//string testInput []={"([)]","(())","[]]", "END"};
string currentLine;
vector allLines;
int lineCount =0;
cout << "Enter expressions line by line. Type 'END' to finish input:
";
while(true){
// cout <<">";
getline(cin, currentLine);
if(currentLine == "END"){
break;
}
allLines.push_back(currentLine);
}
//getline(cin, currentLine);
// int lineCount =0;
// for (const string& currentLine : testInput){
// if(currentLine != "END"){
// Process each character in the current line
// bool errorFound = false;
for ( int i =0; i < allLines.size(); i++){
lineCount = i+1;
currentLine = allLines[i];
for ( int j =0; j < currentLine.length(); j++){
char currentChar = currentLine[j];
if (currentChar =='('|| currentChar =='['){
expressionStack.push(currentChar, lineCount, j +1);
}
else if (currentChar ==')'|| currentChar ==']'){
if (expressionStack.isEmpty()){
cout <<"Right delimiter "<< currentChar
<<" had no left delimiter found at line "<< lineCount <<
", char "<< j +1<< endl;
// errorFound = true;
}else {
char lastOpening;
int lastLine, lastPos;
expressionStack.popExpression(lastOpening, lastLine, lastPos);
if (!isMatchingPair(lastOpening, currentChar)){
cout << "Mismatched operator "<< lastOpening <<" found at line "<< lastLine <<", char "<< lastPos
<<" does not match "<< currentChar <<" found at line "<< lineCount <<", char "<< j+1<< endl;
// errorFound = true;
}
}
}
}
}
// Check for unmatched opening delimiters...
while (!expressionStack.isEmpty()){
char ch;
int line, pos;
expressionStack.popExpression(ch, line, pos);
cout << "Left delimiter "<< ch <<" at line "<< line <<", char "<< pos <<" had no right delimiter." << endl;
// errorFound = true;
}
return 0;
}" needs to return this putput given this input Enter expressions line by line. Type 'END' to finish input:
>(3+5)*[2-(4/2]
>1+2)*3
> END
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.
but it returns Enter expressions line by line. Type 'END' to finish input:
>(3+5)*[2-(4/2]
>1+2)*3
> END
Mismatched operator ( found at line 1, char 16 does not match ] found at line 1, char 22 Mismatched operator [ found at line 1, char 11 does not match ) found at line 2, char 6

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!