Advertisement
M_A_Tabarani

Subtraction of Matrices

Nov 9th, 2015
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. const int MAX_ROWS = 6;
  9. const int MAX_COLS = 6;
  10. class Matrix
  11. {
  12. public:Matrix () {}
  13.     Matrix (int, int, int);
  14.     Matrix& operator-=(const Matrix& );
  15. private:
  16.     int rows;
  17.     int cols;
  18.     int mat[MAX_ROWS][MAX_ROWS];
  19.     friend ostream& operator << (ostream&, const Matrix&);
  20.     friend istream& operator >> (istream&, Matrix&);
  21.     };
  22.     // Inputs two matrices from files designated by the user.
  23.     // Forms and displays their product.
  24.  
  25. int main()
  26. {
  27.     Matrix m1;
  28.     Matrix m2;
  29.  
  30.     string file1Name;
  31.     string file2Name;
  32.     cout << "Name of file containing first matrix => ";
  33.     cin >> file1Name;
  34.     ifstream infilOne (file1Name.c_str(), ios::in);
  35.     infilOne >> m1;
  36.     infilOne.close();
  37.     cout << "Name of file containing second matrix => ";
  38.     cin >> file2Name;
  39.     ifstream infilTwo (file2Name.c_str(), ios::in);
  40.     infilTwo >> m2;
  41.     infilTwo.close();
  42.     if (!infilOne.fail() && !infilTwo.fail())
  43.     {
  44.         cout << endl << "Matrix1" << endl << endl << m1 << endl << endl;
  45.         cout << "Matrix 2" << endl << endl << m2 << endl << endl;
  46.  
  47.         m1-=m2;
  48.  
  49.         cout << "Matrix 1 - Matrix 2 = " << endl << endl << m1 << endl << endl;
  50.     }
  51.     else
  52.         cout << "Error in input" << endl;
  53.     return 0;
  54. }
  55.     // Constructor that initializes a Matrix object of size
  56.     //initRows x initCols, setting matrix elements to initValue
  57.  
  58.     Matrix :: Matrix (int initRows, int initCols, int initValue)
  59.     {
  60.         rows = initRows;
  61.         cols = initCols;
  62.  
  63.         for(int i = 0; i < rows; ++i)
  64.             for (int j = 0; j < cols; ++j)
  65.             mat[i][j] = initValue;}
  66.         // Multiplies m1 by m2 if these matrices are comfortable.
  67.         // Otherwise, displays an error message and sets m1's size to 0 x 0
  68.     Matrix& Matrix :: operator-=(const Matrix& m2)
  69.     {
  70.         int val, i, j;
  71.         Matrix sub;
  72.         if (cols != m2.cols || rows != m2.rows)
  73.         {
  74.         cout << "Matrices are not comfortable." << endl;
  75.         sub.rows = 0;
  76.         sub.cols = 0;
  77.         }
  78.         else {
  79.         sub.rows = rows;sub.cols= m2.cols;
  80.         for (i = 0; i <= sub.rows; ++i)
  81.         for (j = 0; j <= sub.cols; ++j)
  82.         {
  83.                 val = mat[i][j] - m2.mat[i][j];
  84.             sub.mat[i][j] = val;
  85. }}*this = sub;return *this;}
  86.  
  87. //
  88. // Writes to the output stream the contents of matrix m,
  89. // one row at a time
  90. //
  91.  
  92. ostream& operator << (ostream& os, const Matrix& m)
  93. {
  94.     for (int i = 0; i < m.rows; ++i)
  95.         {
  96.             for (int j = 0; j < m.cols; ++j)
  97.             cout << setw (5) << m.mat[i][j];
  98.     cout << endl;
  99.     }
  100.     return os;
  101.     }
  102.  
  103. //
  104. // Get the number of rows and columns from the input stream.
  105. //If rows <+ MAX_ROWS and cols <+ MAX_CLOS, fills m with data from file;
  106. // Otherwise signals failure on stream is. In the event of input failure, sets m size to 0 x 0
  107. //
  108.  
  109. istream& operator >> (istream& is, Matrix& m)
  110. {
  111.     int i, j;
  112.     is >> m.rows >> m.cols;
  113.     if (is.fail())
  114.         {
  115.             m.rows = 0;m.cols = 0;
  116.     } else if (m.rows > MAX_ROWS ||m.cols > MAX_COLS)
  117.     {
  118.         is.setstate (ios::failbit);
  119.         m.rows = 0;
  120.         m.cols = 0;
  121.         }
  122.     else {
  123.             for (i =0; i < m.rows; ++i)
  124.             for (j = 0; j < m.cols; ++j)
  125.             is >> m.mat[i][j];
  126.             if (is.fail())
  127.             {
  128.                 m.rows = 0;
  129.     m.cols = 0;
  130.     }
  131.     }
  132.     return is;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement