Chapter 5: STACKS Data Structures

Develop an expression manager that can do a balanced symbol check.
Your program will read three different mathematical expressions from the user that are the followings:

string expression = "{(0+1)*(2+3)}";
string expression = "{(0+1)+[4*(2+3)]}";
string expression = "{(0+1)+[4*(2+3)}}";

As you notice the expressions contain the following symbols: { , }, ( , ), [ , ].
Write a program PEX5.cpp that after it reads the above expressions will check and report whether the expression is balanced or not.
Remember that { , }, ( , ), [ , ] are the only symbols that will be checked. All other characters will be ignored.
Remember to use the stack STL as you process each expression. Call the method that processes the expression as follows:
bool isItBalanced(string inputExpr)
If there is a mismatch in the expression make sure you report the symbol of the mismatch and its actual position in the expression.

Expected output:
Enter your expression: {(0+1)*(2+3)}

Expected output: {(0+1)*(2+3)} == 1

Enter your expression: {(0+1)+[4*(2+3)]}

Expected output: {(0+1)+[4*(2+3)]} == 1

Enter your expression: {(0+1)+[4*(2+3)}}

Mismatch found : } at 15

Expected output: {(0+1)+[4*(2+3)}} == 0

[code]

//Assignment 5
//PEX5.cpp

#include<iostream>
#include<conio.h>
#include<stack>
#include<string>

using namespace std;

static int position = 0;
static char data[1];

// Function to check whether two characters are opening
// and closing of same type.
bool ArePair(char opening, char closing)
{
if (opening == '(' && closing == ')') return true;
else if (opening == '{' && closing == '}') return true;
else if (opening == '[' && closing == ']') return true;
return false;
}
//Method to check the expression is balanced or not
bool isItBalanced(string exp)
{
stack<char> S;
for (int i = 0;i<exp.length();i++)
{
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
S.push(exp[i]);
else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if (S.empty() || !ArePair(S.top(), exp[i]))
{
position = i;
data[0] = exp[i];
return false;
}

else
S.pop();
}
}
return S.empty() ? true : false;
}

void display(int i, string expression)
{
if (!i)
{
cout << "\nMismatch found :" << data << " at " << position << "\n";
cout << "\nExpected output:" << expression << "==" << i << "\n";
}
else
{
cout << "\nExpected output:" << expression << "==" << i << "\n";
}
}
int main()
{
std::string expression1, expression2, expression3;
std::cout << "\nEnter your expression:";
std::cin >> expression1;
display(isItBalanced(expression1), expression1);
std::cout << "\nEnter your expression:";
std::cin >> expression2;
display(isItBalanced(expression2), expression2);
std::cout << "\nEnter your expression:";
std::cin >> expression3;
display(isItBalanced(expression3), expression3);
return 0;
}

[code]

Error coming up is on data
"data" is ambiguous
Could someone please help ???

I don't wanna develop an expression manager though. ;(
Your code compiles fine in GCC, is that a warning or an error? Ambiguity is typically the result of poorly named variables, maybe try renaming your data variable to something else?
I am using visual studio its an error, I will try renaming data, I also tried it in codeBlocks and it runs fine
Thank you!
closed account (z05DSL3A)
I am using visual studio its an error, I will try renaming data
Read up on scope resolution. The 'data' you wish to use is in global scope so using ::data should resolve the ambiguity.

Your variable is not necessarily poorly named but poorly scoped.
Also note that here:
cout << "\nMismatch found :" << data << " at " << position << "\n";
you are treating data as a nul-terminated string and it is not, resulting in undefined behavior.

I'm at a loss as to why you're using an array to hold a single character.
Topic archived. No new replies allowed.