Được sửa bởi nienluan1 ngày Wed Sep 05, 2012 8:59 pm; sửa lần 2.
Các bạn ơi xem code cua minh sai chỗ nào vậy mình tìm hoài không thấy
Giới tính :
Tuổi : 34
Đến từ : can tho
Ngày Tham gia : 22/08/2012
Tổng số bài gửi : 12
Được sửa bởi nienluan1 ngày Wed Sep 05, 2012 8:59 pm; sửa lần 2.
Giới tính :
Tuổi : 31
Đến từ : Cần thơ
Ngày Tham gia : 10/01/2012
Tổng số bài gửi : 750
Giới tính :
Tuổi : 34
Đến từ : can tho
Ngày Tham gia : 22/08/2012
Tổng số bài gửi : 12
Giới tính :
Đến từ : TPHCM
Ngày Tham gia : 03/04/2011
Tổng số bài gửi : 2292
của bạn đâynienluan1 đã viết:Bạn có code GIẢI HỆ PHƯƠNG TRÌNH TUYẾN TÍNH VỚI HỆ SỐ LÀ PHÂN SỐ BẰNG QUI TẮC CRAMER cho mình xin với tới ngày nộp rùi mà chưa làm xong nữa.
Cài đặt Giải hệ phương trình tuyến tính bằng phương pháp khử Gauss trên ngôn ngữ lập trình C
- Code:
#include "math.h"
#include "conio.h"
#include "iostream.h"
/*Nhap ma tran he so*/
double**NhapMaTran(int n){
double**Temp;
*Temp = new double[n];
for(int i = 0; i<n; i++){
Temp[i] = new double[n+1];
for(int j = 0; j<=n; j++){
if(j!=n)
cout<<"a["<<i<<"]["<<j<<"] = ";
else
cout<<"b["<<i<<"] = ";
cin>>Temp[i][j];
}
}
return Temp;
}
/*Xuat ma tran*/
void XuatMaTran(double**A, int n) {
for(int i=0 ; i<n; i++){
cout<<endl;
for(int j=0 ; j<=n; j++)
cout<<A[i][j]<<"\t";
}
}
/*Xuat he phuong trinh tuyen tinh */
void XuatHPT(double**A, int n) {
for(int i=0 ; i<n; i++){
cout<<endl<<A[i][0]<<".x0";
for(int j=1 ; j<n; j++) {
if(A[i][j]<0)
cout<<"\t"<<A[i][j]<<".x"<<j;
else
cout<<"\t+"<<A[i][j]<<".x"<<j;
}
cout<<"\t = "<<A[i][n];
}
}
void Exit(){
asm {
mov ah,4ch
int 21h
}
}
/*Chuong Trinh Chinh*/
void main(){
clrscr();
int n;
cout<<"Nhap Cap Cua Ma Tran n = ";
cin>>n;
double**A = NhapMaTran(n);
//XuatMaTran(A,n);
XuatHPT(A,n);
A = NULL;
getch();
Exit();
}
crame
- Code:
#include <stdio.h>
#define nmax 20
class vector
{
double v[nmax];
int size;
public:
vector(int _size = nmax)
{
size = _size;
for (int k = 0; k < nmax; k++) v[k] = 0;
}
double &operator [] (int k) { return v[k];}
void show();
void input();
};
void vector::input()
{
printf("\nSo phan tu cua vec to: ");
scanf("%d",&size);
for (int k = 0; k < size; k++)
{
printf("[%d] = ", k);
scanf("%lf", &v[k]);
}
}
void vector::show()
{
for (int k = 0; k < size; k++) printf("%8.2lf",v[k]);
printf("\n");
}
class matrix
{
vector v[nmax];
int rows, cols;
public:
matrix(int r = nmax, int c = nmax)
{
rows = r; cols = c;
for (int k = 0; k < nmax; k++) v[k] = vector();
}
vector &operator [] (int k) { return v[k];}
int issquare();
matrix M(int, int);
double mdeterm();
matrix colreplace(int, vector);
friend int cramer(matrix, vector, vector &);
void input();
void show();
};
int matrix::issquare() { return rows==cols;}
void matrix::input()
{
printf("\nSo hang cua ma tran: ");
scanf("%d",&rows);
printf("\nSo cot cua ma tran: ");
scanf("%d",&cols);
for (int k = 0; k < rows; k++)
for (int j = 0; j < cols; j++)
{
printf("[%d][%d] = ", k,j);
scanf("%lf", &v[k][j]);
}
}
void matrix::show()
{
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++) printf("%8.2lf",v[k][j]);
printf("\n");
}
}
matrix matrix::M(int m, int n)
{
matrix b = *this;
int k, j;
for (k = m; k < rows-1; k++)
for (j = 0; j < cols; j++) b[k][j] = b[k+1][j];
for (j = n; j < cols-1; j++)
for (k = 0; k < rows; k++) b[k][j] = b[k][j+1];
b.rows = rows-1; b.cols = cols-1;
return b;
}
double matrix::mdeterm()
{
if (rows == 1) return v[0][0];
double temp = 0;
for (int k = rows-1; k >=0; k--)
if ((cols + k) % 2)
temp -= v[k][cols-1]*M(k,cols-1).mdeterm();
else
temp += v[k][cols-1]*M(k,cols-1).mdeterm();
return temp;
}
matrix matrix::colreplace(int k, vector c)
{
matrix b = *this;
for (int j = 0; j < rows; j++) b[j][k] = c[j];
return b;
}
int cramer(matrix a, vector b, vector &x)
{
double det = a.mdeterm();
if (det)
{
x = vector(a.rows);
for (int k = 0; k < a.rows; k++)
x[k] = a.colreplace(k,b).mdeterm()/det;
return 1;
}
return 0;
}
void main()
{
matrix a(4,4);
vector b, x;
a.input();
b.input();
if (a.issquare())
{
int status = cramer(a, b, x);
if (status)
{
printf("\nNghiem: ");
x.show();
}
else printf("\nHe suy bien\n");
}
else printf("\nHe khong tuong thich\n");
}
Được sửa bởi Admin ngày Thu Aug 30, 2012 7:59 pm; sửa lần 1.
Giới tính :
Đến từ : TPHCM
Ngày Tham gia : 03/04/2011
Tổng số bài gửi : 2292
Có hệ pt sau :
ax + by = cz
dx + ey = fz
D = ae - bd
Dx = ce - bf
Dy = af - cd
Ta giải được 2 nghiệm trên :
x = Dx/D
y = Dy/D
Trường hợp mà basilisk kể trên chỉ là trường hợp riêng của hệ Crame thôi.
Hệ Crame là hệ có ma trận liên kết A là ma trận vuông và det(A)!=0, tức là hệ PT tuyến tính n phương trình, n ẩn số. Muốn giải hệ này tổng quát thì đưa về ma trận, có 2 cách:
1) Làm theo đúng CT: giả sử hệ có nghiệm X=[xj] thì xj=det(Aj)/det(A)
2) Đưa ma trận đầy đủ A_ =[A|B] (B là cột hệ số) về dạng ma trận tam giác trên và giải từ dưới lên.
cách giải 1
Cách thứ nhất : Sử dụng ma trận nghịch đảo
- Code:
oid MatAlgebra(double **x,double **a)
{
int i,j,k;
double Sum,m;
double **b, **q;
b=new double *[N+1];
q=new double *[N+1];
for (i=1; i<=N; i++)
{
b[i] = new double [N+1];
q[i] = new double [N+1];
}
for (i = 1; i <= N;i++)
for (j = 1; j <= N;j++)
{
b[i][j] = 0;
q[i][j] = a[i][j];
if (i == j)
b[i][j] = 1;
}
// Perform row operations
for (k = 1;k <= N-1;k++)
for (i = k+1;i <= N;i++)
{
m = q[i][k]/q[k][k];
for (j = 1;j <= N;j++)
{
q[i][j] -= m*q[k][j];
b[i][j] -= m*b[k][j];
}
}
}
- Code:
double Determinant(double **a,int n)
{
int i,j,j1,j2;
double det = 0;
double **m = NULL;
if (n < 1) { /* Error */
} else if (n == 1) { /* Shouldn't get used */
det = a[0][0];
} else if (n == 2) {
det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
} else {
det = 0;
for (j1=0;j1<n;j1++) {
m = malloc((n-1)*sizeof(double *));
for (i=0;i<n-1;i++)
m[i] = malloc((n-1)*sizeof(double));
for (i=1;i<n;i++) {
j2 = 0;
for (j=0;j<n;j++) {
if (j == j1)
continue;
m[i-1][j2] = a[i][j];
j2++;
}
}
det += pow(-1.0,1.0+j1+1.0) * a[0][j1] * Determinant(m,n-1);
for (i=0;i<n-1;i++)
free(m[i]);
free(m);
}
}
return(det);
}
Giới tính :
Tuổi : 34
Đến từ : can tho
Ngày Tham gia : 22/08/2012
Tổng số bài gửi : 12
Giới tính :
Đến từ : TPHCM
Ngày Tham gia : 03/04/2011
Tổng số bài gửi : 2292
nienluan1 đã viết:Admin giúp mình làm hoàn chỉnh bài này nha mình học yếu môn này lắm
đây bạn hoàn chỉnh rùi mà.mình bận lắm ko có nhiêu thời gian cho mấy việc này.
bạn test xem
- Code:
#include <stdio.h>
#define nmax 20
class vector
{
double v[nmax];
int size;
public:
vector(int _size = nmax)
{
size = _size;
for (int k = 0; k < nmax; k++) v[k] = 0;
}
double &operator [] (int k) { return v[k];}
void show();
void input();
};
void vector::input()
{
printf("\nSo phan tu cua vec to: ");
scanf("%d",&size);
for (int k = 0; k < size; k++)
{
printf("[%d] = ", k);
scanf("%lf", &v[k]);
}
}
void vector::show()
{
for (int k = 0; k < size; k++) printf("%8.2lf",v[k]);
printf("\n");
}
class matrix
{
vector v[nmax];
int rows, cols;
public:
matrix(int r = nmax, int c = nmax)
{
rows = r; cols = c;
for (int k = 0; k < nmax; k++) v[k] = vector();
}
vector &operator [] (int k) { return v[k];}
int issquare();
matrix M(int, int);
double mdeterm();
matrix colreplace(int, vector);
friend int cramer(matrix, vector, vector &);
void input();
void show();
};
int matrix::issquare() { return rows==cols;}
void matrix::input()
{
printf("\nSo hang cua ma tran: ");
scanf("%d",&rows);
printf("\nSo cot cua ma tran: ");
scanf("%d",&cols);
for (int k = 0; k < rows; k++)
for (int j = 0; j < cols; j++)
{
printf("[%d][%d] = ", k,j);
scanf("%lf", &v[k][j]);
}
}
void matrix::show()
{
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++) printf("%8.2lf",v[k][j]);
printf("\n");
}
}
matrix matrix::M(int m, int n)
{
matrix b = *this;
int k, j;
for (k = m; k < rows-1; k++)
for (j = 0; j < cols; j++) b[k][j] = b[k+1][j];
for (j = n; j < cols-1; j++)
for (k = 0; k < rows; k++) b[k][j] = b[k][j+1];
b.rows = rows-1; b.cols = cols-1;
return b;
}
double matrix::mdeterm()
{
if (rows == 1) return v[0][0];
double temp = 0;
for (int k = rows-1; k >=0; k--)
if ((cols + k) % 2)
temp -= v[k][cols-1]*M(k,cols-1).mdeterm();
else
temp += v[k][cols-1]*M(k,cols-1).mdeterm();
return temp;
}
matrix matrix::colreplace(int k, vector c)
{
matrix b = *this;
for (int j = 0; j < rows; j++) b[j][k] = c[j];
return b;
}
int cramer(matrix a, vector b, vector &x)
{
double det = a.mdeterm();
if (det)
{
x = vector(a.rows);
for (int k = 0; k < a.rows; k++)
x[k] = a.colreplace(k,b).mdeterm()/det;
return 1;
}
return 0;
}
void main()
{
matrix a(4,4);
vector b, x;
a.input();
b.input();
if (a.issquare())
{
int status = cramer(a, b, x);
if (status)
{
printf("\nNghiem: ");
x.show();
}
else printf("\nHe suy bien\n");
}
else printf("\nHe khong tuong thich\n");
}
Giới tính :
Tuổi : 34
Đến từ : can tho
Ngày Tham gia : 22/08/2012
Tổng số bài gửi : 12
tại sau vậy Admin (Giúp mình nha )
Giới tính :
Tuổi : 34
Đến từ : can tho
Ngày Tham gia : 22/08/2012
Tổng số bài gửi : 12
Admin đã viết:Đoạn code này mình đưa vào c++ khi nhập số dòng số cột là nó thoat ra ha. Sau vậy Adminnienluan1 đã viết:Admin giúp mình làm hoàn chỉnh bài này nha mình học yếu môn này lắm
- Code:
#include <stdio.h>
#define nmax 20
class vector
{
double v[nmax];
int size;
public:
vector(int _size = nmax)
{
size = _size;
for (int k = 0; k < nmax; k++) v[k] = 0;
}
double &operator [] (int k) { return v[k];}
void show();
void input();
};
void vector::input()
{
printf("\nSo phan tu cua vec to: ");
scanf("%d",&size);
for (int k = 0; k < size; k++)
{
printf("[%d] = ", k);
scanf("%lf", &v[k]);
}
}
void vector::show()
{
for (int k = 0; k < size; k++) printf("%8.2lf",v[k]);
printf("\n");
}
class matrix
{
vector v[nmax];
int rows, cols;
public:
matrix(int r = nmax, int c = nmax)
{
rows = r; cols = c;
for (int k = 0; k < nmax; k++) v[k] = vector();
}
vector &operator [] (int k) { return v[k];}
int issquare();
matrix M(int, int);
double mdeterm();
matrix colreplace(int, vector);
friend int cramer(matrix, vector, vector &);
void input();
void show();
};
int matrix::issquare() { return rows==cols;}
void matrix::input()
{
printf("\nSo hang cua ma tran: ");
scanf("%d",&rows);
printf("\nSo cot cua ma tran: ");
scanf("%d",&cols);
for (int k = 0; k < rows; k++)
for (int j = 0; j < cols; j++)
{
printf("[%d][%d] = ", k,j);
scanf("%lf", &v[k][j]);
}
}
void matrix::show()
{
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++) printf("%8.2lf",v[k][j]);
printf("\n");
}
}
matrix matrix::M(int m, int n)
{
matrix b = *this;
int k, j;
for (k = m; k < rows-1; k++)
for (j = 0; j < cols; j++) b[k][j] = b[k+1][j];
for (j = n; j < cols-1; j++)
for (k = 0; k < rows; k++) b[k][j] = b[k][j+1];
b.rows = rows-1; b.cols = cols-1;
return b;
}
double matrix::mdeterm()
{
if (rows == 1) return v[0][0];
double temp = 0;
for (int k = rows-1; k >=0; k--)
if ((cols + k) % 2)
temp -= v[k][cols-1]*M(k,cols-1).mdeterm();
else
temp += v[k][cols-1]*M(k,cols-1).mdeterm();
return temp;
}
matrix matrix::colreplace(int k, vector c)
{
matrix b = *this;
for (int j = 0; j < rows; j++) b[j][k] = c[j];
return b;
}
int cramer(matrix a, vector b, vector &x)
{
double det = a.mdeterm();
if (det)
{
x = vector(a.rows);
for (int k = 0; k < a.rows; k++)
x[k] = a.colreplace(k,b).mdeterm()/det;
return 1;
}
return 0;
}
void main()
{
matrix a(4,4);
vector b, x;
a.input();
b.input();
if (a.issquare())
{
int status = cramer(a, b, x);
if (status)
{
printf("\nNghiem: ");
x.show();
}
else printf("\nHe suy bien\n");
}
else printf("\nHe khong tuong thich\n");
}
Giới tính :
Tuổi : 31
Đến từ : Cần thơ
Ngày Tham gia : 10/01/2012
Tổng số bài gửi : 750
Giới tính :
Tuổi : 34
Đến từ : can tho
Ngày Tham gia : 22/08/2012
Tổng số bài gửi : 12
Tony Stack đã viết:bạn nhập sai
Sai Chỗ Nào Vậy Bạn Chỉ Giúp Mình Với
|
|