Добавил:
Valeriya
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовая работа. Вариант 21 / Gauss_C_Sharp_alg
.txt using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int n;
int m;
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0)
{
// MessageBox.Show("Danger","Alert");
// Dialog box with exclamation icon. [8]
MessageBox.Show("Your haven't input value for amount to string.",
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
Form1.ActiveForm.Refresh();
return;
}
//конвертация типов данных
//читаем из textBox1 в n <stolbiy>
n = Convert.ToInt32(textBox1.Text);
//добавляем +один размер для хранения вектора B
n = n + 1;
//читаем из textBox2 в m <stroki>
m = Convert.ToInt32(textBox2.Text);
int[,] matrix = new int[n, m];
for (int i = 0; i < n; i++)
{
dataGridView1.Columns.Add("ob2", "ob2");
}
for (int i = 0; i < m; i++)
{
dataGridView1.Rows.Add();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, MouseEventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
//MessageBox.Show("Gauss calc\nver 2 alpha");
AboutBox1 about = new AboutBox1();
about.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//очистка полей ввода и вывода
//удаление строк и столбцов (полностью) из dataGridView1
while (dataGridView1.Rows.Count != 0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
}
while (dataGridView1.Columns.Count != 0)
{
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
}
//очистка поля ввода textBox1
textBox1.Clear();
textBox2.Clear();
n = 0;
m = 0;
}
private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
{
// Form2 f2 = new Form2();
//f2.Show();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
//аналогия очистки, т.е. создает новый ввод
//очистка полей ввода и вывода
//удаление строк и столбцов (полностью) из dataGridView1
while (dataGridView1.Rows.Count != 0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
}
while (dataGridView1.Columns.Count != 0)
{
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
}
//очистка поля ввода textBox1
textBox1.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
//Check
MessageBox.Show(Convert.ToString(n),"n value");
MessageBox.Show(Convert.ToString(m), "m value");
//Amount elements to use and they size init
//n is use as global variable, n is a size of quad matrix
float[,] matrix;
float[] a;
float[] b;
a = new float[n];//variable a[],solve
b = new float[n];//vector b[], last column in DataGridView
matrix = new float[n, m];//matrix[], DGV without last column
//Fill matrix with elements from DataGridView
//size of DGV and matrix is match
for(int i=0; i<n; i++)
for (int j = 0; j < m; j++)
{
//without last column, to init matrix by DGV
matrix[i, j] = Convert.ToInt32(dataGridView1.Rows[j].Cells[i].Value);
//last column to init vector b
// b[i] = Convert.ToInt32(dataGridView1.Rows[i].Cells[j+1].Value);
//check
MessageBox.Show(Convert.ToString(matrix[i,j]), "Cell MATRIX value");
//MessageBox.Show(Convert.ToString(b[i]), "Cell VECTOR B value");
}
//Main function
//Method of Gauss included
float temp = 0;
for (int i = 0; i < n; i++)
{
if (matrix[i, i] == 0)
{
for (int j = 0; j < n; j++)
{
if (j == i)
continue;
if (matrix[j, i] != 0 && matrix[i, j] != 0)
{
for (int k = 0; k < n; k++)
{
temp = matrix[j, k];
matrix[j, k] = matrix[i, k];
matrix[i, k] = temp;
}
temp = b[j];
b[j] = b[i];
b[i] = temp;
break;
}
}
}
}
//Result of actions
//Answer included in a[]
float s = 0;
for (int i = n - 1; i >= 0; i--)
{
for (int j = i; j < n; j++)
s += matrix[i, j] * a[j];
a[i] = (b[i] - s) / matrix[i, i];//answer or solve
}
//a[] can be exported to different controls
for (int i = 0; i < n; i++)
{
MessageBox.Show(Convert.ToString(a[i]), "Solver A value");
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
int n;
int m;
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0)
{
// MessageBox.Show("Danger","Alert");
// Dialog box with exclamation icon. [8]
MessageBox.Show("Your haven't input value for amount to string.",
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
Form1.ActiveForm.Refresh();
return;
}
//конвертация типов данных
//читаем из textBox1 в n <stolbiy>
n = Convert.ToInt32(textBox1.Text);
//добавляем +один размер для хранения вектора B
n = n + 1;
//читаем из textBox2 в m <stroki>
m = Convert.ToInt32(textBox2.Text);
int[,] matrix = new int[n, m];
for (int i = 0; i < n; i++)
{
dataGridView1.Columns.Add("ob2", "ob2");
}
for (int i = 0; i < m; i++)
{
dataGridView1.Rows.Add();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void contextMenuStrip1_Opening(object sender, MouseEventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
//MessageBox.Show("Gauss calc\nver 2 alpha");
AboutBox1 about = new AboutBox1();
about.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//очистка полей ввода и вывода
//удаление строк и столбцов (полностью) из dataGridView1
while (dataGridView1.Rows.Count != 0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
}
while (dataGridView1.Columns.Count != 0)
{
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
}
//очистка поля ввода textBox1
textBox1.Clear();
textBox2.Clear();
n = 0;
m = 0;
}
private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
{
// Form2 f2 = new Form2();
//f2.Show();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
//аналогия очистки, т.е. создает новый ввод
//очистка полей ввода и вывода
//удаление строк и столбцов (полностью) из dataGridView1
while (dataGridView1.Rows.Count != 0)
{
dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
}
while (dataGridView1.Columns.Count != 0)
{
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
}
//очистка поля ввода textBox1
textBox1.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
//Check
MessageBox.Show(Convert.ToString(n),"n value");
MessageBox.Show(Convert.ToString(m), "m value");
//Amount elements to use and they size init
//n is use as global variable, n is a size of quad matrix
float[,] matrix;
float[] a;
float[] b;
a = new float[n];//variable a[],solve
b = new float[n];//vector b[], last column in DataGridView
matrix = new float[n, m];//matrix[], DGV without last column
//Fill matrix with elements from DataGridView
//size of DGV and matrix is match
for(int i=0; i<n; i++)
for (int j = 0; j < m; j++)
{
//without last column, to init matrix by DGV
matrix[i, j] = Convert.ToInt32(dataGridView1.Rows[j].Cells[i].Value);
//last column to init vector b
// b[i] = Convert.ToInt32(dataGridView1.Rows[i].Cells[j+1].Value);
//check
MessageBox.Show(Convert.ToString(matrix[i,j]), "Cell MATRIX value");
//MessageBox.Show(Convert.ToString(b[i]), "Cell VECTOR B value");
}
//Main function
//Method of Gauss included
float temp = 0;
for (int i = 0; i < n; i++)
{
if (matrix[i, i] == 0)
{
for (int j = 0; j < n; j++)
{
if (j == i)
continue;
if (matrix[j, i] != 0 && matrix[i, j] != 0)
{
for (int k = 0; k < n; k++)
{
temp = matrix[j, k];
matrix[j, k] = matrix[i, k];
matrix[i, k] = temp;
}
temp = b[j];
b[j] = b[i];
b[i] = temp;
break;
}
}
}
}
//Result of actions
//Answer included in a[]
float s = 0;
for (int i = n - 1; i >= 0; i--)
{
for (int j = i; j < n; j++)
s += matrix[i, j] * a[j];
a[i] = (b[i] - s) / matrix[i, i];//answer or solve
}
//a[] can be exported to different controls
for (int i = 0; i < n; i++)
{
MessageBox.Show(Convert.ToString(a[i]), "Solver A value");
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
Соседние файлы в папке Курсовая работа. Вариант 21