Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- namespace LOIS_RGZ
- {
- public partial class Form1 : Form
- {
- private string FilePath;
- private TreeNode<CircleNode> root = new TreeNode<CircleNode>(new CircleNode("S"));
- private string terminal = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789;()\"%:, =- /\n \\";
- private string terminal2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOQRSTUVWXYZ";
- private void Panel1_Paint(object sender, PaintEventArgs e)
- {
- e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
- e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
- root.DrawTree(e.Graphics);
- }
- private void Panel1_Resize(object sender, EventArgs e)
- {
- ArrangeTree();
- }
- private void ArrangeTree()
- {
- using (Graphics gr = panel1.CreateGraphics())
- {
- float xmin = 0, ymin = 0;
- root.Arrange(gr, ref xmin, ref ymin);
- xmin = (panel1.ClientSize.Width - xmin) / 2;
- ymin = (panel1.ClientSize.Height - ymin) / 2;
- root.Arrange(gr, ref xmin, ref ymin);
- }
- panel1.Refresh();
- }
- public Form1()
- {
- InitializeComponent();
- openFileDialog1.Filter = "Text files(*.txt)|*.txt";
- saveFileDialog1.Filter = "Text files(*.txt)|*.txt";
- }
- private void openToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
- return;
- FilePath = openFileDialog1.FileName;
- string fileText = File.ReadAllText(FilePath);
- textBox1.Text = fileText;
- }
- private void saveToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (!string.IsNullOrEmpty(FilePath))
- {
- File.WriteAllText(FilePath, textBox1.Text);
- }
- saveAsToolStripMenuItem_Click(sender, e);
- }
- private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
- return;
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, textBox1.Text);
- }
- /*test*/ private void RecFind(List<string> elements, TreeNode<CircleNode> node)
- {
- var text = textBox1.Text;
- var index = elements.IndexOf("(\"");
- var index2 = elements.LastIndexOf("u");
- if (index != -1)
- {
- var node1 = new TreeNode<CircleNode>(new CircleNode("T"));
- node.AddChild(node1);
- RecFind(GetList(elements, 0, index), node1);
- var node2 = new TreeNode<CircleNode>(new CircleNode("E"));
- RecFind(GetList(elements, index + 1, elements.Count), node2);
- node.AddChild(node2);
- var node3 = new TreeNode<CircleNode>(new CircleNode("\");"));
- node.AddChild(node3);
- }
- else {
- switch (node.Data.Text)
- {
- case "T":
- {
- if (elements[0].Length == 2)
- {
- var node1 = new TreeNode<CircleNode>(new CircleNode(elements[0]));
- node.AddChild(node1);
- }
- else
- {
- var node1 = new TreeNode<CircleNode>(new CircleNode(elements[0][0] + "T"));
- node.AddChild(node1);
- RecFind(elements, node1);
- }
- break;
- }
- case "E":
- {
- var node1 = new TreeNode<CircleNode>(new CircleNode("T"));
- node.AddChild(node1);
- RecFind(elements, node1);
- break;
- }
- default:
- {
- if (node.Data.Text.Length < elements[0].Length)
- {
- var node1 = new TreeNode<CircleNode>(new CircleNode(GetFirstElements(elements[0], node.Data.Text.Length) + "T"));
- node.AddChild(node1);
- RecFind(elements, node1);
- }
- else
- {
- var node1 = new TreeNode<CircleNode>(new CircleNode(elements[0]));
- node.AddChild(node1);
- }
- break;
- }
- }
- }
- }
- private string GetFirstElements(string s, int n)
- {
- var ss = "";
- for(int i=0; i<n; i++)
- {
- ss += s[i];
- }
- return ss;
- }
- private List<string> GetList(List<string> elements, int i1, int i2)
- {
- var list = new List<string>();
- for(int i=i1; i<i2; i++)
- {
- list.Add(elements[i]);
- }
- return list;
- }
- private void closeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- }
- private void linguisticalToolStripMenuItem_Click(object sender, EventArgs e)
- {
- int start = 0;
- string test = "";
- string test2 = "";
- string test3 = "";
- var text = textBox1.Text;
- var elements = text.ToList();
- listBox1.Items.Clear();
- terminal2.ToCharArray();
- for (int i = 0; i < elements.Count; i++)
- {
- try
- {
- if (text.All(v => text.All(v1 => terminal.Contains(v1))))
- {
- if (elements[i] == 'p' && elements[i + 1] == 'r' && elements[i + 2] == 'i' && elements[i + 3] == 'n' && elements[i + 4] == 't' && elements[i + 5] == 'f')
- {
- start = 1;
- i++;
- i++;
- i++;
- i++;
- i++;
- listBox1.Items.Add("printf - Функция вывода информации");
- }
- else
- {
- if (start == 1)
- {
- if (text.IndexOf(';') == text.Length - 1)
- {
- if (text.Where(v => v == ';').Count() <= 1)
- {
- Char charRange = '"';
- Char charRange2 = '"';
- int startIndex = text.IndexOf(charRange);
- int endIndex = text.LastIndexOf(charRange2);
- int length = endIndex - startIndex;
- test = text.Substring(startIndex + 1, length - 1);
- Char charRang1 = ',';
- Char charRang2 = ')';
- int startIndexx = text.IndexOf(charRang1);
- int endIndexx = text.LastIndexOf(charRang2);
- int length2 = endIndexx - startIndexx;
- test2 = text.Substring(startIndexx + 1, length2 - 1);
- Char charRanged = '"';
- Char charRangedd = '"';
- int startIndexd = text.IndexOf(charRanged);
- int endIndexd = text.LastIndexOf(charRangedd);
- int length3 = endIndexd - startIndexd;
- test3 = text.Substring(startIndexd + 1, length3 - 1);
- //printf("test"); ориентир
- //printf("x=%f\n", x); ориентир
- int index = text.IndexOf(',');
- if (index > 0)
- {
- if (elements[i] == '(')
- {
- if (elements[i + 1] != ')')
- {
- listBox1.Items.Add("( - открытие параметров");
- listBox1.Items.Add(test + " - Выводимое строковое значение");
- listBox1.Items.Add(test2 + " - Выводимая переменная");
- listBox1.Items.Add(") - закрытие параметров");
- }
- }
- }
- if (index < 0)
- {
- if (elements[i] == '(')
- {
- if (elements[i + 1] != ')')
- {
- listBox1.Items.Add("( - открытие параметров");
- listBox1.Items.Add(test3 + " -Выводимое строковое значение");
- listBox1.Items.Add(") - закрытие параметров");
- }
- }
- }
- if (elements[i] == ';')
- {
- listBox1.Items.Add("; - Окончание функции");
- }
- }
- else { MessageBox.Show("В строке присутствует более одного символа ;"); break; }
- }
- else { MessageBox.Show("В конце нет символа ;"); break; }
- }
- else { MessageBox.Show("Отсутствует функция printf"); break; }
- }
- }
- else { MessageBox.Show("В строке содержатся недопустимые символы"); break; }
- }
- catch
- {
- }
- }
- }
- /*test*/ private void syntacticToolStripMenuItem_Click(object sender, EventArgs e)
- {
- var text = textBox1.Text;
- var elements = text.Split().ToList();
- root.Children.Clear();
- RecFind(elements, root);
- ArrangeTree();
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- private void закрытьПрограммуToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- }
- interface IDrawable
- {
- // Return the object's needed size.
- SizeF GetSize(Graphics gr, Font font);
- // Draw the object centered at (x, y).
- void Draw(float x, float y, Graphics gr, Pen pen,
- Brush bg_brush, Brush text_brush, Font font);
- }
- class TreeNode<T> where T : IDrawable
- {
- // The data.
- public T Data;
- // Child nodes in the tree.
- public List<TreeNode<T>> Children = new List<TreeNode<T>>();
- // Space to skip horizontally between siblings
- // and vertically between generations.
- private const float Hoffset = 5;
- private const float Voffset = 10;
- // The node's center after arranging.
- private PointF Center;
- // Drawing properties.
- public Font MyFont = null;
- public Pen MyPen = Pens.Black;
- public Brush FontBrush = Brushes.Black;
- public Brush BgBrush = Brushes.White;
- // Constructor.
- public TreeNode(T new_data)
- : this(new_data, new Font("Times New Roman", 12))
- {
- Data = new_data;
- }
- public TreeNode(T new_data, Font fg_font)
- {
- Data = new_data;
- MyFont = fg_font;
- }
- // Add a TreeNode to out Children list.
- public void AddChild(TreeNode<T> child)
- {
- Children.Add(child);
- }
- // Arrange the node and its children in the allowed area.
- // Set xmin to indicate the right edge of our subtree.
- // Set ymin to indicate the bottom edge of our subtree.
- public void Arrange(Graphics gr, ref float xmin, ref float ymin)
- {
- // See how big this node is.
- SizeF my_size = Data.GetSize(gr, MyFont);
- // Recursively arrange our children,
- // allowing room for this node.
- float x = xmin;
- float biggest_ymin = ymin + my_size.Height;
- float subtree_ymin = ymin + my_size.Height + Voffset;
- foreach (TreeNode<T> child in Children)
- {
- // Arrange this child's subtree.
- float child_ymin = subtree_ymin;
- child.Arrange(gr, ref x, ref child_ymin);
- // See if this increases the biggest ymin value.
- if (biggest_ymin < child_ymin) biggest_ymin = child_ymin;
- // Allow room before the next sibling.
- x += Hoffset;
- }
- // Remove the spacing after the last child.
- if (Children.Count > 0) x -= Hoffset;
- // See if this node is wider than the subtree under it.
- float subtree_width = x - xmin;
- if (my_size.Width > subtree_width)
- {
- // Center the subtree under this node.
- // Make the children rearrange themselves
- // moved to center their subtrees.
- x = xmin + (my_size.Width - subtree_width) / 2;
- foreach (TreeNode<T> child in Children)
- {
- // Arrange this child's subtree.
- child.Arrange(gr, ref x, ref subtree_ymin);
- // Allow room before the next sibling.
- x += Hoffset;
- }
- // The subtree's width is this node's width.
- subtree_width = my_size.Width;
- }
- // Set this node's center position.
- Center = new PointF(xmin + subtree_width / 2,
- ymin + my_size.Height / 2);
- // Increase xmin to allow room for
- // the subtree before returning.
- xmin += subtree_width;
- // Set the return value for ymin.
- ymin = biggest_ymin;
- }
- // Draw the subtree rooted at this node
- // with the given upper left corner.
- public void DrawTree(Graphics gr, ref float x, float y)
- {
- // Arrange the tree.
- Arrange(gr, ref x, ref y);
- // Draw the tree.
- DrawTree(gr);
- }
- // Draw the subtree rooted at this node.
- public void DrawTree(Graphics gr)
- {
- // Draw the links.
- DrawSubtreeLinks(gr);
- // Draw the nodes.
- DrawSubtreeNodes(gr);
- }
- // Draw the links for the subtree rooted at this node.
- private void DrawSubtreeLinks(Graphics gr)
- {
- foreach (TreeNode<T> child in Children)
- {
- // Draw the link between this node this child.
- gr.DrawLine(MyPen, Center, child.Center);
- // Recursively make the child draw its subtree nodes.
- child.DrawSubtreeLinks(gr);
- }
- }
- // Draw the nodes for the subtree rooted at this node.
- private void DrawSubtreeNodes(Graphics gr)
- {
- // Draw this node.
- Data.Draw(Center.X, Center.Y, gr, MyPen, BgBrush, FontBrush, MyFont);
- // Recursively make the child draw its subtree nodes.
- foreach (TreeNode<T> child in Children)
- {
- child.DrawSubtreeNodes(gr);
- }
- }
- }
- class CircleNode : IDrawable
- {
- // The string we will draw.
- public string Text;
- public CircleNode(string new_text)
- {
- Text = new_text;
- }
- // Return the size of the string plus a 10 pixel margin.
- public SizeF GetSize(Graphics gr, Font font)
- {
- return gr.MeasureString(Text, font) + new SizeF(10, 10);
- }
- // Draw the object centered at (x, y).
- void IDrawable.Draw(float x, float y, Graphics gr, Pen pen, Brush bg_brush, Brush text_brush, Font font)
- {
- // Fill and draw an ellipse at our location.
- SizeF my_size = GetSize(gr, font);
- RectangleF rect = new RectangleF(
- x - my_size.Width / 2,
- y - my_size.Height / 2,
- my_size.Width, my_size.Height);
- gr.FillEllipse(bg_brush, rect);
- gr.DrawEllipse(pen, rect);
- // Draw the text.
- using (StringFormat string_format = new StringFormat())
- {
- string_format.Alignment = StringAlignment.Center;
- string_format.LineAlignment = StringAlignment.Center;
- gr.DrawString(Text, font, text_brush, x, y, string_format);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement