From a432cd3df6b5be221df822d2c0ada956eb351edb Mon Sep 17 00:00:00 2001 From: Lev Rusanov <30170278+JDM170@users.noreply.github.com> Date: Sat, 7 Oct 2023 15:43:07 +0700 Subject: [PATCH] Implemented getUnknownAlpha Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com> --- Form1.Designer.cs | 97 +++++++++++++++--------- Form1.cs | 188 ++++++++++++++++++++++++---------------------- 2 files changed, 160 insertions(+), 125 deletions(-) diff --git a/Form1.Designer.cs b/Form1.Designer.cs index e0e9817..16c4b5a 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -28,13 +28,13 @@ /// private void InitializeComponent() { - System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea10 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); - System.Windows.Forms.DataVisualization.Charting.Legend legend10 = new System.Windows.Forms.DataVisualization.Charting.Legend(); - System.Windows.Forms.DataVisualization.Charting.Series series28 = new System.Windows.Forms.DataVisualization.Charting.Series(); - System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint19 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 0D); - System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint20 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 0D); - System.Windows.Forms.DataVisualization.Charting.Series series29 = new System.Windows.Forms.DataVisualization.Charting.Series(); - System.Windows.Forms.DataVisualization.Charting.Series series30 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); + System.Windows.Forms.DataVisualization.Charting.Legend legend3 = new System.Windows.Forms.DataVisualization.Charting.Legend(); + System.Windows.Forms.DataVisualization.Charting.Series series7 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint5 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 0D); + System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint6 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 0D); + System.Windows.Forms.DataVisualization.Charting.Series series8 = new System.Windows.Forms.DataVisualization.Charting.Series(); + System.Windows.Forms.DataVisualization.Charting.Series series9 = new System.Windows.Forms.DataVisualization.Charting.Series(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); @@ -58,6 +58,7 @@ this.textBox1 = new System.Windows.Forms.TextBox(); this.del_by_name = new System.Windows.Forms.Button(); this.clean_graph = new System.Windows.Forms.Button(); + this.compare = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView3)).BeginInit(); @@ -82,16 +83,19 @@ // this.Column1.HeaderText = "alpha"; this.Column1.Name = "Column1"; + this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column2 // this.Column2.HeaderText = "low"; this.Column2.Name = "Column2"; + this.Column2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column3 // this.Column3.HeaderText = "high"; this.Column3.Name = "Column3"; + this.Column3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // dataGridView2 // @@ -111,20 +115,24 @@ // this.Column4.HeaderText = "alpha"; this.Column4.Name = "Column4"; + this.Column4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column5 // this.Column5.HeaderText = "low"; this.Column5.Name = "Column5"; + this.Column5.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column6 // this.Column6.HeaderText = "high"; this.Column6.Name = "Column6"; + this.Column6.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // dataGridView3 // this.dataGridView3.AllowUserToAddRows = false; + this.dataGridView3.AllowUserToDeleteRows = false; this.dataGridView3.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView3.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column7, @@ -139,16 +147,19 @@ // this.Column7.HeaderText = "alpha"; this.Column7.Name = "Column7"; + this.Column7.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column8 // this.Column8.HeaderText = "low"; this.Column8.Name = "Column8"; + this.Column8.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // Column9 // this.Column9.HeaderText = "high"; this.Column9.Name = "Column9"; + this.Column9.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; // // add // @@ -192,29 +203,29 @@ // // chart1 // - chartArea10.Name = "ChartArea1"; - this.chart1.ChartAreas.Add(chartArea10); - legend10.Name = "Legend1"; - this.chart1.Legends.Add(legend10); + chartArea3.Name = "ChartArea1"; + this.chart1.ChartAreas.Add(chartArea3); + legend3.Name = "Legend1"; + this.chart1.Legends.Add(legend3); this.chart1.Location = new System.Drawing.Point(380, 12); this.chart1.Name = "chart1"; - series28.ChartArea = "ChartArea1"; - series28.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series28.Legend = "Legend1"; - series28.Name = "A1"; - series28.Points.Add(dataPoint19); - series28.Points.Add(dataPoint20); - series29.ChartArea = "ChartArea1"; - series29.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series29.Legend = "Legend1"; - series29.Name = "B2"; - series30.ChartArea = "ChartArea1"; - series30.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; - series30.Legend = "Legend1"; - series30.Name = "C3"; - this.chart1.Series.Add(series28); - this.chart1.Series.Add(series29); - this.chart1.Series.Add(series30); + series7.ChartArea = "ChartArea1"; + series7.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series7.Legend = "Legend1"; + series7.Name = "A1"; + series7.Points.Add(dataPoint5); + series7.Points.Add(dataPoint6); + series8.ChartArea = "ChartArea1"; + series8.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series8.Legend = "Legend1"; + series8.Name = "B2"; + series9.ChartArea = "ChartArea1"; + series9.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; + series9.Legend = "Legend1"; + series9.Name = "C3"; + this.chart1.Series.Add(series7); + this.chart1.Series.Add(series8); + this.chart1.Series.Add(series9); this.chart1.Size = new System.Drawing.Size(512, 440); this.chart1.TabIndex = 7; this.chart1.Text = "chart1"; @@ -276,11 +287,22 @@ this.clean_graph.UseVisualStyleBackColor = true; this.clean_graph.Click += new System.EventHandler(this.clean_graph_Click); // + // compare + // + this.compare.Location = new System.Drawing.Point(712, 458); + this.compare.Name = "compare"; + this.compare.Size = new System.Drawing.Size(155, 87); + this.compare.TabIndex = 14; + this.compare.Text = "Сравнить"; + this.compare.UseVisualStyleBackColor = true; + this.compare.Click += new System.EventHandler(this.compare_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(907, 574); + this.Controls.Add(this.compare); this.Controls.Add(this.clean_graph); this.Controls.Add(this.del_by_name); this.Controls.Add(this.textBox1); @@ -310,17 +332,8 @@ #endregion private System.Windows.Forms.DataGridView dataGridView1; - private System.Windows.Forms.DataGridViewTextBoxColumn Column1; - private System.Windows.Forms.DataGridViewTextBoxColumn Column2; - private System.Windows.Forms.DataGridViewTextBoxColumn Column3; private System.Windows.Forms.DataGridView dataGridView2; - private System.Windows.Forms.DataGridViewTextBoxColumn Column4; - private System.Windows.Forms.DataGridViewTextBoxColumn Column5; - private System.Windows.Forms.DataGridViewTextBoxColumn Column6; private System.Windows.Forms.DataGridView dataGridView3; - private System.Windows.Forms.DataGridViewTextBoxColumn Column7; - private System.Windows.Forms.DataGridViewTextBoxColumn Column8; - private System.Windows.Forms.DataGridViewTextBoxColumn Column9; private System.Windows.Forms.Button add; private System.Windows.Forms.Button sub; private System.Windows.Forms.Button mul; @@ -332,6 +345,16 @@ private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button del_by_name; private System.Windows.Forms.Button clean_graph; + private System.Windows.Forms.DataGridViewTextBoxColumn Column1; + private System.Windows.Forms.DataGridViewTextBoxColumn Column2; + private System.Windows.Forms.DataGridViewTextBoxColumn Column3; + private System.Windows.Forms.DataGridViewTextBoxColumn Column4; + private System.Windows.Forms.DataGridViewTextBoxColumn Column5; + private System.Windows.Forms.DataGridViewTextBoxColumn Column6; + private System.Windows.Forms.DataGridViewTextBoxColumn Column7; + private System.Windows.Forms.DataGridViewTextBoxColumn Column8; + private System.Windows.Forms.DataGridViewTextBoxColumn Column9; + private System.Windows.Forms.Button compare; } } diff --git a/Form1.cs b/Form1.cs index 82d9062..3ef7fd1 100644 --- a/Form1.cs +++ b/Form1.cs @@ -19,33 +19,6 @@ namespace WindowsFormsApp1 private double ConvertDGValue(object value) { return Convert.ToDouble(value); } - private void MergeDGs() - { - DataGridView matrix_low, matrix_high; - if (dataGridView1.Rows.Count < dataGridView2.Rows.Count) - { - matrix_low = dataGridView1; - matrix_high = dataGridView2; - } - else - { - matrix_low = dataGridView2; - matrix_high = dataGridView1; - } - int lowRowCount = matrix_low.Rows.Count, - highRowCount = matrix_high.Rows.Count; - int wrong = 0; - for (int first = 0; first < highRowCount; first++) - { - for (int second = 0; second < lowRowCount; second++) - if (ConvertDGValue(matrix_low[0, second].Value) != ConvertDGValue(matrix_high[0, first].Value)) - wrong++; - if (wrong > lowRowCount - 1) - dataGridView3.Rows.Add(new object[] { matrix_high[0, first].Value, matrix_high[1, first].Value, matrix_high[2, first].Value }); - wrong = 0; - } - } - private double[,] ConvertDGtoMatrix(DataGridView dgv) { int rowCount = dgv.Rows.Count, @@ -74,13 +47,75 @@ namespace WindowsFormsApp1 return matrix; } + private Tuple getUnknownAlpha(double[,] matrix1, double[,] matrix2) + { + double[,] matrix_low, matrix_high; + if(matrix1.GetLength(1) < matrix2.GetLength(1)) + { + matrix_low = matrix1; + matrix_high = matrix2; + } + else + { + matrix_low = matrix2; + matrix_high = matrix1; + } + int colCount = matrix_low.GetLength(0), + lowRowCount = matrix_low.GetLength(1), + highRowCount = matrix_high.GetLength(1), + wrong = 0, + offsetNew = 0; + double[,] matrix3 = new double[colCount, highRowCount]; + for (int row = 0; row < lowRowCount; row++) + for (int col = 0; col < colCount; col++) + matrix3[col, row] = matrix_low[col, row]; + for (int high = 0; high < highRowCount; high++) + { + for (int low = 0; low < lowRowCount; low++) + if (matrix_low[0, low] != matrix_high[0, high]) + wrong++; + if (wrong > lowRowCount - 1) + { + matrix3[0, lowRowCount + offsetNew] = matrix_high[0, high]; + matrix3[1, lowRowCount + offsetNew] = 0; + matrix3[2, lowRowCount + offsetNew] = 0; + offsetNew++; + } + wrong = 0; + } + + matrix3 = SortMatrix(matrix3); + int minCoord = 0, maxCoord = 0; + double k, b; + for (int i = 0; i < highRowCount; i++) + { + if (matrix3[1, i] == 0 && matrix3[2, i] == 0) + { + minCoord = i - 1; + maxCoord = i + 1; + // k = (y2 - y1) / (x2 - x1); + // b = (x2 * y1 - x1 * y2) / (x2 - x1); + k = (matrix3[0, maxCoord] - matrix3[0, minCoord]) / (matrix3[1, maxCoord] - matrix3[1, minCoord]); + b = (matrix3[1, maxCoord] * matrix3[0, minCoord] - matrix3[1, minCoord] * matrix3[0, maxCoord]) / (matrix3[1, maxCoord] - matrix3[1, minCoord]); + matrix3[1, i] = -(b - matrix3[0, i]) / k; + + k = (matrix3[0, maxCoord] - matrix3[0, minCoord]) / (matrix3[2, maxCoord] - matrix3[2, minCoord]); + b = (matrix3[2, maxCoord] * matrix3[0, minCoord] - matrix3[2, minCoord] * matrix3[0, maxCoord]) / (matrix3[2, maxCoord] - matrix3[2, minCoord]); + matrix3[2, i] = -(b - matrix3[0, i]) / k; + } + } + + return Tuple.Create(matrix3, matrix_high); + } + private void PrintGraph(double[,] matrix, string chart_series) { DataPointCollection points = chart1.Series.FindByName(chart_series).Points; int matrixLength = matrix.GetLength(1); + points.Clear(); for (int row = 0; row < matrixLength; row++) points.AddXY(matrix[1, row], matrix[0, row]); - for(int row = matrixLength - 1; row >= 0; row--) + for (int row = matrixLength - 1; row >= 0; row--) points.AddXY(matrix[2, row], matrix[0, row]); } @@ -95,17 +130,6 @@ namespace WindowsFormsApp1 dataGridView2.Rows.Add(new object[] { 0.2, 2, 7 }); dataGridView2.Rows.Add(new object[] { 1, 4, 5 }); - /*// Simple merging - if(dataGridView2.Rows.Count > dataGridView1.Rows.Count) - { - for(int row = dataGridView1.Rows.Count; row < dataGridView2.Rows.Count; row++) - { - dataGridView3.RowCount++; - for(int col = 0; col < dataGridView1.Columns.Count; col++) - dataGridView3[col, row].Value = ConvertDGValue(dataGridView2[col, row].Value); - } - }*/ - /*double[,] matrix = ConvertDGtoMatrix(dataGridView2); for (int row = 0; row < matrix.GetLength(1); row++) { @@ -117,17 +141,11 @@ namespace WindowsFormsApp1 private void addition_Click(object sender, EventArgs e) { - double[,] matrix_low, matrix_high; - if (dataGridView1.Rows.Count < dataGridView2.Rows.Count) - { - matrix_low = ConvertDGtoMatrix(dataGridView1); + double[,] matrix_low = ConvertDGtoMatrix(dataGridView1), matrix_high = ConvertDGtoMatrix(dataGridView2); - } - else - { - matrix_low = ConvertDGtoMatrix(dataGridView2); - matrix_high = ConvertDGtoMatrix(dataGridView1); - } + Tuple tuple = getUnknownAlpha(matrix_low, matrix_high); + matrix_low = tuple.Item1; + matrix_high = tuple.Item2; dataGridView3.Rows.Clear(); for (int high = 0; high < matrix_high.GetLength(1); high++) for (int low = 0; low < matrix_low.GetLength(1); low++) @@ -138,22 +156,15 @@ namespace WindowsFormsApp1 matrix_low[1, low] + matrix_high[1, high], matrix_low[2, low] + matrix_high[2, high] }); - MergeDGs(); } private void subtraction_Click(object sender, EventArgs e) { - double[,] matrix_low, matrix_high; - if (dataGridView1.Rows.Count < dataGridView2.Rows.Count) - { - matrix_low = ConvertDGtoMatrix(dataGridView1); + double[,] matrix_low = ConvertDGtoMatrix(dataGridView1), matrix_high = ConvertDGtoMatrix(dataGridView2); - } - else - { - matrix_low = ConvertDGtoMatrix(dataGridView2); - matrix_high = ConvertDGtoMatrix(dataGridView1); - } + Tuple tuple = getUnknownAlpha(matrix_low, matrix_high); + matrix_low = tuple.Item1; + matrix_high = tuple.Item2; dataGridView3.Rows.Clear(); for (int high = 0; high < matrix_high.GetLength(1); high++) for (int low = 0; low < matrix_low.GetLength(1); low++) @@ -164,22 +175,15 @@ namespace WindowsFormsApp1 matrix_low[1, low] - matrix_high[2, high], matrix_low[2, low] - matrix_high[1, high] }); - MergeDGs(); } private void multiply_Click(object sender, EventArgs e) { - double[,] matrix_low, matrix_high; - if (dataGridView1.Rows.Count < dataGridView2.Rows.Count) - { - matrix_low = ConvertDGtoMatrix(dataGridView1); + double[,] matrix_low = ConvertDGtoMatrix(dataGridView1), matrix_high = ConvertDGtoMatrix(dataGridView2); - } - else - { - matrix_low = ConvertDGtoMatrix(dataGridView2); - matrix_high = ConvertDGtoMatrix(dataGridView1); - } + Tuple tuple = getUnknownAlpha(matrix_low, matrix_high); + matrix_low = tuple.Item1; + matrix_high = tuple.Item2; dataGridView3.Rows.Clear(); for (int high = 0; high < matrix_high.GetLength(1); high++) for (int low = 0; low < matrix_low.GetLength(1); low++) @@ -190,33 +194,36 @@ namespace WindowsFormsApp1 matrix_low[1, low] * matrix_high[1, high], matrix_low[2, low] * matrix_high[2, high] }); - MergeDGs(); } private void divide_Click(object sender, EventArgs e) { - double[,] matrix_low, matrix_high; - if (dataGridView1.Rows.Count < dataGridView2.Rows.Count) - { - matrix_low = ConvertDGtoMatrix(dataGridView1); + double[,] matrix_low = ConvertDGtoMatrix(dataGridView1), matrix_high = ConvertDGtoMatrix(dataGridView2); - } - else - { - matrix_low = ConvertDGtoMatrix(dataGridView2); - matrix_high = ConvertDGtoMatrix(dataGridView1); - } + Tuple tuple = getUnknownAlpha(matrix_low, matrix_high); + matrix_low = tuple.Item1; + matrix_high = tuple.Item2; dataGridView3.Rows.Clear(); for (int high = 0; high < matrix_high.GetLength(1); high++) for (int low = 0; low < matrix_low.GetLength(1); low++) if (matrix_low[0, low] == matrix_high[0, high]) - dataGridView3.Rows.Add(new object[] + { + if (matrix_high[1, high] > 0 && matrix_high[2, high] > 0) { - matrix_high[0, high], - matrix_low[1, low] / matrix_high[2, high], - matrix_low[2, low] / matrix_high[1, high] - }); - MergeDGs(); + dataGridView3.Rows.Add(new object[] + { + matrix_high[0, high], + matrix_low[1, low] / matrix_high[2, high], + matrix_low[2, low] / matrix_high[1, high] + }); + } + else + { + dataGridView3.Rows.Clear(); + MessageBox.Show("Деление на 0 невозможно. Задайте другое значение!"); + return; + } + } } private void build_a_Click(object sender, EventArgs e) @@ -249,5 +256,10 @@ namespace WindowsFormsApp1 chart1.Series.FindByName("B2").Points.Clear(); chart1.Series.FindByName("C3").Points.Clear(); } + + private void compare_Click(object sender, EventArgs e) + { + // compare + } } }