⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inverse.cpp

📁 这是一个利用VC++编写的求逆矩阵的算法
💻 CPP
字号:
 /*This program calculates inverse of a 3x3 matrix.
 Copyright (c) reserved Shahab Faruqi 2001  */

 //inverse.cpp

 #include<dos.h>
 #include<math.h>
 #include<fstream.h>
 #include<conio.h>
 #include<process.h>

 /*This function plays the opening sound of inverse.cpp. It also plays when
 there is an error or when then program terminates*/

 void sound()
 {
 sound(1800);
 delay(100);
 sound(1000);
 delay(160);
 nosound();
 }

 /*This function gives the background color and text color to the input/output
 of inverse.cpp. To change the color change the number inside the brackets*/

 void background()
 {
 textbackground(1);
 clrscr();
 textcolor(15);
 }

 void main()
 {
 background();                /*Call to function background*/
 sound();                     /*Call to function sound*/
 float x,y,z,p,q,r,a,b,c;
 textcolor(15);
 cout<<"\n    enter the elements of the matrix: ";
 gotoxy(5,5);cin>>x;          /*This portion of the code makes the*/
 gotoxy(15,5);cin>>y;         /*user enter values of the matrix whose*/
 gotoxy(25,5);cin>>z;         /*inverse is to be found*/
 gotoxy(5,7);cin>>p;
 gotoxy(15,7);cin>>q;
 gotoxy(25,7);cin>>r;
 gotoxy(5,9);cin>>a;
 gotoxy(15,9);cin>>b;
 gotoxy(25,9);cin>>c;
 clrscr();float det;
 int flag=0;
 /*calculate determinant*/
 det=x*((q*c)-(b*r))-y*((p*c)-(a*r))+z*((p*b)-(q*a));
 if(det==0)                   /*if matrix is singular program terminates*/
 {sound();
 cout<<"\n Given matrix is either singular and hence not invertible or their is error in";
 cout<<"   data. Check to see whether all values are correctly entered.\n\n This program will now terminate.";
 getch();exit(1);}
 else if(det<0)flag=1;
 float val=1/det;
 float x1,y1,z1,p1,q1,r1,a1,b1,c1;
 x1=((q*c)-(b*r));            /*calculate cofactors*/
 y1=((-1)*((p*c)-(a*r)));
 z1=((p*b)-(q*a));
 p1=((-1)*((y*c)-(b*z)));
 q1=((x*c)-(a*z));
 r1=((-1)*((x*b)-(a*y)));
 a1=((y*r)-(q*z));
 b1=((-1)*((x*r)-(p*z)));
 c1=((x*q)-(p*y));
 sound();
 cout<<"\nThe inverse of the given matrix is: ";
 gotoxy(1,7);if(flag==1)cout<<"-1/";else cout<<"1/";
 cout<<fabs(det);          /*absolute value of determinant*/
 gotoxy(10,5);cout<<x1;    /*prints output without multiplying by*/
 gotoxy(20,5);cout<<p1;    /*determinant*/
 gotoxy(30,5);cout<<a1;
 gotoxy(10,7);cout<<y1;
 gotoxy(20,7);cout<<q1;
 gotoxy(30,7);cout<<b1;
 gotoxy(10,9);cout<<z1;
 gotoxy(20,9);cout<<r1;
 gotoxy(30,9);cout<<c1;
 gotoxy(40,7);cout<<"or";
 x1*=val;y1*=val;z1*=val;  /*multiply each value by determinant*/
 p1*=val;q1*=val;r1*=val;
 a1*=val;b1*=val;c1*=val;
 gotoxy(10,12);cout<<x1;   /*prints output*/
 gotoxy(20,12);cout<<p1;
 gotoxy(30,12);cout<<a1;
 gotoxy(10,14);cout<<y1;
 gotoxy(20,14);cout<<q1;
 gotoxy(30,14);cout<<b1;
 gotoxy(10,16);cout<<z1;
 gotoxy(20,16);cout<<r1;
 gotoxy(30,16);cout<<c1;
 getch();                  /*wait for key press*/
 clrscr();
 gotoxy(25,11);
 cout<<"Thank You for using this program.\n";
 gotoxy(25,12);
 cout<<"Copyright reserved (c) Shahab Faruqi,2001";
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -