Jump to content

Check Parenthesis C++

Sebastian Kurpiel

What's up guys, I have tried to write code to check if an equation has balanced parenthesises my code outputs the equation but it doesn't check if the parenthesises are balanced. These were the goals of my professor 

  • In class lab called Parenthesis Checker
  • Write an algorithm to check if we have all the parenthesis in an equation
  • You will be using a stack of type char in order to do this
  • You will use a string to capture the equation to see if it is balanced


#include <iostream>
#include <stack>
#include <string>

using namespace std;

bool check_parenthesis(string equation) {
    char holder;
    stack<char> par_holder;
    for (int i = 0; i<equation.length(); i++) {
        holder = equation[i];
        cout << "Test:" << holder << endl;
        //Please right the algorithm in order to see if parenthesis are correct
        if (equation[i] == '(')
        {
            par_holder.push(equation[i]);
        }
        else if (equation[i] == ')')
        {
            if (par_holder.empty())
                return false;
            else
                par_holder.pop();
        }
    }
    return true;
}


int main() {
    string equation;
    while (equation != "q") {
        cout << "Please enter an equation or q to quit: ";
        //Please have write tthe outcomes correctly, like that of the example output
        cout << "please enter an equation" << endl;
        cin >> equation;
        check_parenthesis(equation);
    }
    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, Sebastian Kurpiel said:

but it doesn't check if the parenthesises are balanced. 

check_parenthesis(equation);

You are checking if the parenthesis are balanced, your're just never printing out the result.

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, fizzlesticks said:

check_parenthesis(equation);

You are checking if the parenthesis are balanced, your're just never printing out the result.

 

 

Where would I  add the cout << "balanced!" part? I have tried everywhere and I'm still receiving errors...

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Sebastian Kurpiel said:

Where would I  add the cout << "balanced!" part? I have tried everywhere and I'm still receiving errors...

You'd have to check the return value from check_parenthesis

if(check_parenthesis(equation))
{
	//parenthesis are balanced
}
else
{
	//not balanced
}

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

std::count_if()

 

You'll need to write a custom increment/decrement lambda, but this is trivial.

Software Engineer for Suncorp (Australia), Computer Tech Enthusiast, Miami University Graduate, Nerd

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×