询问c++中的括号匹配
Asking about the parenthesis matching in c++
我现在正在做我的第一个数据结构作业
主题是"括号匹配"
我心里的答案很简单,
只需将左括号推入堆栈,当遇到右括号时将其弹出。
完成我的代码后,我提交给我们学校的在线评判系统
10道题只对9道。
这是我的c++代码
是否有任何情况,我错过了这段代码??谢谢大家!
/的/问题问题是输入一个整数N <1000意味着测试用例
及之后的N个字符串长度<如果它是一个有效的字符串>如果它是一个有效的字符串>
,应该测试1000。如果是,输出大小写N(1~N): yes
No,输出大小写N(从1~N):No
字符串可以包含换行符
#测试用例输入2[] [] & lt ;>()[{}]
& lt; {>}
输出案例1:Yes
案例2:No
#include <iostream>
#include <string>
using namespace std;
class PARENTHE
{
public:
PARENTHE(int slength);
~PARENTHE();
int StackSize() const;
bool StackEmpty() const;
char top() const;
void Push(const char);
void Pop();
private:
char *str;
int slength;
int stop;
};
PARENTHE::PARENTHE(int slength)
{
str = new char [slength];
stop = -1;
}
PARENTHE::~PARENTHE()
{ delete [] str; }
inline int PARENTHE::StackSize() const
{ return stop+1; }
inline bool PARENTHE::StackEmpty() const
{
return (stop == -1);
}
inline char PARENTHE::top() const
{ return str[stop]; }
void PARENTHE::Push(const char c)
{
str[++stop] = c;
}
void PARENTHE::Pop()
{
stop--;
}
int main()
{
int t;
while( cin>>t )
{
int i = 0;
while( i < t )
{
string temp;
cin>>temp;
if(temp == "n")
{
cout<<"Case "<<++i<<": "<<"Yes"<<endl;
break;
}
PARENTHE S( 1001 );
bool check = false;
for( int it = 0; it < temp.size() ; ++it )
{
if( temp[it] == '{' || temp[it] == '[' || temp[it] == '(' || temp[it] == '<' )
S.Push(temp[it]);
else if ( temp[it] == '}' || temp[it] == ']' || temp[it] == '>' || temp[it] == ')' )
{
if(!S.StackEmpty())
{
if(( S.top() == '{' && temp[it] == '}' ) || ( S.top() == '(' && temp[it] == ')' ) || ( S.top() == '[' && temp[it] == ']' ) || ( S.top() == '<' && temp[it] == '>' ) )
{
S.Pop();
}
else { break; }
}
else { break; }
}
if ( it == (temp.size()-1) && (!S.StackSize()))
{
check = true;
}
}
if(check)
cout<<"Case "<<++i<<": "<<"Yes"<<endl;
else
cout<<"Case "<<++i<<": "<<"No"<<endl;
}
}
return 0;
}
我建议你检查一下反向润色符号,它会解决你所有的问题。
相关文章:
- 询问在设计我的手臂模拟器功能表示格式1
- 为什么如果我添加这一行,我的程序会不断询问值
- 如何多次询问来自QML对话框的输入?
- 在C++的头文件中使用常量并在程序中询问其地址的任何潜在危险
- C++结构编译器错误询问向量的迭代器
- GLSL 统一 ivec 未设置,并且在询问时具有默认(错误)值
- 从主线程中询问运行对象表 (ROT) 时出错
- C .如何通过在交换机案例中再次询问用户名来找到密码
- C :如何询问文件名并打印字符数组
- C :询问用户输入而无需按使用汇编按ENTER
- 在没有程序的情况下询问用户相同的事情被终止 C++
- 关于Visual Studio 2017中未发现错误的精致询问
- 在询问其内容之前,请创建文件
- 从C 进行prolog询问
- QML:在关闭应用程序之前询问确认
- 在运行时询问变体中的替代是否满足某个概念
- 在另一个线程上询问qsqlquery
- 使用类询问数字
- C++VCL询问.xlsb Excel数据库
- 使用全能客户端询问值