Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Drawing.Drawing2D;
  12. using System.Drawing.Text;
  13.  
  14. namespace LOIS_RGZ
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private string FilePath;
  19.  
  20. private TreeNode<CircleNode> root = new TreeNode<CircleNode>(new CircleNode("S"));
  21.  
  22. private string terminal = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789;()\"%:, =- /\n \\";
  23. private string terminal2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOQRSTUVWXYZ";
  24.  
  25. private void Panel1_Paint(object sender, PaintEventArgs e)
  26. {
  27. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  28. e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  29. root.DrawTree(e.Graphics);
  30. }
  31. private void Panel1_Resize(object sender, EventArgs e)
  32. {
  33. ArrangeTree();
  34. }
  35. private void ArrangeTree()
  36. {
  37. using (Graphics gr = panel1.CreateGraphics())
  38. {
  39. float xmin = 0, ymin = 0;
  40. root.Arrange(gr, ref xmin, ref ymin);
  41. xmin = (panel1.ClientSize.Width - xmin) / 2;
  42. ymin = (panel1.ClientSize.Height - ymin) / 2;
  43. root.Arrange(gr, ref xmin, ref ymin);
  44. }
  45. panel1.Refresh();
  46. }
  47.  
  48. public Form1()
  49. {
  50. InitializeComponent();
  51. openFileDialog1.Filter = "Text files(*.txt)|*.txt";
  52. saveFileDialog1.Filter = "Text files(*.txt)|*.txt";
  53. }
  54.  
  55. private void openToolStripMenuItem_Click(object sender, EventArgs e)
  56. {
  57. if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
  58. return;
  59.  
  60. FilePath = openFileDialog1.FileName;
  61. string fileText = File.ReadAllText(FilePath);
  62. textBox1.Text = fileText;
  63. }
  64.  
  65. private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  66. {
  67. if (!string.IsNullOrEmpty(FilePath))
  68. {
  69. File.WriteAllText(FilePath, textBox1.Text);
  70. }
  71. saveAsToolStripMenuItem_Click(sender, e);
  72. }
  73.  
  74. private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
  75. {
  76. if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
  77. return;
  78.  
  79. string filename = saveFileDialog1.FileName;
  80. File.WriteAllText(filename, textBox1.Text);
  81. }
  82.  
  83.  
  84.  
  85. /*test*/ private void RecFind(List<string> elements, TreeNode<CircleNode> node)
  86. {
  87. var text = textBox1.Text;
  88. var index = elements.IndexOf("(\"");
  89. var index2 = elements.LastIndexOf("u");
  90. if (index != -1)
  91. {
  92.  
  93.  
  94.  
  95.  
  96.  
  97. var node1 = new TreeNode<CircleNode>(new CircleNode("T"));
  98. node.AddChild(node1);
  99. RecFind(GetList(elements, 0, index), node1);
  100. var node2 = new TreeNode<CircleNode>(new CircleNode("E"));
  101. RecFind(GetList(elements, index + 1, elements.Count), node2);
  102. node.AddChild(node2);
  103. var node3 = new TreeNode<CircleNode>(new CircleNode("\");"));
  104. node.AddChild(node3);
  105.  
  106. }
  107.  
  108. else {
  109. switch (node.Data.Text)
  110. {
  111. case "T":
  112. {
  113. if (elements[0].Length == 2)
  114. {
  115. var node1 = new TreeNode<CircleNode>(new CircleNode(elements[0]));
  116. node.AddChild(node1);
  117. }
  118. else
  119. {
  120. var node1 = new TreeNode<CircleNode>(new CircleNode(elements[0][0] + "T"));
  121. node.AddChild(node1);
  122. RecFind(elements, node1);
  123. }
  124. break;
  125. }
  126. case "E":
  127. {
  128. var node1 = new TreeNode<CircleNode>(new CircleNode("T"));
  129. node.AddChild(node1);
  130. RecFind(elements, node1);
  131. break;
  132. }
  133. default:
  134. {
  135. if (node.Data.Text.Length < elements[0].Length)
  136. {
  137. var node1 = new TreeNode<CircleNode>(new CircleNode(GetFirstElements(elements[0], node.Data.Text.Length) + "T"));
  138. node.AddChild(node1);
  139. RecFind(elements, node1);
  140. }
  141. else
  142. {
  143. var node1 = new TreeNode<CircleNode>(new CircleNode(elements[0]));
  144. node.AddChild(node1);
  145. }
  146. break;
  147. }
  148. }
  149.  
  150. }
  151. }
  152.  
  153. private string GetFirstElements(string s, int n)
  154. {
  155. var ss = "";
  156. for(int i=0; i<n; i++)
  157. {
  158. ss += s[i];
  159. }
  160. return ss;
  161. }
  162.  
  163. private List<string> GetList(List<string> elements, int i1, int i2)
  164. {
  165. var list = new List<string>();
  166. for(int i=i1; i<i2; i++)
  167. {
  168. list.Add(elements[i]);
  169. }
  170. return list;
  171. }
  172.  
  173. private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  174. {
  175.  
  176. }
  177.  
  178. private void linguisticalToolStripMenuItem_Click(object sender, EventArgs e)
  179. {
  180. int start = 0;
  181. string test = "";
  182. string test2 = "";
  183. string test3 = "";
  184. var text = textBox1.Text;
  185. var elements = text.ToList();
  186. listBox1.Items.Clear();
  187. terminal2.ToCharArray();
  188. for (int i = 0; i < elements.Count; i++)
  189. {
  190. try
  191. {
  192. if (text.All(v => text.All(v1 => terminal.Contains(v1))))
  193. {
  194. if (elements[i] == 'p' && elements[i + 1] == 'r' && elements[i + 2] == 'i' && elements[i + 3] == 'n' && elements[i + 4] == 't' && elements[i + 5] == 'f')
  195. {
  196. start = 1;
  197. i++;
  198. i++;
  199. i++;
  200. i++;
  201. i++;
  202. listBox1.Items.Add("printf - Функция вывода информации");
  203. }
  204. else
  205. {
  206. if (start == 1)
  207. {
  208. if (text.IndexOf(';') == text.Length - 1)
  209. {
  210. if (text.Where(v => v == ';').Count() <= 1)
  211. {
  212. Char charRange = '"';
  213. Char charRange2 = '"';
  214. int startIndex = text.IndexOf(charRange);
  215. int endIndex = text.LastIndexOf(charRange2);
  216. int length = endIndex - startIndex;
  217. test = text.Substring(startIndex + 1, length - 1);
  218.  
  219. Char charRang1 = ',';
  220. Char charRang2 = ')';
  221. int startIndexx = text.IndexOf(charRang1);
  222. int endIndexx = text.LastIndexOf(charRang2);
  223. int length2 = endIndexx - startIndexx;
  224. test2 = text.Substring(startIndexx + 1, length2 - 1);
  225.  
  226. Char charRanged = '"';
  227. Char charRangedd = '"';
  228. int startIndexd = text.IndexOf(charRanged);
  229. int endIndexd = text.LastIndexOf(charRangedd);
  230. int length3 = endIndexd - startIndexd;
  231. test3 = text.Substring(startIndexd + 1, length3 - 1);
  232. //printf("test"); ориентир
  233. //printf("x=%f\n", x); ориентир
  234. int index = text.IndexOf(',');
  235. if (index > 0)
  236. {
  237. if (elements[i] == '(')
  238. {
  239. if (elements[i + 1] != ')')
  240. {
  241. listBox1.Items.Add("( - открытие параметров");
  242. listBox1.Items.Add(test + " - Выводимое строковое значение");
  243. listBox1.Items.Add(test2 + " - Выводимая переменная");
  244. listBox1.Items.Add(") - закрытие параметров");
  245.  
  246. }
  247. }
  248. }
  249. if (index < 0)
  250. {
  251. if (elements[i] == '(')
  252. {
  253. if (elements[i + 1] != ')')
  254. {
  255. listBox1.Items.Add("( - открытие параметров");
  256. listBox1.Items.Add(test3 + " -Выводимое строковое значение");
  257. listBox1.Items.Add(") - закрытие параметров");
  258.  
  259. }
  260. }
  261. }
  262. if (elements[i] == ';')
  263. {
  264. listBox1.Items.Add("; - Окончание функции");
  265.  
  266.  
  267. }
  268. }
  269. else { MessageBox.Show("В строке присутствует более одного символа ;"); break; }
  270. }
  271. else { MessageBox.Show("В конце нет символа ;"); break; }
  272. }
  273. else { MessageBox.Show("Отсутствует функция printf"); break; }
  274. }
  275. }
  276. else { MessageBox.Show("В строке содержатся недопустимые символы"); break; }
  277. }
  278. catch
  279. {
  280.  
  281. }
  282.  
  283. }
  284. }
  285. /*test*/ private void syntacticToolStripMenuItem_Click(object sender, EventArgs e)
  286. {
  287. var text = textBox1.Text;
  288. var elements = text.Split().ToList();
  289.  
  290. root.Children.Clear();
  291. RecFind(elements, root);
  292. ArrangeTree();
  293. }
  294.  
  295. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  296. {
  297.  
  298. }
  299.  
  300. private void закрытьПрограммуToolStripMenuItem_Click(object sender, EventArgs e)
  301. {
  302. this.Close();
  303. }
  304. }
  305.  
  306.  
  307. interface IDrawable
  308. {
  309. // Return the object's needed size.
  310. SizeF GetSize(Graphics gr, Font font);
  311. // Draw the object centered at (x, y).
  312. void Draw(float x, float y, Graphics gr, Pen pen,
  313. Brush bg_brush, Brush text_brush, Font font);
  314. }
  315. class TreeNode<T> where T : IDrawable
  316. {
  317. // The data.
  318. public T Data;
  319. // Child nodes in the tree.
  320. public List<TreeNode<T>> Children = new List<TreeNode<T>>();
  321. // Space to skip horizontally between siblings
  322. // and vertically between generations.
  323. private const float Hoffset = 5;
  324. private const float Voffset = 10;
  325. // The node's center after arranging.
  326. private PointF Center;
  327. // Drawing properties.
  328. public Font MyFont = null;
  329. public Pen MyPen = Pens.Black;
  330. public Brush FontBrush = Brushes.Black;
  331. public Brush BgBrush = Brushes.White;
  332. // Constructor.
  333. public TreeNode(T new_data)
  334. : this(new_data, new Font("Times New Roman", 12))
  335. {
  336. Data = new_data;
  337. }
  338. public TreeNode(T new_data, Font fg_font)
  339. {
  340. Data = new_data;
  341. MyFont = fg_font;
  342. }
  343. // Add a TreeNode to out Children list.
  344. public void AddChild(TreeNode<T> child)
  345. {
  346. Children.Add(child);
  347. }
  348. // Arrange the node and its children in the allowed area.
  349. // Set xmin to indicate the right edge of our subtree.
  350. // Set ymin to indicate the bottom edge of our subtree.
  351. public void Arrange(Graphics gr, ref float xmin, ref float ymin)
  352. {
  353. // See how big this node is.
  354. SizeF my_size = Data.GetSize(gr, MyFont);
  355. // Recursively arrange our children,
  356. // allowing room for this node.
  357. float x = xmin;
  358. float biggest_ymin = ymin + my_size.Height;
  359. float subtree_ymin = ymin + my_size.Height + Voffset;
  360. foreach (TreeNode<T> child in Children)
  361. {
  362. // Arrange this child's subtree.
  363. float child_ymin = subtree_ymin;
  364. child.Arrange(gr, ref x, ref child_ymin);
  365. // See if this increases the biggest ymin value.
  366. if (biggest_ymin < child_ymin) biggest_ymin = child_ymin;
  367. // Allow room before the next sibling.
  368. x += Hoffset;
  369. }
  370. // Remove the spacing after the last child.
  371. if (Children.Count > 0) x -= Hoffset;
  372. // See if this node is wider than the subtree under it.
  373. float subtree_width = x - xmin;
  374. if (my_size.Width > subtree_width)
  375. {
  376. // Center the subtree under this node.
  377. // Make the children rearrange themselves
  378. // moved to center their subtrees.
  379. x = xmin + (my_size.Width - subtree_width) / 2;
  380. foreach (TreeNode<T> child in Children)
  381. {
  382. // Arrange this child's subtree.
  383. child.Arrange(gr, ref x, ref subtree_ymin);
  384. // Allow room before the next sibling.
  385. x += Hoffset;
  386. }
  387. // The subtree's width is this node's width.
  388. subtree_width = my_size.Width;
  389. }
  390. // Set this node's center position.
  391. Center = new PointF(xmin + subtree_width / 2,
  392. ymin + my_size.Height / 2);
  393. // Increase xmin to allow room for
  394. // the subtree before returning.
  395. xmin += subtree_width;
  396. // Set the return value for ymin.
  397. ymin = biggest_ymin;
  398. }
  399. // Draw the subtree rooted at this node
  400. // with the given upper left corner.
  401. public void DrawTree(Graphics gr, ref float x, float y)
  402. {
  403. // Arrange the tree.
  404. Arrange(gr, ref x, ref y);
  405. // Draw the tree.
  406. DrawTree(gr);
  407. }
  408. // Draw the subtree rooted at this node.
  409. public void DrawTree(Graphics gr)
  410. {
  411. // Draw the links.
  412. DrawSubtreeLinks(gr);
  413. // Draw the nodes.
  414. DrawSubtreeNodes(gr);
  415. }
  416. // Draw the links for the subtree rooted at this node.
  417. private void DrawSubtreeLinks(Graphics gr)
  418. {
  419. foreach (TreeNode<T> child in Children)
  420. {
  421. // Draw the link between this node this child.
  422. gr.DrawLine(MyPen, Center, child.Center);
  423. // Recursively make the child draw its subtree nodes.
  424. child.DrawSubtreeLinks(gr);
  425. }
  426. }
  427. // Draw the nodes for the subtree rooted at this node.
  428. private void DrawSubtreeNodes(Graphics gr)
  429. {
  430. // Draw this node.
  431. Data.Draw(Center.X, Center.Y, gr, MyPen, BgBrush, FontBrush, MyFont);
  432. // Recursively make the child draw its subtree nodes.
  433. foreach (TreeNode<T> child in Children)
  434. {
  435. child.DrawSubtreeNodes(gr);
  436. }
  437. }
  438. }
  439. class CircleNode : IDrawable
  440. {
  441. // The string we will draw.
  442. public string Text;
  443. public CircleNode(string new_text)
  444. {
  445. Text = new_text;
  446. }
  447. // Return the size of the string plus a 10 pixel margin.
  448. public SizeF GetSize(Graphics gr, Font font)
  449. {
  450. return gr.MeasureString(Text, font) + new SizeF(10, 10);
  451. }
  452. // Draw the object centered at (x, y).
  453. void IDrawable.Draw(float x, float y, Graphics gr, Pen pen, Brush bg_brush, Brush text_brush, Font font)
  454. {
  455. // Fill and draw an ellipse at our location.
  456. SizeF my_size = GetSize(gr, font);
  457. RectangleF rect = new RectangleF(
  458. x - my_size.Width / 2,
  459. y - my_size.Height / 2,
  460. my_size.Width, my_size.Height);
  461. gr.FillEllipse(bg_brush, rect);
  462. gr.DrawEllipse(pen, rect);
  463. // Draw the text.
  464. using (StringFormat string_format = new StringFormat())
  465. {
  466. string_format.Alignment = StringAlignment.Center;
  467. string_format.LineAlignment = StringAlignment.Center;
  468. gr.DrawString(Text, font, text_brush, x, y, string_format);
  469. }
  470. }
  471. }
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement