///chương trình c giải hệ phương trình bất nhất với số ẩn bất kì, n ẩn
#include<stdio.h>
#include<conio.h>
void nhap(float a[][100], float b[], int n)
{
printf("_______________________________________________\n");
for (int i = 0; i < n; i++)
{
printf("nhap cac he so phuong trinh %d:\n", i + 1);
for (int j = 0; j < n; j++)
scanf_s("%f", &a[i][j]);
scanf_s("%f", &b[i]);
}
printf("_______________________________________________\n");
}
float det(float a[][100], int n)
{
int i, j, k, dem = 0,kt=0;
float b[100], c[100], h, kq=1;
for (i = 0; i < n - 1; i++)
{
if (a[i][i] == 0)
{
for (j = 0; j < n; j++)
if (a[i][j] != 0)
{
for (k = 0; k < n; k++)
{
c[k] = a[k][i];
a[k][i] = a[k][j];
a[k][j] = c[k];
}
dem++;
kt++;
break;
}
if (kt == 0) return 0;
}
b[i] = a[i][i];
for (j = 0; j < n; j++) a[i][j] = a[i][j] / b[i];
for (j = i + 1; j < n; j++)
{
h = a[j][i];
for (k = 0; k < n; k++) a[j][k] = a[j][k] - h*a[i][k];
}
}
b[n - 1] = a[n - 1][n - 1];
for (i = 0; i < n; i++) kq *= b[i];
if (dem % 2 == 0) return kq; else return -kq;
}
void truyen(float a[][100], float b[][100], int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
b[i][j] = a[i][j];
}
void gan(float b[100], float a[100][100], int n, int m)
{
for (int i = 0; i < n; i++)
a[i][m] = b[i];
}
void main()
{
printf("___________Tran_Khanh_Toan__________________\n");
printf("_______________________________________________\n");
float dd;
float a[100][100], b[100], x[100], c[100][100];
int n, i;
printf(">>ban muon giai he phuong trinh bat nhat may an ? :");
scanf_s("%d", &n);
nhap(a, b, n);
truyen(a, c, n);
dd = det(c, n);
if (dd!=0)
{
for (i = 0; i < n; i++)
{
truyen(a, c, n);
gan(b, c, n, i);
x[i] = det(c, n);
}
printf("he phuong trinh co nghiem la:\n");
for (i = 0; i < n; i++)
printf("x%d = %.2f\n", i + 1, x[i]);
}
else printf(">>he phuong trinh vo nghiem hoac vo so nghiem");
_getch();
}
0 comments:
Post a Comment