em không biết sao mà nó không cho nhập hệ số. Khi nhập thì báo lỗi: Unhandled exception at 0x00f91641 in giải hệ phương trình.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
// giải hệ phương trình.cpp
// giải hệ phương trình.cpp
Code:
#include "stdafx.h" #include "malloc.h" #include "math.h" #include "iostream" using namespace std; float** heso; float x,y,z,t,d,dx,dy,dz,dt; void nhap( float** heso, int dem, int so_an); float dinhthuccap2(float** heso, int x, int y); float dinhthuccap3(float** heso, int x, int y, int z); float dinhthuccap4(float** heso, int x, int y, int z, int t); void tinhdinhthuc(float** heso, int dem, int so_an); void xuatnghiem(int dem, int so_an); void main() { int so_an,dem; char k='y'; cout<<"\n\n Giai he phuong trinh \n\n"; do { cout<<" Giai he 2 an: 2\n Giai he 3 an: 3\n Giai he 4 an: 4\n"; cin>>so_an; switch(so_an) { case 2: dem=3; heso=(float**)malloc(6*sizeof(float*)); break; case 3: dem=4; heso=(float**)malloc(12*sizeof(float*)); break; case 4: dem=5; heso=(float**)malloc(20*sizeof(float*)); break; } nhap(heso,dem,so_an); tinhdinhthuc(heso,dem,so_an); xuatnghiem(dem,so_an); cout<<"Tiep tuc thuc hien chuong trinh? (Y/N)"; cin>>k; } while (k=='y'||k=='Y'); } void nhap( float** heso, int dem, int so_an) { for (int i=0;i<dem;i++) for (int j=0;j<so_an;j++) { cout<<"nhap he so A["<<i+1<<"]["<<j+1<<"] "; cin>>heso[i][j]; } } float dinhthuccap2(float** heso, int a, int b) { return (heso[a][a]*heso[b][b]-heso[b][a]*heso[a][b]); } float dinhthuccap3(float** heso, int a, int b, int c) { return ((heso[a][a]*heso[b][b]*heso[c][c]+heso[a][b]*heso[b][c]*heso[c][a]+heso[b][a]*heso[c][b]*heso[a][c])-(heso[c][a]*heso[b][b]*heso[a][c]+heso[a][b]*heso[b][c]*heso[c][c]+heso[b][c]*heso[c][b]*heso[a][a])); } float dinhthuccap4(float** heso, int a, int b, int c, int d) { return pow((float)(-1),(float)(a+a))*(heso[a][a])*dinhthuccap3(heso,b,c,d)+pow((float)(-1),(float)(a+b))*(heso[a][b])*dinhthuccap3(heso,a,c,d)+pow((float)(-1),(float)(a+c))*(heso[a][c])*dinhthuccap3(heso,a,b,d)+pow((float)(-1),(float)(a+d))*(heso[a][d])*dinhthuccap3(heso,a,b,c); } void tinhdinhthuc(float** heso,int dem, int so_an) { float d,dx,dy,dz,dt; switch(so_an) { case 2: d=dinhthuccap2(heso,1,2); dx=dinhthuccap2(heso,2,3); dy=dinhthuccap2(heso,1,3); break; case 3: d=dinhthuccap3(heso,1,2,3); dx=dinhthuccap3(heso,2,3,4); dy=dinhthuccap3(heso,1,3,4); dz=dinhthuccap3(heso,1,2,4); break; case 4: d=dinhthuccap4(heso,1,2,3,4); dx=dinhthuccap4(heso,2,3,4,5); dy=dinhthuccap4(heso,1,3,4,5); dz=dinhthuccap4(heso,1,2,4,5); dt=dinhthuccap4(heso,1,2,3,5); break; } } void xuatnghiem( int dem, int so_an) { if (d==0) { switch(so_an) { case 2: if (dx!=0||dy!=0) cout<<" phuong trinh vo nghiem"; else cout<<" phuong trinh vo so nghiem";break; case 3: if (dx!=0||dy!=0||dz!=0) cout<<" phuong trinh vo nghiem"; else cout<<" phuong trinh vo so nghiem";break; case 4: if (dx!=0||dy!=0||dz!=0||dt!=0) cout<<" phuong trinh vo nghiem"; else cout<<" phuong trinh vo so nghiem"; break; } } else switch (so_an) { case 2: x= dx/d; y= dy/d; cout<<"x= "<<x<<"\ny= "<<y<<endl; break; case 3: x= dx/d; y= dy/d; z=dz/d; cout<<"x= "<<x<<"\ny= "<<y<<"z= "<<z<<endl; break; case 4: x= dx/d; y= dy/d; z=dz/d; t= dt/d; cout<<"x= "<<x<<"\ny= "<<y<<"\nz= "<<z<<"\nt= "<<t<<endl; break; } }
Comment