本文主要是介绍VSC++: 括号对称比较,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
括号的使用规则:大括号,中括号,小括号{[()]};中括号,小括号[()];小括号();大括号、中括号、小括号、中括号、小括号、大括号{[()][()]};大括号,中括号,小括号,小括号{[(())]};大括号,中括号,小括号,小括号{[()()]};小括号不能嵌套,小括号可连续使用。
{[]}、{()}、([])、({})、[{}]、{}、[]、{[}]、[(])都属非法。
char aa[]{"[]{}()"}, aaa[47]{}; cin >> aaa;//缘由https://bbs.csdn.net/topics/396213711int 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/396213711char 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/396213711char 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/396213711char 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;
}
这篇关于VSC++: 括号对称比较的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!