📄 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 Windowsanalyz
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void but_Click(object sender, EventArgs e)
{
string mm = tbox1.Text;
char[] c = mm.ToCharArray();
shur shu = new shur();
char[] cc = shu.cutf(c);
string outp1;
outp1 = shu.worday(cc);
tbox2.Text = outp1;
}
private void button1_Click(object sender, EventArgs e)
{
tbox1.Text = " ";
}
private void button3_Click(object sender, EventArgs e)
{
tbox2.Text = " ";
}
private void open1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
openFileDialog1.Title = "打开要分析的源文件。";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
tbox1.Text = sr.ReadToEnd();
sr.Close();
}
}
private void save1_Click(object sender, EventArgs e)
{
if (tbox1.Text == "")
{
MessageBox.Show(" 您正在试图导出一片空白," + "\n" +
"而事实上似乎没有这个必要," + "\n" +
"请先分析表达式后," + "\n" +
"再进行导出操作!" + "\n" +
" ok?" + "\n",
"不必要的操作",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "保存分析结果.";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter wr = new
System.IO.StreamWriter(saveFileDialog1.FileName);
wr.Write(tbox1.Text);
wr.Close();
}
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
openFileDialog1.Title = "打开要分析的源文件。";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
tbox1.Text = sr.ReadToEnd();
sr.Close();
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (tbox1.Text == "")
{
MessageBox.Show(" 您正在试图导出一片空白," + "\n" +
"而事实上似乎没有这个必要," + "\n" +
"请先分析表达式后," + "\n" +
"再进行导出操作!" + "\n" +
" ok?" + "\n",
"不必要的操作",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "保存分析结果.";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter wr = new
System.IO.StreamWriter(saveFileDialog1.FileName);
wr.Write(tbox1.Text);
wr.Close();
}
}
}
}
public class shur
{
string[] strarry = new string[20] { "if", "break", "case" ,"char","const",
"do","double","else",
"float","for","goto",
"int","long","return",
"short","struct",
"switch","void",
"while ","stdio.h"};
public char[] cutf(char[] c1)
{
int i, j;
char[] c2 = new char[100];
for (i = 0, j = 0; j < c1.Length; j++)
{
if (c1[j] != '\n' && c1[j] != ' ' && c1[j] != '\r')
{
c2[i] = c1[j];
i++;
}
}
c2[i] = '\0';
return (c2);
}
public string worday(char[] cc)
{
System.Collections.ArrayList pp = new System.Collections.ArrayList();
int j = 0;
for (; cc[j] != '\0'; j++)
{
word word1 = new word();
word1 = fenxi(cc, ref j);
pp.Add(word1);
}
string outp = "";
word word4 = new word();
for (int a = 0; a < pp.Count; a++)
{
word4 = (word)pp[a];
outp += word4.type1 + "\t" + word4.danci1 + "\r" + "\n";
}
return (outp);
}
public word fenxi(char[] cc, ref int i)
{
int begin;
int end;
word word2 = new word();
if (('A' <= cc[i]) && (cc[i] <= 'Z') || ('a' <= cc[i]) && (cc[i] <= 'z') || cc[i] == '_')
{
begin = i;
i++;
while (cc[i] != '\0')
{
if (('A' <= cc[i]) && (cc[i] <= 'Z') || ('a' <= cc[i]) && (cc[i] <= 'z') || cc[i] == '_')
{
end = i;
word2 = comp(cc, begin, end, 3);
if (word2.danci1 != "#")
return (word2);
i++;
}
else
{
i--;
end = i;
word2 = add(cc, begin, end, 2);
break;
}
}
if (cc[i] == '\0')
{
i--;
end = i;
word2 = add(cc, begin, end, 2);
}
}
switch (cc[i])
{
case '(':
case ')':
case '{':
case '}':
case ',':
case ';':
case '"':
case '#':
case '[':
case ']':
case '*':
case '/': begin = i;
end = i;
word2 = add(cc, begin, end, 0);
break;
case '+': i++;
if (cc[i] == '+' || cc[i] == '=')
{
i--;
begin = i;
i++;
end = i;
word2 = add(cc, begin, end, 0);
break;
}
else
{
i--;
begin = i;
end = i;
word2 = add(cc, begin, end, 0);
break;
}
case '-': i++;
if (cc[i] == '-' || cc[i] == '=')
{
i--;
begin = i;
i++;
end = i;
word2 = add(cc, begin, end, 0);
break;
}
else
{
i--;
begin = i;
end = i;
word2 = add(cc, begin, end, 0);
break;
}
case '=':
case '>':
case '<': i++;
if (cc[i] == '=')
{
i--;
begin = i;
i++;
end = i;
word2 = add(cc, begin, end, 0);
break;
}
else
{
i--;
begin = i;
end = i;
word2 = add(cc, begin, end, 0);
break;
}
}
if (('0' <= cc[i]) && (cc[i] <= '9'))
{
begin = i;
i++;
while (cc[i] != '\0')
{
if (('0' <= cc[i]) && (cc[i] <= '9'))
{
i++;
}
else
{
i--;
end = i;
word2 = add(cc, begin, end, 1);
break;
}
}
if (cc[i] == '\0')
{
end = i;
word2 = add(cc, begin, end, 1);
}
}
return (word2);
}
public word add(char[] c, int nbegin, int nend, int ntype)
{
word word3 = new word();
char[] b = new char[10];
string output = " ";
for (; nbegin <= nend; nbegin++)
{
output += c[nbegin];
}
word3.type1 = ntype;
word3.danci1 = output;
return (word3);
}
public word comp(char[] c, int nbegin, int nend, int ntype)
{
word word5 = new word();
char[] b = new char[10];
string output = "";
int m;
for (; nbegin <= nend; nbegin++)
{
output += c[nbegin];
}
for (int i = 0; i < 20; )
{
m = string.Compare(output, strarry[i]);
if (m != 0)
i++;
if (m == 0)
{
word5.type1 = ntype;
word5.danci1 = output;
return (word5);
}
}
word5.type1 = 0;
word5.danci1 = "#";
return (word5); ;
}
}
public struct word
{
int type;
string danci;
public int type1
{
get { return type; }
set { type = value; }
}
public string danci1
{
get { return danci; }
set { danci = value; }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -