我无法理解它的工作原理,在button_onclick事件的开头我调用textBox.Clear(),即我希望每次单击此按钮时,文本框都会被清除,但如果我不关闭表单,则不会发生这种情况,输出结果会累积。在这段代码中:
private void but_OK_Click(object sender, EventArgs e)
{
txt_output.Clear();
string str_input = txt_input.Text;
Format_Rule(str_input);
for (int i = 0; i < str_in.Count; i++)
{
txt_output.Text += str_in.GetKey(i) + " " + str_in.Get(i) + Environment.NewLine;
}
}
这是所有代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ТЯП_Лаб1
{
public partial class Form1 : Form
{
NameValueCollection str_in = new NameValueCollection();
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string alpha_number = "0123456789";
string alpha_operand = "-+*/";
private bool Search_Operand(string str)
{
for (int i = 0; i < alpha_operand.Length; i++)
{
if (str.Contains(alpha_operand[i]))
{
return true;
}
}
return false;
}
private bool Search_Number(string str)
{
for (int i = 0; i < alpha_number.Length; i++)
{
if (str.Contains(alpha_number[i]))
{
return true;
}
}
return false;
}
private bool Search_Word(string str)
{
for (int i = 0; i < alphabet.Length; i++)
{
if (str.Contains(alphabet[i]))
{
return true;
}
}
return false;
}
private void Format_Rule(string str )
{
string str_oper = "";
string str_set = "";
if (!Search_Operand(str))
{
for (int i = 0; i < str.Length; i++)
{
if(i == str.Length)
str_set += str[i];
else
str_set += str[i] + " | ";
}
str_in.Add("S->", "T");
str_in.Add("T->", "T | TF");
str_in.Add("F->", str_set);
}
else
{
for (int i = 0; i < str.Length; i++)
{
if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
if(i != str.Length)
str_oper += str[i] + "T" + " | ";
else
str_oper += str[i] + "T";
}
else
{
if(i != str.Length)
str_set += str[i] + " | ";
else
str_set += str[i];
}
}
str_in.Add("S->", str_oper);
str_in.Add("T->", "T | TF");
str_in.Add("F->", str_set);
}
}
public Form1()
{
InitializeComponent();
}
private void but_OK_Click(object sender, EventArgs e)
{
txt_output.Clear();
string str_input = txt_input.Text;
Format_Rule(str_input);
for (int i = 0; i < str_in.Count; i++)
{
txt_output.Text += str_in.GetKey(i) + " " + str_in.Get(i) + Environment.NewLine;
}
}
}
}




