私のプログラムでは、スタックを使用して、プログラミング ステートメントまたは数式の括弧のバランスが取れているかどうかを確認しています。私の人生を除いて、すべてが機能します。ボタンを押してParensをチェックしたときに入力されたのと同じテキストボックスで、Parensのペアをハイライトしてアンバランスにする方法を見つけることができないようです。
参照用の私のコードは次のとおりです。
private void btnCheckParens_Click(object sender, EventArgs e)
{
Stack leftParens = new Stack();
Stack rightParens = new Stack();
string expression = txtParens.Text;
string ch;
int indexOfParens;
for ( int i = 0; i < expression.Length; i++)
{
ch = expression.Substring(i,1);
if (isParenthesis(ch))
{
if (ch == "(")
leftParens.Push(ch);
else
rightParens.Push(ch);
}
}
if (!(leftParens.Count == rightParens.Count))
{
if (leftParens.Count > rightParens.Count)
{
indexOfParens = expression.LastIndexOf("(");
txtParens.SelectionStart = indexOfParens;
txtParens.SelectionLength = 1;
}
else
indexOfParens = expression.LastIndexOf(")");
txtParens.SelectionStart = indexOfParens;
txtParens.SelectionLength = 1;
}
else
MessageBox.Show("Number of parens are balanced!","success");
}
static bool isParenthesis(string ch) { bool フラグ; if ( ch == "(" || ch == ")") フラグ = true; 他のフラグ = false; 戻りフラグ; }