c++ 括号匹配
·
#include<iostream>
#include <string>
using namespace std;
class LinkStack
{
char stack[1024];
int top = 1024;
bool gg = true;//判断是否需要进行ok和left
public:
LinkStack() {}
~LinkStack() {}
void push(char x)
{
top--;
stack[top] = x;
}
int pop()
{//删除栈顶数据;
char x = stack[top];
top++;
return x;
}
bool empty()
{
if (top == 1024)
return 1;
return 0;
}
//设计函数Match(……),判断字符串中的圆括号的匹配情况。;
void Match(char* x)
{
int n = 0, i = 0, j = 0;
int b[20], c[20];
while (*x != '\0')
{
char L = '\0';//左括号
char R = '\10';//右括号
char ch = *x;
switch (ch)
{
case'(':
push(ch);
i++;
b[i] = n;
if (j > 0)
j--;
break;
case')':
L = '(';
R = ch;
break;
default:
break;
}
n++;
if (R == ch)//11111111111
{
if (empty() == false)
{
char t = pop();//出栈
i--;
j++;
if (j != 0)
c[j] = n;
}
else
{
cout << "right" << " ";
for (; j > 0; j--)
cout << c[j] << " ";
cout << endl;
gg = false;
}
}
x++;
}
if (gg == true)
{
if (int a = empty() == true)
cout << "ok" << endl;
else
{
cout << "left" << " ";//
for (; i > 0; i--)
cout << b[i] << " ";
cout << endl;
}
}
}
};
int main()
{
char a[1024];
cin >> a;
LinkStack s;
s.Match(a);
return 0;
}
更多推荐
所有评论(0)