Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i think the problem in [bool pair]
but I could not solve it
please help me

[^]

C++
#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";
}


What I have tried:

i think the problem in [bool pair]
but I could not solve it
please help me
Posted
Updated 23-Jan-18 4:15am
Comments
Leo Chapiro 23-Jan-18 10:02am    
Try to debug your source code and to localize where the error is

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.
C++
#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.
 
Share this answer
 
Comments
M.Gehad 23-Jan-18 10:27am    
ill try debugger now
You did not told us what kind of problem you have.

But your code will throw compilation errors that you should inspect to fix your code.

Just have a look at this code portion (which I have properly formatted to make it readable):
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;
    }
}

exp is of type std::string. You can't compare such with chars (characters enclosed by single quotes like '(' are of type char). Similar applies to calling bracket.push(exp) which expects an int as argument instead of a std::string.

You probably want to iterate over the characters in your exp string. Use the string::operator[] - C++ Reference[^] to access single characters at a given position.

When doing so, you might recognise the next error. With C/C++, indexes are zero based. So you have to iterate starting at zero up to - but excluding - the length of the string. See also the example code from the above link.

Note also that the function always returns within the loop. That is a logic error which is usually not detected by the compiler but will result in wrong results printed out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900