括号的使用规则:大括号,中括号,小括号{[()]};中括号,小括号[()];小括号();大括号、中括号、小括号、中括号、小括号、大括号{[()][()]};大括号,中括号,小括号,小括号{[(())]};大括号,中括号,小括号,小括号{[()()]};小括号不能嵌套,小括号可连续使用。
{[]}、{()}、([])、({})、[{}]、{}、[]、{[}]、[(])都属非法。
char aa[]{"[]{}()"}, aaa[47]{}; cin >> aaa;//缘由https://bbs.csdn.net/topics/396213711
int a[6]{0}, j = 0, jj = 0, z = sizeof(aaa) / sizeof(aaa[0]);
while (j < z && aaa[j] != '\0')if (jj < 6)if (aaa[j] == aa[jj])++a[jj++]; else ++jj; else jj = 0, ++j;
cout << (a[0] == a[1] && a[2] == a[3] && a[4] == a[5]) << endl;
使用数组设计了2种逻辑,小学算术先小括号始中括号大括号,未使用小括号就使用中大括号为非法,大学逻辑或编程逻辑只检查顺序对称不分括号类型,代码留着到适合应用场景时用,免日后再写.检查数量对称,优先级顺序,左右对称如下:
void 小学括号对称()
{//缘由https://bbs.csdn.net/topics/396213711
char aa[]{"{}[]()"}, aaa[47]{}; cin >> aaa;
int j = 0, jj[3]{0}, jc = 0;
bool k = true;
while (aaa[j] != '\0')
{
if (aaa[j] == aa[0])jc = jj[0] += 1;
else if (aaa[j] == aa[2])jc = jj[1] += 1;
else if (aaa[j] == aa[4])jc = jj[2] += 1;//+嵌套限制
else if (aaa[j] == aa[1])k = (jc == 1), (jj[1] && k ? jc = jj[1]/*, jj[0] = 0 : 0*/ : 0);
else if (aaa[j] == aa[3])k = (jc == 1), (jj[2] && k ? jc = jj[0], (jj[0] ? 0 : jc = jj[2])/*, jj[1] = 0*/ : 0);
else if (aaa[j] == aa[5])k = (jc == 1), (k ? jc = jj[1], (jj[1] ? 0 : jc = jj[0])/*, jj[2] = 0*/ : 0);
if (k)++j; else break;
}
cout << k << endl;
}
void 大学括号对称()
{//缘由https://bbs.csdn.net/topics/396213711
char aa[]{"{}[]()"}, zj[447]{}, aaa[447]{}; cin >> aaa;
int j = 0, jj = 0;
bool k = true;
while (aaa[j] != '\0')
{
if (aaa[j] == aa[0])zj[jj++] = aa[1];
else if (aaa[j] == aa[2])zj[jj++] = aa[3];
else if (aaa[j] == aa[4])zj[jj++] = aa[5];
else if (aaa[j] == zj[jj - 1])zj[--jj] = 0;
else k = false;
if (k)++j; else break;
}
if (zj[0] != '\0')k = false;
cout << k << endl;
}
void 括号对称比较()
{//缘由https://bbs.csdn.net/topics/396213711
char aa[]{"{}[]()"}, aaa[47]{}; cin >> aaa;
int j = 0, jj[3]{0}, jc = 0;
bool k = true;
while (aaa[j] != '\0')
{
if (aaa[j] == aa[0])jc = jj[0] += 4;
else if (aaa[j] == aa[2])jc = jj[1] += 7;
else if (aaa[j] == aa[4])jc = jj[2] += 9;//+嵌套限制
else if (aaa[j] == aa[1])
k = (jc == 1), (k ? jc = jj[0] -= 1 : 0);
else if (aaa[j] == aa[3])
k = (jc == 3), (k ? jc = jj[1] -= 3, jc ? 0 : jc = jj[0] : 0);
else if (aaa[j] == aa[5])
k = (jc == 5), (k ? jc = jj[2] -= 5, jc ? 0 : jj[1] ? jc = jj[1] : jc = jj[0] : 0);
if (k)++j; else break;
}
cout << k << endl;
}