include stdio include process include math define _CRT_SECURE_NO_WARNI

  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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <process.h>
#include <math.h>
#define _CRT_SECURE_NO_WARNINGS
float *a, *b, *x, *y, **sums;
int N, K;
//N - number of data points
//K - polinom power
//K<=N
char filename[256];
FILE* InFile = NULL;
void count_num_lines(){
//count number of lines in input file - number of equations
int nelf = 0; //non empty line flag
do{
nelf = 0;
while (fgetc(InFile) != '\n' && !feof(InFile)) nelf = 1;
if (nelf) N++;
} while (!feof(InFile));
}
void freematrix(){
//free memory for matrixes
int i;
for (i = 0; i<K + 1; i++){
delete[] sums[i];
}
delete[] a;
delete[] b;
delete[] x;
delete[] y;
delete[] sums;
}
void allocmatrix(){
//allocate memory for matrixes
int i, j, k;
a = new float[K + 1];
b = new float[K + 1];
x = new float[N];
y = new float[N];
sums = new float*[K + 1];
if (x == NULL || y == NULL || a == NULL || sums == NULL){
printf("\nNot enough memory to allocate. N=%d, K=%d\n", N, K);
exit(-1);
}
for (i = 0; i<K + 1; i++){
sums[i] = new float[K + 1];
if (sums[i] == NULL){
printf("\nNot enough memory to allocate for %d equations.\n", K + 1);
}
}
for (i = 0; i<K + 1; i++){
a[i] = 0;
b[i] = 0;
for (j = 0; j<K + 1; j++){
sums[i][j] = 0;
}
}
for (k = 0; k<N; k++){
x[k] = 0;
y[k] = 0;
}
}
void readmatrix(){
int i = 0, j = 0, k = 0;
//read x, y matrixes from input file
for (k = 0; k<N; k++){
fscanf(InFile, "%f", &x[k]);
fscanf(InFile, "%f", &y[k]);
}
//init square sums matrix
for (i = 0; i<K + 1; i++){
for (j = 0; j<K + 1; j++){
sums[i][j] = 0;
for (k = 0; k<N; k++){
sums[i][j] += pow(x[k], i + j);
}
}
}
//init free coefficients column
for (i = 0; i<K + 1; i++){
for (k = 0; k<N; k++){
b[i] += pow(x[k], i) * y[k];
}
}
}
void printresult(){
//print polynom parameters
int i = 0;
printf("\n");
for (i = 0; i<K + 1; i++){
printf("a[%d] = %f\n", i, a[i]);
}
}
/*void testsolve(){
//test that ax=y
FILE* OutFile;
int i=0, j=0;
float sum, s, begin, end, step;
begin = x[0][1];
end = x[N-1][1];
step = (end-begin)/20;
printf("\n");
for(i=0; i<N; i++){
sum = 0;
for(j=0; j<N; j++){
sum += x[i][j]*a[j];
}
printf("%f\t%f\n", s, y[i]);
}
OutFile = fopen("test.txt", "wt");
for(s = begin; s<=end; s+=step){
sum = 0;
for(j=0; j<N; j++){
sum += pow(s, j)*a[j];
}
fprintf(OutFile, "%f\t%f\n", s, sum);
}
fclose(OutFile);
}*/
/*void printresult(){
int i=0;
printf("\n");
printf("Result\n");
for(i=0; i<N; i++){
printf("A%d = %f\n", i, a[i]);
}
}*/
void diagonal(){
int i, j, k;
float temp = 0;
for (i = 0; i<K + 1; i++){
if (sums[i][i] == 0){
for (j = 0; j<K + 1; j++){
if (j == i) continue;
if (sums[j][i] != 0 && sums[i][j] != 0){
for (k = 0; k<K + 1; k++){
temp = sums[j][k];
sums[j][k] = sums[i][k];
sums[i][k] = temp;
}
temp = b[j];
b[j] = b[i];
b[i] = temp;
break;
}
}
}
}
}
void cls(){
for (int i = 0; i<25; i++) printf("\n");
}
void main(){
int i = 0, j = 0, k = 0;
cls();
do{
printf("\nInput filename: ");
scanf("%s", filename);
InFile = fopen(filename, "rt");
} while (InFile == NULL);
count_num_lines();
printf("\nNumber of points: N=%d", N);
do{
printf("\nInput power of approximation polinom K<N: ");
scanf("%d", &K);
} while (K >= N);
allocmatrix();
rewind(InFile);
//read data from file
readmatrix();
//check if there are 0 on main diagonal and exchange rows in that case
diagonal();
fclose(InFile);
//printmatrix();
//process rows
for (k = 0; k<K + 1; k++){
for (i = k + 1; i<K + 1; i++){
if (sums[k][k] == 0){
printf("\nSolution is not exist.\n");
return;
}
float M = sums[i][k] / sums[k][k];
for (j = k; j<K + 1; j++){
sums[i][j] -= M * sums[k][j];
}
b[i] -= M*b[k];
}
}
//printmatrix();
for (i = (K + 1) - 1; i >= 0; i--){
float s = 0;
for (j = i; j<K + 1; j++){
s = s + sums[i][j] * a[j];
}
a[i] = (b[i] - s) / sums[i][i];
}
//InFile = fopen(filename, "rt");
//readmatrix();
//fclose(InFile);
//printmatrix();
//testsolve();
printresult();
freematrix();
}