I dont want my teacher to accuse me of cheating or plaigarism
You don't like to be accused? Does that leave the options:
A. Don't cheat
B. Don't get caught
The option A is much easier, considering the technology that the teacher can now use.
What can you learn from the code that you can't use?
What can you learn from the errors that you have made?
Don't answer to us, answer to yourself.
Here is most of the algorithm, taken right from the previously posted code. Try writing your own code to match it
1 2 3 4 5 6 7 8 9
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 we encounter closing braces, then pop the stack and see if the closing brace matches
}
}
There are two special cases that you'll need to add:
- What if the stack is empty when you encounter a closing brace? That happens when there is an extra closing brace somewhere.
- What if the stack is not empty when you're done processing the string? That happens when there is an extra opening brace somewhere.
#include <iostream>
#include <string>
#include <stack>
usingnamespace std;
//Function prototype
bool isBalanced(string s);
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> charStack;
char x;
for (int k = 0; k < str.length(); k++)
{
{
switch (str[k])
{
case'(':
// THIS IS WEHRE YOU PUT left paren on stack
charStack.push(str[k]); break;
case')':
// CHECK TO SEE IF THE right paren has a mate on the stack
if (charStack.empty())
returnfalse;
else
charStack.pop();
break;
default:
// ITHIS IS HOW YOU ignore all other characters
;
}
}
if (charStack.empty() || !isBalanced(charStack.top(), str[k]))
returnfalse;
else
charStack.pop();
}
}
//Check empty stack
return charStack.empty());
}
Lines 32 & 33. You don't need both blocks. Delete line 33 and the matching '}' at line 50.
Lines 41-45: You aren't actually checking if the character matches what's on the stack.
Line 52: why7 is this calling isBalanced again? Why is it passing two parameters?
Line 55: why do you pop exactly one item? How many items should be on the stack here? None? One? 75? Why?
Coding isn't like writing an essay where you can babble on about random stuff. In programming, you have to be extremely precise. Every statement means something specific and you need to understand and intend every single one.
Getting a "Cannot open include "bits/stdc" error message
<bits/stdc++.h> is a GCC implementation use only header, used as an implementation of a compiled header setup. It is not available with any other compiler, certainly not MSVC.
It is not to be included by the programmer.
Competition sites use it, and do it for specific and IMO wrong reasons.
Fix what? That code doesn't even compile, so start by looking at your compiler errirs and fixing them.
Also, you would help yourself enormously if you adopted a sensible indentation style. That would help you see at a glance the logic and structure of your code.
Because people keep accusing me of hearing. So I started fresh with a new implementation. I’m a true beginner but in this C++ college course. I’m getting errors but don’t know how to fix the errors. I would appreciate some help
If you're getting errors, why aren't you telling us what the errors are? How does it benefit you to withold that information from the people who might be able to help you?
I thought the purpose of the "edit and run" function built in on was for someone to see whether the code i posted compiles or what errors popped up ? versuses listing them all invidually