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
|
int main(){
int size=0;
float x[5];
float A[5][5];
float b[5];
int l=0,c=0;
char line[100];
FILE *file;
char Path1[50];
char Path2[50];
char Path3[50];
char Path4[50];
printf("Path1:");
gets(Path1);
printf("Path2:");
gets(Path2);
printf("Path3:");
gets(Path3);
file = fopen(Path1,"r");
if(file == NULL) {
printf("file unknow - file_name.txt\n");
return 0;
}
while(fgets(line, sizeof(line), file) != NULL) {
size++;
}
fclose(file);
file = fopen(Path1,"r");
if(file == NULL) {
printf("file unknow - file_name.txt\n");
return 0;
}
while(fgets(line, sizeof(line), file) != NULL) {
char *split = strtok(line," ");
if(split)
{
A[l][c] = (float)strtod(split,NULL);
c++;
}
while(split != NULL)
{
split=strtok(NULL," ");
if (c < size)
A[l][c] = (float)strtod(split,NULL);
c++;
}
c=0;
l++;
}
fclose(file);
c=0;
l=0;
file = fopen(Path2,"r");
if(file == NULL) {
printf("file unknow - file_name.txt\n");
return 0;
}
while(fgets(line, sizeof(line), file) != NULL) {
if (c < size)
b[c] = (float)strtod(line,NULL);
c++;
}
int k,i,j; float m;
for (k=0;k<=size-2;k++){
for (i=k+1;i<=size-1;i++){
m=A[i][k]/A[k][k];
for(j=0;j<size;j++){
A[i][j]=A[i][j]-(m*A[k][j]);
}
b[i]=b[i]-(m*b[k]);
}
ofstream fout(strcat(Path3,"\\matriz.txt"));
for(i=0; i<size; i++){
for(j=0; j<size; j++){
fout << A[i][j] << "\t";
}
fout << endl;
}
}
retrosubstituicao(A,b,x,size);
ofstream fout(strcat(Path3,"\\matriz2.txt"));
for(i=0; i<size; i++){
fout << "x " << i << " "<< x[i] << "\t";
fout << endl;
}
return 0;
}
|