I've been asked as a bonus programming challenge to see if braces match in a random string or char like this: {1+1} this would return 1, while {1+1}) would return 0.
This is what I have so far but it doesn't seem to do anything. Any help would be great? thanks
//bonus.cpp
#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
int checkBraces (string s)
{
//int myLength = s.length();
std::stack<int> stack;
char d;
for (int i = 0; i < s.length(); i++)
{
char c = s[i];
if (c == '(')
{
stack.push(c);
}
else if (c == '[')
{
stack.push(c);
}
else if (c == '{')
{
stack.push(c);
}
else if (c == ')')
{
if (stack.empty())
{
return false;
}
else
{
d = stack.top();
stack.pop();
if (d != '(')
{
return false;
}
}
}
else if (c == ']')
{
if (stack.empty())
{
return false;
}
else
{
d = stack.top();
stack.pop();
if (d != '[')
{
return false;
}
}
}
else if (c == '}')
{
if (stack.empty())
{
return false;
}
else
{
d = stack.top();
stack.pop();
if (d != '{')
{
return false;
}
}
}
}
if (stack.empty()) return true;
else return false;
}
int main()
{
cout << "This program checks brace ([{}]) matching in a string." << endl;
checkBraces ("{1+1}");
}
What makes you think it doesn't do anything? It does. It checks for braces, but you're not doing anything with the return of checkBraces, which, btw, should return a bool, not an int.
Did you perhaps meant something like:
if (checkBraces ("{1+1}"))
cout << "matching";
else
cout << "not matching";
Pro-tip: learn how to use a debugger. You should learn how to debug before you start coding anything more than a "hello world".
As an addition to what have already been said, I would say that you could reduce the amount of code. As anyway you put chars into your stack, why not having a std::stack<char>?
You could save the braces into another string, to automatically compare it using one of the std::algorithms
const std::string openingBraces("{[(");
const std::string closingBraces("}])");
if (std::find(openingBraces.begin(), openingBraces.end(), currentChar) != openingBraces.end())
yourStack.push(currentChar);
else if (std::find(closingBraces.begin(), closingBraces.end(), currentChar) != closingBraces.end())
{
// check if currentChar is matching the one on top of your stack
}
I haven't written everything as it's always better to find answers by yourself.
Minimum you should do is to print the outcome of checkBraces.
but it doesn't seem to do anything
It does do something. It prints This program checks brace ([{}]) matching in a string..
You are calling checkBraces ("{1+1}") but you aren't doing anything with the returned value. Since this call can be optimized away, you are in a sense correct that your program doesn't seem to do anything.
So make it do something. Print the string that is to be tested, then print the result of the test. Once you have done that, you should test, and when you're done with that, you should test some more. Don't just test easy cases such as {i+1}. Test convoluted cases that should pass, and also test cases that should fail.
Learning how to test and learning how to debug are just as important skills (if not more important skills) as is learning how to write code.
Related
So, I was doing Data Structure and ALgorithm using C++ and STL, I was trying to implement Infix to postfix using stack. I am not sure what is this issue with the code? There is no compile error and when the code runs it returns -1073741510. I have rechecked the whole code, couldn't found any issues
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int isOperator(char ch)
{
if(ch=='+' || ch == '-' || ch == '*' || ch == '/')
return 1;
else
return 0;
}
int precedence(char ch)
{
//for limited input only
if(ch == '*' || ch == '/')
return 3;
else if(ch=='+' || ch == '-' )
return 2;
else
return 0;
}
string infixtopostfix(string infix)
{
stack <char> st;
int i=0;
string postfix;
while(infix[i]!='\0')
{
if(!isOperator(infix[i]))
{
postfix.push_back(infix[i]);
i++;
}
else
{
if(precedence(infix[i])>precedence(st.top()))
{
st.push(infix[i]);
i++;
}
else{
postfix.push_back(st.top());
st.pop();
}
}
}
while(!st.empty())
{
postfix.push_back(st.top());
st.pop();
}
return postfix;
}
int main()
{
string infix= "a+b";
cout<<"Postfix-->"<<infixtopostfix(infix)<<endl;
return 0;
}
Your postfix string has a size of zero. This means that any attempt to access the characters of that string is an error. If you want to add characters to a string use push_back.
postfix[j] = infix[i];
should be
postfix.push_back(infix[i]);
and (twice)
postfix[j] = st.top();
should be
postfix.push_back(st.top());
and
postfix[j]='\0';
should be removed.
There is no need to nul terminate std::string. Once you have made all these changes you will also see that the j variable can be removed. A std::string knows it's own size, you don't need a separate variable to keep track. It seems that you are programming a std::string as if it's like a C string.
It seems to be a very common misunderstanding that std::string (or a std::vector) automatically grows when you subscript it. This is not true.
EDIT
You have another error here,
if(precedence(infix[i])>precedence(st.top()))
The stack maybe empty when executing this statement, leading to a crash. I'm guessing the code should read
if(st.empty() || precedence(infix[i])>precedence(st.top()))
With that change your code works for me.
Thank You everyone for the help. Here is the final answer which is running without any error.
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int isOperator(char ch)
{
if(ch=='+' || ch == '-' || ch == '*' || ch == '/')
return 1;
else
return 0;
}
int precedence(char ch)
{
//for limited input only
if(ch == '*' || ch == '/')
return 3;
else if(ch=='+' || ch == '-' )
return 2;
else
return 0;
}
string infixtopostfix(string infix)
{
stack <char> st;
int i=0;
string postfix;
while(infix[i]!='\0')
{
if(!isOperator(infix[i]))
{
postfix.push_back(infix[i]);
i++;
}
else{
if(st.empty() || precedence(infix[i])>precedence(st.top()))
{
st.push(infix[i]);
i++;
}
else{
postfix.push_back(st.top());
st.pop();
}
}
}
while(!st.empty())
{
postfix.push_back(st.top());
st.pop();
}
return postfix;
}
int main()
{
string infix= "a+b";
cout<<"Postfix-->"<<infixtopostfix(infix)<<endl;
return 0;
}
So, the goal is to check to see if the C style string ends with a period or exclamation mark. However, for some reason, i keep getting false.
bool isItSentence(const char* s)
{
int x = strlen(s);
for (int c = 0; s[c] != '\0'; c++)
{
if (!isupper(s[0])) return false;
if (isupper(s[c]) && c > 0) return false;
if (s[x-1] != '.') return false;
if (s[x-1] != '!') return false;
}
return true;
}
int main()
{
std::string str = "Smelly.";
reverse(str.c_str());
std::cout << isItSentence(str.c_str()) << std::endl;
std::cout << strlen(str.c_str()) << std::endl;
system("pause");
Heres what I have so far. But when I add the last if statement to handle exclamation marks, it returns zero. Any suggestions?
First, note s[x-1] is a loop invariant, so you'd rather move it out of the for loop
if (s[x-1] != '.') return false;
if (s[x-1] != '!') return false;
this is always false (a char cannot be both a dot and an explanation mark).
the test should rather be
if (s[x-1] != '.' && s[x-1] != '!') return false;
This is my code for checking if a string of grouping characters is properly balanced.
It works fine on my local machine, but the online judge gives me a run-time error.
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool balanced(string exp)
{
stack<char> st;
int i;
for(i=0;i<exp.length();i++)
{
if(exp[i]== '{' || exp[i]=='[' || exp[i]== '(') st.push(exp[i]);
else if(exp[i]=='}'){
if(st.top() == '{' && !st.empty()) st.pop();
else return false;
}
else if(exp[i]==')'){
if(st.top() == '(' && !st.empty()) st.pop();
else return false;
}
else if(exp[i]==']'){
if(st.top()=='[' && !st.empty()) st.pop();
else return false;
}
}
if(st.empty())return true;
else return false;
}
int main() {
string exp;int n;
cin >> n;
cin.ignore();
while(n--)
{
getline(cin,exp);
bool balance = balanced(exp);
if(balance == true)cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
if(st.top() == '{' && !st.empty())
You should check for stack emptiness before taking the top.
if(!st.empty() && st.top() == '{')
Spacing
I have a few issues with your spacing and brace usage. First, your logic in your loop is WAY too indented. A single indent is fine. Second, do not write logic on the same line as if unless it's a trivial condition and has no else - and never write logic on the same line as else. It's impossible to read. Strongly prefer writing braces throughout. Also add a space after if
Here's balanced rewritten with better spacing:
bool balanced(string exp)
{
stack<char> st;
for(int i=0; i<exp.length(); i++)
{
if (exp[i]== '{' || exp[i]=='[' || exp[i]== '(') {
st.push(exp[i]);
}
else if (exp[i]=='}') {
if (st.top() == '{' && !st.empty()) {
st.pop();
}
else {
return false;
}
else if (exp[i]==')') {
if(st.top() == '(' && !st.empty()) {
st.pop();
}
else {
return false;
}
}
else if(exp[i]==']') {
if(st.top()=='[' && !st.empty()) {
st.pop();
}
else {
return false;
}
}
}
if (st.empty()) {
return true;
}
else {
return false;
}
}
Now that I can read your code, we can get to the logic issues.
Dealing with bools
You end with if (expr) return true; else return false;. That's exactly equivalent to just:
return st.empty();
Similarly, you have:
bool balanced = balanced(exp);
if (balance == true) ...
else ...
Never write == true, and you don't even need the variable here:
if (balanced(exp)) {
...
}
else {
...
}
Which can even be a ternary:
cout << (balanced(exp) ? "Yes" : "No") << endl;
Repetition
Your logic is extremely repetitive. We have three types of opens and three types of closed - we treat the opens identically, and we treat the closes the same way - check if the stack is non-empty and the top element is the equivalent open. For any char other than those 6 - we don't care.
So we can collapse our logic to be more explicitly equivalent with the three closes. Also a switch helps a lot here:
switch (exp[i]) {
case '{':
case '[':
case '(':
st.push(exp[i]);
break;
case '}':
case ']':
case ')':
if (!st.empty() && st.top() == open_brace(exp[i])) {
st.pop();
}
else {
return false;
}
break;
default:
break;
}
Where you just have to implement open_brace to do the right thing. This saves a bunch of code, which in turn makes it less error prone. Also note the reordering of the conditions - you need to check for non-emptiness first.
Arguments
balanced doesn't modify it's argument, or really need to do anything with it other than iterate over it. So take it by reference-to-const:
bool balanced(std::string const& expression)
And lastly...
using namespace std;
Avoid it.
I attended a quiz, I gave the code but the auto-test shows that one of the eight test cases failed.
I myself tested my code many times, but all passed. I can't find where is the problem.
The question is to design a algorithm to check whether the brackets in a string match.
1) Just consider rounded brackets () and square brackets [], omit ohter chars.
2) Each pair brackets should match each other. That means ( matches ), and [ matches ].
3) Intercrossing is not allowed, such as : ([)]. There are two pairs of brackets, but they intercross each other.
To solve the problem, my method is described as follows:
Search each char in the whole input string, the index from 0 to str.size() - 1.
Use two stacks to record the opening tag (, and [, each type in one stack. When encountering one of them, push its index in the corresponding stack.
When encouterning the closing tag ) and ], we pop the corresponding stack.
Before popping, check the top of two stacks, the current stack should have the max index, otherwise that means there are unmatched opening tag with the other type, so the intercrossing can be checked this way.
My Code is Here:
#include <iostream>
#include <stack>
using namespace std;
int main()
{
string str;
cin >> str;
stack<int> s1, s2;
int result = 0;
for (int ix = 0, len = str.size(); ix < len; ix++)
{
if (str[ix] == '(')
{
s1.push(ix);
}
else if (str[ix] == '[')
{
s2.push(ix);
}
else if (str[ix] == ')')
{
if (s1.empty() || (!s2.empty() && s1.top() < s2.top()))
{
result = 1;
break;
}
s1.pop();
}
else if (str[ix] == ']')
{
if (s2.empty() || (!s1.empty() && s2.top() < s1.top()))
{
result = 1;
break;
}
s2.pop();
}
else
{
// do nothing
}
}
if (!s1.empty() || !s2.empty())
{
result = 1;
}
cout << result << endl;
}
As methoned before, this question can be solved by just on stack, so I modified my code, and here is the single stack version. [THE KEY POINT IS NOT TO ARGUE WHITCH IS BETTER, BUT WHAT'S WRONG WITH MY CODE.]
#include <iostream>
#include <stack>
using namespace std;
int main()
{
string str;
cin >> str;
stack<char> s;
const char *p = str.c_str();
int result = 0;
while (*p != '\0')
{
if (*p == '(' || *p == '[')
{
s.push(*p);
}
else if (*p == ')')
{
if (s.empty() || s.top() != '(')
{
result = 1;
break;
}
s.pop();
}
else if (*p == ']')
{
if (s.empty() || s.top() != '[')
{
result = 1;
break;
}
s.pop();
}
else
{
// do nothing
}
p++;
}
if (!s.empty())
{
result = 1;
}
cout << result << endl;
}
When using formatted input to read a std::string only the first word is read: after skipping leading whitespate a string is read until the first whitespace is encountered. As a result, the input ( ) should match but std::cin >> str would only read (. Thus, the input should probably look like this:
if (std::getline(std::cin, str)) {
// algorithm for matching parenthesis and brackets goes here
}
Using std::getline() still makes an assumption about how the input is presented, namely that it is on one line. If the algorithm should process the entire input from std::cin I would use
str.assign(std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>());
Although I think the algorithm is unnecessary complex (on stack storing the kind of parenthesis would suffice), I also think that it should work, i.e., the only problem I spotted is the way the input is obtained.
I have written a code for infix to postfix conversion,This piece of code is not encountering any kind of compile time error but after taking the input infix expression it is giving some runtime errors which i am unable to understand these errors are something related to string as the message says.
#include<iostream>
#include<string>
#define N 50
using namespace std;
class stack
{
private:
char arr[N];
int tos;
public:
void push(char p)
{
if (tos != N)
arr[++tos] = p;
else
cout << "stack full";
}
char pop()
{
if (tos == -1)
cout << "stack Empty";
else
return (arr[tos--]);
}
bool isempty()
{
if (tos == -1)
return (true);
else
return (false);
}
char top()
{
return arr[tos];
}
stack()
{
tos = -1;
}
};
int pres(char sym)
{
if (sym == '^')
return 3;
else if (sym == '*' || '/')
return 2;
else if (sym == '+' || '-')
return 1;
else if (sym == '(')
return 0;
}
bool isoperator(char op)
{
if (op=='+' || op=='-' || op=='/' || op=='*' || op=='^')
return true;
else
return false;
}
int main()
{
string infix, postfix;
stack s;
int in=0;
int post=0;
cout << "Enter an infix expression: ";
cin >> infix;
s.push('(');
infix.append(")");
char temp;
while (!(s.isempty()))
{
if (isdigit(infix[in]))
postfix[post++] = infix[in];
else if (infix[in] == '(')
s.push(infix[in]);
else if (infix[in] == ')')
{
while (1)
{
temp = s.pop();
if (temp == '(')
break;
else
postfix[post] = infix[in];
}
}
else if (isoperator(infix[in]))
{
while (pres(s.top()) >= pres(infix[in]))
postfix[post++] = s.pop();
s.push(infix[in]);
}
in++;
}
cout << "Postfix expression is: " << postfix;
system("pause");
}
I m unable to get what's wrong with it. Can any one help??
I found the following logical errors in your code:
the result string postfix is empty at the beginning, but you're writing to single character positions using postfix[post++]=. This is not valid and is propably causing the "string related" errors. You should only use postfix.push_back() to add characters to the output string.
In the first inner while loop (while(1)) the last statement should read
postfix.push_back(temp);
since you want to append the operators from the stack to the output.
Your code falsely accept input with unbalanced additional closing parents like "1+4)". Personally, I would put the input position as outer loop condition and verify that the stack is empty after the loop (and check for empty stack in the pop() function) for detecting input errors.
The biggest error is in his pres() function, it should be:
else if (sym == '*' || sym == '/')
else if (sym == '+' || sym == '-')
I have noticed some of the errors mentioned by MartinStettner.