|
Create two buttons and two ListBox data display boxes on the dialog box.
One is to read the data from the excel table and display the contents of the excel table in a ListBox. The other button is to read the data from the excel table to implement the calculation of least squares and put the result in another ListBox Display.
Because I'm using the MFC framework in Visual C ++ for the first time, I don't understand a lot of things, and I read a lot of articles on the Internet, but I always make mistakes. Which teacher would like to help.
The calculation procedure is better for me.
#include <stdio.h>
#include <math.h>
#define N 1000000
#define H 100
#define E 0.0001
float A [H] [H];
float _A [H] [H];
float x [H];
float x0 [H];
float A_A [H] [H];
float b [H];
float B [H];
int n;
int m;
float Sumax (int n, int i, float x0 [])
{
int j;
float result;
result = 0;
for (j = 0; j <n; j ++)
result + = A_A [i] [j] * x0 [j];
return result;
}
float Maxof (float x [], float x0 [], int n)
{
int i;
float result;
result = 0;
for (i = 0; i <n; i ++)
{
if (result <fabs (x [i] -x0 [i]))
result = fabs (x [i] -x0 [i]);
}
return result;
}
void Jacobiagrithm (float b [], int n, float x [], float e)
{
int i, k;
float R;
for (i = 0; i <n; i ++)
x0 [i] = 0;
for (k = 1; k <= N; k ++)
{
for (i = 0; i <n; i ++)
x [i] = x0 [i] + (b [i] -Sumax (n, i, x0)) / A_A [i] [i];
R = Maxof (x, x0, n);
if (R <= e) return;
for (i = 0; i <n; i ++)
x0 [i] = x [i];
}
}
void Init ()
{
int i, j;
float sum;
scanf ("% d",&m);
scanf ("% d",&n);
for (i = 0; i <m; i ++)
for (int j = 0; j <n; j ++)
scanf ("% f",&A [i] [j]);
for (i = 0; i <m; i ++)
scanf ("% f",&b [i]);
// for (i = 0; i <m; i ++)
// A [i] [j] = 1;
for (i = 0; i <n; i ++)
for (j = 0; j <m; j ++)
_A [i] [j] = A [j] [i];
for (i = 0; i <m; i ++)
for (j = 0; j <m; j ++)
{
sum = 0;
for (int k = 0; k <m; k ++)
if (m> = n)
sum + = _ A [i] [k] * A [k] [j];
else
sum + = A [k] [j] * _ A [i] [k];
A_A [i] [j] = sum;
}
for (i = 0; i <m; i ++)
{
sum = 0;
for (int k = 0; k <m; k ++)
if (m> = n)
sum + = _ A [i] [k] * b [k];
else
sum + = b [k] * _ A [i] [k];
B [i] = sum;
}
Jacobiagrithm (B, n, x, E);
for (i = 0; i <n; i ++)
printf ("% f", x [i]);
}
void main ()
{
Init ();
} |
|