Hellko

matrix

Mar 13th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. class matrix {                  //////////////
  6. private:                        //  a   b   //
  7.     int a,b,c,d;                //  c   d   //
  8. public:                         //////////////
  9.     matrix(void);
  10.     matrix(int,int,int,int);
  11.     matrix(const matrix&);
  12.     int det(void);                        ////    ///    ///           ///  /////////      /////////
  13.     void show(const char[255]="\b");     /////    ///    ///          ///  ///     ///    ///     ///
  14.     void operator=(const matrix&);      // ///    ///    ///         ///   ///     ///    ///     ///
  15.     matrix operator+(matrix);          //  ///    ///    ///        ///    ///     ///    ///     ///
  16.     matrix operator*(matrix);              ///    //////////       ///      /////////      /////////
  17.     void operator+=(matrix);               ///           ///      ///       /////////      /////////
  18.     void operator*=(matrix);               ///           ///     ///       ///     ///    ///     ///
  19.     matrix operator-(void);                ///           ///    ///        ///     ///    ///     ///
  20.     matrix operator-(matrix);              ///           ///   ///         ///     ///    ///     ///
  21.     void operator-=(matrix);             ///////         ///  ///           /////////      /////////
  22. };
  23. void matrix::operator -=(matrix A) {
  24.     (*this)=(*this)-A;
  25. }
  26.  
  27. void matrix::operator *=(matrix A) {
  28.     (*this)=(*this)*A;
  29. }
  30.  
  31. void matrix::operator +=(matrix A) {
  32.     (*this)=(*this)+A;
  33. }
  34.  
  35. matrix matrix::operator*(matrix A) {
  36.     return matrix(a*A.a+b*A.c, a*A.b+b*A.d, c*A.a+d*A.c, c*A.b+d*A.d);
  37. }
  38.  
  39. matrix matrix::operator-(matrix A) {
  40.     return -A+(*this);
  41. }
  42.  
  43. matrix matrix::operator-(void) {
  44.     return matrix(-a,-b,-c,-d);
  45. }
  46.  
  47. matrix matrix::operator+(matrix A) {
  48.     return matrix(a+A.a,b+A.b,c+A.c,d+A.d);
  49. }
  50.  
  51. void matrix::operator =(const matrix& A) {
  52.     a=A.a; b=A.b; c=A.c; d=A.d;
  53. }
  54.  
  55. matrix::matrix() {
  56.     a=1; b=0; c=0; d=1;
  57. }
  58.  
  59. matrix::matrix(int a1, int b1, int c1, int d1) {
  60.     a=a1; b=b1; c=c1; d=d1;
  61. }
  62.  
  63. matrix::matrix(const matrix &A) {
  64.     a=A.a; b=A.b; c=A.c; d=A.d;
  65. }
  66.  
  67. int matrix::det() {
  68.     return a*d-b*c;
  69. }
  70.  
  71. void matrix::show(const char str[255]) {
  72.     cout<<"Matrix "<<str<<":"<<endl;
  73.     cout<<setw(5)<<a<<setw(5)<<b<<endl;
  74.     cout<<setw(5)<<c<<setw(5)<<d<<endl<<endl;
  75. }
  76.  
  77. int main() {
  78.     matrix A(1,2,3,4),B(4,5,6,7),C;
  79.     A.show("A");
  80.     B.show("B");
  81.     C.show("C");
  82.     C=A*B;
  83.     C.show("C=A*B");
  84.     cout<<"Det[A]: "<<A.det()<<endl;
  85.     cout<<"Det[B]: "<<B.det()<<endl;
  86.     cout<<"Det[C]: "<<C.det()<<endl;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment