Advice: learn to indent your code, it helps reading. Comments is a good idea too.
Do not pack things on same line, it just make things more confuse with no gain.
#include<iostream>
#include<string>
using namespace std;
struct stack{
int arr[100];
int top=-1;
void push(int val){
if(top==99)return;
top++;
arr[top]=val;
}
void pool(){
if(is_empty())return;
top--;
}
int top_val(){
if(is_empty())return -1;
return arr[top];
}
bool is_empty(){
if(top==-1)return true;
else return false;
}
};
bool 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 balance(string exp){
stack bracket;
for(int i=1;i<=exp.length();i++){
if(exp=='('||exp=='['||exp=='{'){
bracket.push(exp);
}
else if (exp==')'||exp==']'||exp=='}'){
if(bracket.is_empty())return false;
else if(pair(bracket.top_val(),exp)==false) return false;
bracket.pool();
}
if(bracket.is_empty())return true ;
else return false;
}
}
int main(){
string s;
cin>>s;
if(balance(s))cout<<"the brackets is balance \n";
else cout<<"the brackets isn't balance";
}
Professional programmer's editors have this feature along with other useful ones.
Notepad++ Home[
^]
UltraEdit | The Original Text Editor[
^]
Quote:
i think the problem in [bool pair]
Debugging code is not a matter of guessing, use the debugger and see your code perform.
There is a tool that allow you to see what your code is doing, its name is
debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is
debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[
^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[
^]
Basic Debugging with Visual Studio 2010 - YouTube[
^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.