[C ++]我遇到了代码C ++的问题 [英] [C++] I have a problem with code C++

查看:61
本文介绍了[C ++]我遇到了代码C ++的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为[bool对]中的问题

但我无法解决它

请帮帮我



[ ^ ]



i think the problem in [bool pair]
but I could not solve it
please help me

[^]

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





我的尝试:



i认为[bool对]中的问题

但我无法解决它

请帮帮我



What I have tried:

i think the problem in [bool pair]
but I could not solve it
please help me

推荐答案

建议:学会缩进你的代码,它有助于阅读。评论也是一个好主意。

不要把东西放在同一条线上,只会让事情变得更加混乱而没有收获。

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";
}



专业程序员的编辑有这个功能以及其他有用的功能。

Notepad ++主页 [ ^ ]

UltraEdit |原始文本编辑器 [ ^ ]


Professional programmer's editors have this feature along with other useful ones.
Notepad++ Home[^]
UltraEdit | The Original Text Editor[^]

引用:

我认为[bool pair]中的问题

i think the problem in [bool pair]



调试代码不是问题猜测,使用调试器并查看代码执行情况。

有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


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.


你没有告诉我们你遇到了什么样的问题。



但是你的代码会抛出你应该检查的编译错误来修复你的代码。



看看这个代码部分(我是已正确格式化以使其可读):

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 类型为 std: :字符串。您无法与 char 进行比较(单引号括起来的字符,如'('类型为 char )。类似于调用 bracket.push(exp),它要求int作为参数而不是 std :: string



你可能想迭代你的 exp string。使用 string :: operator [] - C ++参考 [< a href =http://www.cplusplus.com/reference/string/string/operator[]/target =_ blanktitle =New Window> ^ ]访问给定的单个字符位置。



这样做时,你可能会发现下一个错误。使用C / C ++,索引是基于零的。所以你必须从零开始迭代到 - 但不包括 - 字符串的长度。另请参阅上面链接中的示例代码。



另请注意,函数始终在循环内返回。这是一个逻辑错误通常不会被编译器检测到但会导致打印出错误的结果。


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.


这篇关于[C ++]我遇到了代码C ++的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆