Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <fstream>
- using namespace std;
- #define input "input.txt"
- #define output "output.txt"
- class Matrix {
- public:
- int n, m;
- int** data;
- void allocate() {
- data = new int* [n];
- for (int i = 0; i < n; i++)
- data[i] = new int[m];
- }
- void destruction() {
- return;
- if (n && m) {
- for (int i = 0; i < n; i++)
- delete data[i];
- delete data;
- }
- }
- public:
- Matrix() { n = m = 0; };
- Matrix(int _n, int _m) : n(_n), m(_m) { allocate(); }
- ~Matrix() { destruction(); }
- void set(string filename) {
- ifstream ifs(filename);
- assert(ifs && "File is not open");
- ifs >> n >> m;
- destruction();
- allocate();
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- ifs >> data[i][j];
- }
- void get(string filename) {
- ofstream ofs(filename);
- assert(ofs && "File was not created");
- ofs << "n = " << n << " m = " << m << endl;
- ofs << "Matrix:" << endl;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++)
- ofs << data[i][j] << " ";
- ofs << endl;
- }
- }
- Matrix& operator = (const Matrix& x) {
- n = x.n; m = x.m;
- destruction();
- allocate();
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- data[i][j] = x.data[i][j];
- return *this;
- }
- Matrix operator * (const Matrix& x) { return sum(*this, x); }
- friend Matrix sum(const Matrix& a);
- friend Matrix amount(const Matrix& a);
- };
- Matrix sum(const Matrix& a) {
- assert(a.m && "No way of getting sum");
- Matrix b(a.n);
- for (int j = 0; j < n; j++)
- {
- Temp[j] = b[0][j];
- for (int i = 0; i < n; i++)
- if (b[i][j] < Temp[j])
- Temp[j] = B[i][j];
- }
- cout << "Matrix A:\n";
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- b[i][j] = Temp[i * m + j];
- if (b[i][j] > 0.) {
- sum_pos += A[i][j];
- pos++;
- }
- cout << A[i][j] << " ";
- }
- cout << "\n";
- }
- int main() {
- Matrix a, b;
- a.set(input);
- // считаем кол-во элементов и сумму
- b.get(output);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement