Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- const int MAX_ROWS = 6;
- const int MAX_COLS = 6;
- class Matrix
- {
- public:Matrix () {}
- Matrix (int, int, int);
- Matrix& operator-=(const Matrix& );
- private:
- int rows;
- int cols;
- int mat[MAX_ROWS][MAX_ROWS];
- friend ostream& operator << (ostream&, const Matrix&);
- friend istream& operator >> (istream&, Matrix&);
- };
- // Inputs two matrices from files designated by the user.
- // Forms and displays their product.
- int main()
- {
- Matrix m1;
- Matrix m2;
- string file1Name;
- string file2Name;
- cout << "Name of file containing first matrix => ";
- cin >> file1Name;
- ifstream infilOne (file1Name.c_str(), ios::in);
- infilOne >> m1;
- infilOne.close();
- cout << "Name of file containing second matrix => ";
- cin >> file2Name;
- ifstream infilTwo (file2Name.c_str(), ios::in);
- infilTwo >> m2;
- infilTwo.close();
- if (!infilOne.fail() && !infilTwo.fail())
- {
- cout << endl << "Matrix1" << endl << endl << m1 << endl << endl;
- cout << "Matrix 2" << endl << endl << m2 << endl << endl;
- m1-=m2;
- cout << "Matrix 1 - Matrix 2 = " << endl << endl << m1 << endl << endl;
- }
- else
- cout << "Error in input" << endl;
- return 0;
- }
- // Constructor that initializes a Matrix object of size
- //initRows x initCols, setting matrix elements to initValue
- Matrix :: Matrix (int initRows, int initCols, int initValue)
- {
- rows = initRows;
- cols = initCols;
- for(int i = 0; i < rows; ++i)
- for (int j = 0; j < cols; ++j)
- mat[i][j] = initValue;}
- // Multiplies m1 by m2 if these matrices are comfortable.
- // Otherwise, displays an error message and sets m1's size to 0 x 0
- Matrix& Matrix :: operator-=(const Matrix& m2)
- {
- int val, i, j;
- Matrix sub;
- if (cols != m2.cols || rows != m2.rows)
- {
- cout << "Matrices are not comfortable." << endl;
- sub.rows = 0;
- sub.cols = 0;
- }
- else {
- sub.rows = rows;sub.cols= m2.cols;
- for (i = 0; i <= sub.rows; ++i)
- for (j = 0; j <= sub.cols; ++j)
- {
- val = mat[i][j] - m2.mat[i][j];
- sub.mat[i][j] = val;
- }}*this = sub;return *this;}
- //
- // Writes to the output stream the contents of matrix m,
- // one row at a time
- //
- ostream& operator << (ostream& os, const Matrix& m)
- {
- for (int i = 0; i < m.rows; ++i)
- {
- for (int j = 0; j < m.cols; ++j)
- cout << setw (5) << m.mat[i][j];
- cout << endl;
- }
- return os;
- }
- //
- // Get the number of rows and columns from the input stream.
- //If rows <+ MAX_ROWS and cols <+ MAX_CLOS, fills m with data from file;
- // Otherwise signals failure on stream is. In the event of input failure, sets m size to 0 x 0
- //
- istream& operator >> (istream& is, Matrix& m)
- {
- int i, j;
- is >> m.rows >> m.cols;
- if (is.fail())
- {
- m.rows = 0;m.cols = 0;
- } else if (m.rows > MAX_ROWS ||m.cols > MAX_COLS)
- {
- is.setstate (ios::failbit);
- m.rows = 0;
- m.cols = 0;
- }
- else {
- for (i =0; i < m.rows; ++i)
- for (j = 0; j < m.cols; ++j)
- is >> m.mat[i][j];
- if (is.fail())
- {
- m.rows = 0;
- m.cols = 0;
- }
- }
- return is;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement