Advertisement
DacCum

ООП лаб 11

Nov 10th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. #define inputfilename "input.txt"
  7. #define outputfilename "result.txt"
  8. #define logfilename "log.txt"
  9. #define ERROR_MEMPTY "ERROR: matrix is empty.\n"
  10. #define ERROR_REMPTY "ERROR: result was not calculated.\n"
  11.  
  12. ofstream ERROR_LOG(logfilename, ios::app);
  13.  
  14. struct result {
  15.     int* sum,
  16.         * count;
  17.     result() {
  18.         sum = NULL;
  19.         count = NULL;
  20.     }
  21.     void create() {
  22.         sum = new int;
  23.         *sum = 0;
  24.         count = new int;
  25.         *count = 0;
  26.     }
  27.     bool is_empty() {
  28.         if (sum == NULL || count == NULL) return 1;
  29.         return 0;
  30.     }
  31. };
  32.  
  33. class matrix {
  34.     int size_line,
  35.         size_column,
  36.         ** p_matrix;
  37. public:
  38.     matrix() {
  39.         size_line = 0;
  40.         size_column = 0;
  41.         p_matrix = NULL;
  42.     }
  43.     matrix(int _size_line, int _size_column) {
  44.         size_line = _size_line;
  45.         size_column = _size_column;
  46.         p_matrix = new int* [size_line];
  47.         for (int i_line = 0; i_line < size_line; i_line++)
  48.             p_matrix[i_line] = new int[size_column]();
  49.     }
  50.     void finput(ifstream& fin) {
  51.         fin >> size_line;
  52.         fin >> size_column;
  53.         p_matrix = new int* [size_line];
  54.         for (int i_line = 0; i_line < size_line; i_line++) {
  55.             p_matrix[i_line] = new int[size_column];
  56.             for (int i_column = 0; i_column < size_column; i_column++)
  57.                 fin >> p_matrix[i_line][i_column];
  58.         }
  59.     }
  60.     void fprint(ofstream& fout) {
  61.         if (p_matrix == NULL) {
  62.             fout << "in fprint():\n" << ERROR_MEMPTY;
  63.             ERROR_LOG << "in fprint():\n" << ERROR_MEMPTY;
  64.             return;
  65.         }
  66.  
  67.         for (int i_line = 0; i_line < size_line; i_line++) {
  68.             for (int i_column = 0; i_column < size_column; i_column++)
  69.                 fout << p_matrix[i_line][i_column] << '\t';
  70.             fout << endl;
  71.         }
  72.     }
  73.     result set_result() {
  74.         result res;
  75.         if (p_matrix == NULL) {
  76.             ERROR_LOG << "in set_result():\n" << ERROR_MEMPTY;
  77.             return res;
  78.         }
  79.  
  80.         res.create();
  81.         for (int i_line = 0; i_line < size_line; i_line++)
  82.             for (int i_column = 0; i_column < size_column; i_column++)
  83.                 if (p_matrix[i_line][i_column] > 0) {
  84.                     (*res.count)++;
  85.                     *res.sum += p_matrix[i_line][i_column];
  86.                 }
  87.  
  88.         return res;
  89.     }
  90.     void fget_result(ofstream& fout, result res) {
  91.         if (res.is_empty()) {
  92.             fout << "in fget_result():\n" << ERROR_REMPTY;
  93.             ERROR_LOG << "in fget_result():\n" << ERROR_REMPTY;
  94.             return;
  95.         }
  96.         fout << "sum = " << *res.sum << endl;
  97.         fout << "count = " << *res.count << endl;
  98.     }
  99. };
  100.  
  101. int main() {
  102.     tm* ptr = new tm;
  103.     time_t *lt = new time_t;
  104.     *lt = time(NULL);
  105.     localtime_s(ptr, lt);
  106.     char nowtime[100];
  107.     asctime_s(nowtime, ptr);
  108.     ERROR_LOG << nowtime << endl;
  109.  
  110.     matrix A;
  111.     ifstream fin(inputfilename);
  112.     ofstream fout(outputfilename);
  113.     A.finput(fin);
  114.     A.fprint(fout);
  115.     fout << endl;
  116.     result res;
  117.     res = A.set_result();
  118.     A.fget_result(fout, res);
  119.  
  120.     fin.close();
  121.     fout.close();
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement