Stacks help

Pages: 1234
Apr 8, 2020 at 11:03pm
Hello,

I have this assignment


For this assignment, write a program that uses a single stack to check whether a string containing braces, parentheses, and brackets is properly delimited.

Then, write another program that uses a single queue to check whether a string containing braces, parentheses, and brackets is properly delimited.

Please note that two separate programs will be submitted. One using a stack and the other using a queue.

This is the code I have and there are a few errors and I'm not sure how to fix any of this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 #include <iostream>
#include <bits/stdc++.h>

using namespace std;

//Function to check if parenthesis is balanced
bool areParenthesisBalanced(string expr)
{
	stack <char> s;
	char x;

	//Traversing the expression 
	for (int i = 0; i < expr.length(); i++)
	{
		if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{')
		{
			//Push the element into the stack
			s.push(expr[i]);
			continue;
		}

		//If current character is not opening the stack, then it must be closed and empty
		if (s.empty())
			return false;

		switch (expr[i])
		{
			case ')';

				//Store the top element in a
				x = s.top();
				s.pop();
				if (x == '{' || x == '[')
					return false;
				break;

				case'}';
					//Store the top element in b
					x = s.top();
					s.pop();
					if (x == '(' || x == '[')
						return false;
					break;
					case ']';

						//Store the top element in c
						x = s.top();
						s.pop();
						if (x == '(' || x == '{')
							return false;
						break;
		}

		}

	//Check empty stack
	retrun(s.empty());
	}

};


This is the 2nd program i have

#include <iostream>

using namespace std;

char findClosing(char c)
{
if (c == '(')
return ')';
if (c == '{')
return '}';
if (c == '[')
return ']';
return -1;
}

//function to check to see if parenthesis is balanced
bool check(char expr[], int n)
{
//bases case
if (n == 0)
return true;
if (n == 1)
return false;
if (expr[0] == ')' || expr[0] == '}' || expr[0] == ']')
return false;

//search for closing bracker for first opening bracker
char closing = findClosing(expr[0]);

int i, count = 0;
for (i = 1; i < n; i++) {
if (expr[i] == expr[0])
count++;
if (expr[i] == closing) {
if (count == 0)
break;
count--;
}
}
}

//IF we don't find a closing bracket
if (i == n)
return false;

//if closing bracket was next to open
if (i == 1)
return check(expr + 2, n - 2);

if closing bracket is in the middle
return check(expr + 1, i - 1) && check(expr + i + 1, n - i - 1);
}

int main() {
char expr[] = "[(]";
int n = strlen(expr);
if (check(expr, n))
cout << "Balanced";
else
cout << "Not balanced";
return 0;
}








Apr 9, 2020 at 12:04am
> This is the code I have and there are a few errors
¿what errors?

> This is the 2nd program i have
I fail to see a queue there
Apr 9, 2020 at 8:41am
There is a typo on line 57.
In your switch statement you need to replace the ; with a : after case.

There is also a logic error in the code, before you try to fix it watch this tutorial to understand the logic better.
https://www.youtube.com/watch?v=QZOLb0xHB_Q
Apr 9, 2020 at 3:13pm
How about this ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <stack>

using namespace std;

bool is_matching_pair(char open, char close)
{
    if (open == '[' && close == ']') return true;
    else if (open == '{' && close == '}') return true;
    else if (open == '(' && close == ')') return true;

    return false;
}
bool isParenthesisBalanced(string exp)
{
    stack<char> s;
    for (int i = 0; i < exp.length(); ++i)
    {
        // if we encounter opening braces, we push them on to the stack
        if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
            s.push(exp[i]);
        // if we encounter closing braces,
        else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
        {
            //check either if the stack is empty or the braces don't match with the opening ones :
            if (s.empty() || !is_matching_pair(s.top(), exp[i]))
                return false;
            else // if the stack is not empty or the pair matches, pop this closing one
                s.pop();
        }
    }
    return s.empty();// if we got here and the stack is empty, we matched all characters
}


int main()
{
    string expression;
    cout << "Enter a string: ";
    cin >> expression;
    if (isParenthesisBalanced(expression))
        cout << "Balanced!\n";
    else
        cout << "Not balanced!\n";
    return 0;
}
Apr 9, 2020 at 3:54pm
Looks good.
Apr 9, 2020 at 7:00pm
It’s a two part assignment. How would I do one with a queue ?
Apr 9, 2020 at 7:13pm
@cblack618, I thought your latest effort was too good to be true:
https://gist.github.com/mycodeschool/7207410
I'm sure your teacher can spot the minor differences to that.
Last edited on Apr 9, 2020 at 7:23pm
Apr 9, 2020 at 9:35pm
Your point ?
Apr 9, 2020 at 9:38pm
Take a guess, cblack. What do you think he might be saying? Do you think he's making a witty comment about Shakespearean poetry, maybe? Could that be it?

Apr 9, 2020 at 9:45pm
I am not a witty comment decipher
Apr 10, 2020 at 10:25am
He says you're cheating on your assignment. If you weren't smart enough to realise that's what he's saying, you're not smart enough for programming.
Apr 10, 2020 at 12:22pm
I kind of noticed a mistake
Apr 10, 2020 at 12:22pm
In line 5
Apr 10, 2020 at 1:19pm
You don't need is_matching_pair(). By the time you get to the call, you know that the input is ')', ']' or '}', so line 27 can be:
if (s.empty() || s.top() != exp[i]))
Apr 14, 2020 at 12:37am
can someone help me finish this off ?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <string>
#include <stack>



using namespace std;


//Function prototype
bool isBalanced(string str);

int main() {

	string str;
	cout << "Enter a parenthesis string: " << endl;
	cin >> str;

	if (isBalanced(str))
		cout << "This is a balanced parenthesis string. " << endl;
	else
		cout << "This is not a balanced parenthesis string. " << endl;

	return 0;
}

bool isBalanced(string str) {
	stack <char> s;
	char x;

	for (int k = 0; k < str.length(); k++)
	{
		switch (str[k])
			case ')':

				//Store the top element in a
				x = s.top();
				s.pop();
				if (x == '{' || x == '[')
					return false;
				break;

			case'}':
				//Store the top element in b
				x = s.top();
				s.pop();
				if (x == '(' || x == '[')
					return false;
				break;
			case ']':

				//Store the top element in c
				x = s.top();
				s.pop();
				if (x == '(' || x == '{')
					return false;
				break;
	}
}
	
//Check empty stack
return(s.empty());
	}
};
Apr 14, 2020 at 4:21pm
You're going backwards. Why didn't you use your previous code?
Apr 14, 2020 at 9:25pm
Cause someone pointed out that is someone elses code and I dont want my teacher to accuse me of cheating or plaigarism
Apr 15, 2020 at 1:09am
Can someone help me please ?
Apr 15, 2020 at 1:40am
cblack618 wrote:
Can someone help me please ?

Apparently not.

Because cblack618 isn’t actually interested in help. He is interested in a handout.

Hasn’t anyone noticed that every single one of his threads terminate the same way?

I’m impressed some of us keep trying.
Apr 15, 2020 at 1:46am
1) don't be rude 2) i am not looking for a handout. I prsented code i need help with. a handout is someone doing 100 percent of the work
Pages: 1234