Есть приложение,в нём dataGridView,в который вносятся значения:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
| //Создаём функцию typing для ручного ввода
private: void typing(int size, int **mass) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < size; j++) {
//Получаем значения из ячеек таблицы
try {
mass[i][j] = Convert::ToDouble(dataGridView1->Rows[i]->Cells[j]->Value);
}
catch (Exception^ ex) {
MessageBox::Show("Неверный формат данных!");
dataGridView1->Rows[i]->Cells[j]->Value = "";
dataGridView1->ClearSelection();
dataGridView1->Update();
}
}
}
}
//Создаём функцию clear для очистки таблицы
private: void clear() {
for (int i = 1; i < dataGridView1->Rows->Count; i++) {
for (int j = 0; j < dataGridView1->Columns->Count; j++) {
dataGridView1->Rows[i]->Cells[j]->Value = "";
}
}
}
//Создаём функцию show, которая будет выводить нашу матрицу
private: void show(int size, int **mass) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < size; j++) {
//Вывод промежутков стробоскопирования
dataGridView1->Rows[0]->Cells[j]->Value = j*0.1;
dataGridView1->Rows[0]->Cells[j]->ReadOnly;
//Вывод названий строк
dataGridView1->Rows[0]->HeaderCell->Value = "t, сек";
dataGridView1->Rows[1]->HeaderCell->Value = "x, см";
dataGridView1->Rows[2]->HeaderCell->Value = "y, см";
//Вывод значений ячеек
mass[i][j] = Convert::ToDouble(dataGridView1->Rows[i]->Cells[j]->Value);
}
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//Получаем размерность матрицы
int size = Convert::ToInt32(numericUpDown1->Value);
//Создаём двумерный динамический массив для нашей матрицы
int **mass = new int *[size];
for (int i = 0; i < 2; i++) {
mass[i] = new int[size];
}
//Создаём таблицу под матрицу
dataGridView1 -> ColumnCount = size;
dataGridView1->RowCount = 3;
typing(size, mass);
//Вызываем функцию show для выводы матрицы
show(size, mass);
//Удаляем из памяти массив с матрицей
for (int i = 0; i < 2; i++) {
delete[]mass[i];
}
delete[] mass;
}
private: System::Void рассчитатьИПостроитьToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
//Системно нажимаем на кнопку "ОК"
button1->PerformClick();
}
private: System::Void выходToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
//Выходим из приложения
Application::Exit();
}
private: System::Void очиститьToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
clear();
}
private: System::Void dataGridView1_EditingControlShowing(System::Object^ sender, System::Windows::Forms::DataGridViewEditingControlShowingEventArgs^ e) {
TextBox ^tb = (TextBox^)e->Control;
tb->MaxLength = 4; //Максимальное кол-во символов в ячейке
tb->KeyPress += gcnew KeyPressEventHandler(this, &MyForm::tb_KeyPress);
}
void tb_KeyPress(Object^ sender, KeyPressEventArgs^ e) {
//Разрешён ввод только цифр, запятой и нажатие Backspace
if (!((Char::IsDigit(e->KeyChar) || e->KeyChar == (char)8) || e->KeyChar == (char)44))
e->Handled = true;
} |
|
и есть код консольного приложения,решающий систему уравнений:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| #include <iostream>
using namespace std;
// Вывод системы уравнений
void sysout(double **a, double *y, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << "*x" << j;
if (j < n - 1)
cout << " + ";
}
cout << " = " << y[i] << endl;
}
return;
}
double * gauss(double **a, double *y, int n)
{
double *x, max;
int k, index;
const double eps = 0.00001; // точность
x = new double[n];
k = 0;
while (k < n)
{
// Поиск строки с максимальным a[i][k]
max = abs(a[k][k]);
index = k;
for (int i = k + 1; i < n; i++)
{
if (abs(a[i][k]) > max)
{
max = abs(a[i][k]);
index = i;
}
}
// Перестановка строк
if (max < eps)
{
// нет ненулевых диагональных элементов
cout << "Решение получить невозможно из-за нулевого столбца ";
cout << index << " матрицы A" << endl;
return 0;
}
for (int j = 0; j < n; j++)
{
double temp = a[k][j];
a[k][j] = a[index][j];
a[index][j] = temp;
}
double temp = y[k];
y[k] = y[index];
y[index] = temp;
// Нормализация уравнений
for (int i = k; i < n; i++)
{
double temp = a[i][k];
if (abs(temp) < eps) continue; // для нулевого коэффициента пропустить
for (int j = 0; j < n; j++)
a[i][j] = a[i][j] / temp;
y[i] = y[i] / temp;
if (i == k) continue; // уравнение не вычитать само из себя
for (int j = 0; j < n; j++)
a[i][j] = a[i][j] - a[k][j];
y[i] = y[i] - y[k];
}
k++;
}
// обратная подстановка
for (k = n - 1; k >= 0; k--)
{
x[k] = y[k];
for (int i = 0; i < k; i++)
y[i] = y[i] - a[i][k] * x[k];
}
return x;
}
int core()
{
double **a, *y, *x;
int n = 2;
a = new double*[n];
y = new double[n];
for (int i = 0; i < n; i++)
{
a[i] = new double[n];
for (int j = 0; j < n; j++)
{
a[0][0] = dataGridView1->Rows[0]->Cells[j]->Value = Value^2; // не обращайте внимания,просто пометка что здесь должна быть сумма квадратов значений всех ячеек
a[0][1] += Convert::ToDouble(dataGridView1->Rows[0]->Cells[j]->Value);// сумма значений всех ячеек 1-ой строки
a[1][0] = a[0][1];
a[1][1] = size;
}
}
for (int i = 0; i < n; i++)
{
y[0] = //сумма произведений ячейки 1-й строки j-го столбца на ячейку 2-ой строки этого же столбца
y[1] += Convert::ToDouble(dataGridView1->Rows[1]->Cells[j]->Value); // сумма значений всех ячеек 2-ой строки
}
sysout(a, y, n);
x = gauss(a, y, n);
for (int i = 0; i < n; i++)
cout << x[0]<<"t+"<<x[1]<< endl;
cin.get(); cin.get();
return 0;
} |
|
Необходимо как-то связать код консольного приложения с кодом приложения winforms. Пробовал добавлять код консольного приложения в проект через .h и .cpp файл,и потом вызвать core(), но ничего не получается.
Если просто вставить код в "рассчитать и построить",то ругается на необходимость инициализировать index и необходимость ; после { void sysout.