📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Lexeme
{
public partial class mainForm : Form
{
/// <summary>
/// 定义5种类型的字符集合,分别为基本字,界符,操作符,常量和变量
/// </summary>
public class PrimaryWord
{
public string[,] primaryword = new string[13, 2] { { "begin", "1" }, { "end", "2" } ,
{"ood","3"},{"if","4"},{"then","5"},
{"call","6"},{"while","7"},{"do","8"},
{"read","9"},{"const","10"},{"var","11"},
{"procedure","12"},{"write","13"}};
public string this[string s]//索引器
{
get
{
for (int i = 0; i < 13; i++)
{
if (primaryword[i, 0] == s) return primaryword[i, 1];
}
return "Error";
}
}
}
public class Delimiter
{
public string[,] delimiter = new string[7, 2] { { ";", "1" }, { ":=", "2" } ,
{",","3"},{"(","4"},{")","5"},{ ":", "6" },
{".","7"}};
public string this[string s]//索引器
{
get
{
for (int i = 0; i < 6; i++)
{
if (delimiter[i, 0] == s) return delimiter[i, 1];
}
return "Error";
}
}
}
public class Operato
{
public string[,] operato = new string[10, 2] { { "+", "1" }, { "-", "2" } ,
{"*","3"},{"/","4"},{"<=","5"},
{ ">=", "6" },{"<","7"},{">","8"},
{"=","9"},{"#","10"}};
public string this[string s]//索引器
{
get
{
for (int i = 0; i < 10; i++)
{
if (operato[i, 0] == s) return operato[i, 1];
}
return "Error";
}
}
}
public class ConstVal
{
public string[,] constval = new string[100, 2];
}
public class Varval
{
public string[,] varval = new string[100, 2];
}
public PrimaryWord pw = new PrimaryWord();
public Delimiter dl = new Delimiter();
public Operato op = new Operato();
public ConstVal cv = new ConstVal();
public Varval vv = new Varval();
public static int constCount = 0, varCount = 0, wordCount = 0;//constCount为常量的个数,varCount为变量个数,wordCount为整个程序里字符的个数
public static int end = 0, start = 0;//开始表示和结束标志,做指针用
public static string inText,outText;//存储导入的程序字符串
public static string[,] resultArr = new string[100,3];//保存区分后的每种字符
public DataTable dt;
public DataColumn dc;
public DataRow dr;
public void reset()//重置参数
{
constCount = 0;
varCount = 0;
wordCount = 0;
end = 0;
start = 0;
inText = "";
}
public mainForm()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Timebar.Value < 100)//进度条增加
{
Timebar.Value++;
}
else if (Timebar.Value == 100)
{
Result.Text = outText;
timer1.Enabled = false;
Classify();//调用区分词种函数
Matching();
Print();
}
}
private void mainForm_Load(object sender, EventArgs e)
{
//创建内存表
dt = new DataTable();
dc = new DataColumn();
dc.ColumnName = "Word";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Class";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Kind";
dt.Columns.Add(dc);
dataGridView1.DataSource = dt;
}
private void Import_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Text File(*.txt)|*.txt";
openFileDialog1.Title = "Please import your code";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader input1 = new System.IO.StreamReader(openFileDialog1.FileName);
importText.Text = input1.ReadToEnd();//去除字符串中的回车
input1.Close();
reset();
}
System.IO.StreamReader input2 = new System.IO.StreamReader(openFileDialog1.FileName);
inText = input2.ReadToEnd().Replace("\r", "").Replace("\n", " ");
input2.Close();
}
private void Analysis_Click(object sender, EventArgs e)
{
if (Timebar.Value == 100)
Timebar.Value = 0;
timer1.Enabled = true;
}
private void Save_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "Text File(*.txt)|*.txt";
saveFileDialog1.Title = "Please save your analysis result";
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
System.IO.StreamWriter output = new System.IO.StreamWriter(saveFileDialog1.FileName);
output.Write(outText);
output.Close();
}
}
public void Classify()
{
int x=0;//记录end位置,因为在判断是否为字母和数字时,end不能处于文本尾部
while (end <= inText.Length)
{
if (end != inText.Length)
{
x = end;
}
if ((char.IsLetter(inText, x) | char.IsDigit(inText, x)) & end != inText.Length)//判断end位置是否为数字或字母,如是则end++
{
end++;
continue;
}
else if (end == inText.Length)//当end已指向文本尾部时,将当前start到end的字符存入数组
{
resultArr[wordCount,0] = inText.Substring(start, end - start);
wordCount++;
end++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -