|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using System.Drawing; |
| 4 | +using System.Linq; |
| 5 | +using System.Windows.Forms; |
| 6 | +using System.Drawing.Drawing2D; |
| 7 | + |
| 8 | +namespace WindowsFormsApp1 |
| 9 | +{ |
| 10 | + public partial class Form1 : Form |
| 11 | + { |
| 12 | + private int[] array; |
| 13 | + private int i = 0; |
| 14 | + private int j; |
| 15 | + private int key; |
| 16 | + private bool haveKey; |
| 17 | + |
| 18 | + |
| 19 | + public Form1() |
| 20 | + { |
| 21 | + InitializeComponent(); |
| 22 | + } |
| 23 | + |
| 24 | + protected void DrawArrow( Graphics graphics, Rectangle rectangle, int fromCol, int toCol, int fromRow, int toRow, bool arrowStart) |
| 25 | + { |
| 26 | + float width = rectangle.Width; |
| 27 | + float height = rectangle.Height; |
| 28 | + |
| 29 | + Pen pen = new Pen(Color.DarkRed, 7); |
| 30 | + |
| 31 | + |
| 32 | + if (arrowStart) |
| 33 | + { |
| 34 | + pen.StartCap = LineCap.ArrowAnchor; |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + pen.EndCap = LineCap.ArrowAnchor; |
| 39 | + } |
| 40 | + |
| 41 | + graphics.DrawLine( |
| 42 | + pen, |
| 43 | + width / 30 + (width / 15) * fromCol + fromCol * 10 , |
| 44 | + height / 2 + ((width / 8) * fromRow) / 6, |
| 45 | + width / 30 + (width / 15) * toCol + toCol * 10, |
| 46 | + height / 2 + ((width / 8) * toRow) / 6 |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + protected void DrawArrayBoxes(int[] arr, Graphics graphics, Rectangle clientRectangle, int cell, int line, int[] blueRange, int[] pinkRange) |
| 52 | + { |
| 53 | + float width = clientRectangle.Width; |
| 54 | + float height = clientRectangle.Height; |
| 55 | + |
| 56 | + |
| 57 | + for (int index = 0; index < arr.Length; ++index) |
| 58 | + { |
| 59 | + RectangleF rectangleF = new RectangleF( |
| 60 | + width / 30 + (width / 15) * (index + cell) + 6 * (index + cell), |
| 61 | + height / 4 + 30 * line, |
| 62 | + width / 15, |
| 63 | + height / 7); |
| 64 | + |
| 65 | + graphics.FillRectangle(Brushes.White, rectangleF); |
| 66 | + |
| 67 | + if (blueRange.Contains(index)) |
| 68 | + { |
| 69 | + graphics.FillRectangle(Brushes.LightBlue, rectangleF); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + if (pinkRange.Contains(index)) |
| 74 | + { |
| 75 | + graphics.FillRectangle(Brushes.Pink, rectangleF); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + graphics.DrawString( |
| 80 | + arr[index].ToString(), |
| 81 | + new Font("Time New Rome", 16), |
| 82 | + Brushes.Black, |
| 83 | + new RectangleF( |
| 84 | + width / 30 + width / 15 * (index + cell) + 6 * (index + cell), |
| 85 | + height / 4 + 30 * line + 25, |
| 86 | + width / 15, |
| 87 | + height / 7 + 25), |
| 88 | + StringFormat.GenericDefault); |
| 89 | + |
| 90 | + |
| 91 | + graphics.DrawRectangle( |
| 92 | + new Pen(Brushes.Black), |
| 93 | + rectangleF.X, |
| 94 | + rectangleF.Y, |
| 95 | + rectangleF.Width, |
| 96 | + rectangleF.Height); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private void buttonNext_Click(object sender, EventArgs e) |
| 102 | + { |
| 103 | + |
| 104 | + if (array == null) |
| 105 | + { |
| 106 | + i = 0; |
| 107 | + j = 0; |
| 108 | + haveKey = false; |
| 109 | + array = textBoxNums.Text |
| 110 | + .Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries) |
| 111 | + .Select(s => int.Parse(s)) |
| 112 | + .ToArray(); |
| 113 | + |
| 114 | + textBoxNums.Visible = false; |
| 115 | + } |
| 116 | + |
| 117 | + bool upArrow = false; |
| 118 | + bool downArrow = false; |
| 119 | + |
| 120 | + Invalidate(); |
| 121 | + Application.DoEvents(); |
| 122 | + Graphics graphics = CreateGraphics(); |
| 123 | + |
| 124 | + if (i < array.Length) |
| 125 | + { |
| 126 | + |
| 127 | + if (j >= 0 && haveKey && array[j] >= key) |
| 128 | + { |
| 129 | + array[j + 1] = array[j]; |
| 130 | + DrawArrayBoxes(array, graphics, ClientRectangle, 0, 3, Enumerable.Range(0, i + 1).ToArray(), new int[1] { j + 1 }); |
| 131 | + DrawArrow( graphics, ClientRectangle, j, j + 1, 0, 0, false); |
| 132 | + |
| 133 | + j--; |
| 134 | + } |
| 135 | + else if (haveKey) |
| 136 | + { |
| 137 | + array[j + 1] = key; |
| 138 | + DrawArrayBoxes(array, graphics, ClientRectangle, 0, 3, Enumerable.Range(0, i + 1).ToArray(), new int[1] { j + 1 }); |
| 139 | + |
| 140 | + haveKey = false; |
| 141 | + downArrow = true; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + i++; |
| 146 | + DrawArrayBoxes(array, graphics, ClientRectangle, 0, 3, Enumerable.Range(0, i + 1).ToArray(), new int[0]); |
| 147 | + |
| 148 | + if (i == array.Length) |
| 149 | + { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + key = array[i]; |
| 154 | + haveKey = true; |
| 155 | + upArrow = true; |
| 156 | + j = i - 1; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + DrawArrayBoxes(new int[1] { key }, graphics, ClientRectangle, j + 1, 1, new int[0], upArrow ? new int[1] : new int[0]); |
| 161 | + |
| 162 | + if (!upArrow && !downArrow) |
| 163 | + { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + DrawArrow( graphics, ClientRectangle, j + 1, j + 1, upArrow ? 0 : 1, downArrow ? 0 : 1, true); |
| 168 | + |
| 169 | + |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + array = null; |
| 174 | + textBoxNums.Visible = true; |
| 175 | + } |
| 176 | + |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments