Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- #define inputfilename "input.txt"
- #define outputfilename "result.txt"
- #define logfilename "log.txt"
- #define ERROR_MEMPTY "ERROR: matrix is empty.\n"
- #define ERROR_REMPTY "ERROR: result was not calculated.\n"
- ofstream ERROR_LOG(logfilename, ios::app);
- struct result {
- int* sum,
- * count;
- result() {
- sum = NULL;
- count = NULL;
- }
- void create() {
- sum = new int;
- *sum = 0;
- count = new int;
- *count = 0;
- }
- bool is_empty() {
- if (sum == NULL || count == NULL) return 1;
- return 0;
- }
- };
- class matrix {
- int size_line,
- size_column,
- ** p_matrix;
- public:
- matrix() {
- size_line = 0;
- size_column = 0;
- p_matrix = NULL;
- }
- matrix(int _size_line, int _size_column) {
- size_line = _size_line;
- size_column = _size_column;
- p_matrix = new int* [size_line];
- for (int i_line = 0; i_line < size_line; i_line++)
- p_matrix[i_line] = new int[size_column]();
- }
- void finput(ifstream& fin) {
- fin >> size_line;
- fin >> size_column;
- p_matrix = new int* [size_line];
- for (int i_line = 0; i_line < size_line; i_line++) {
- p_matrix[i_line] = new int[size_column];
- for (int i_column = 0; i_column < size_column; i_column++)
- fin >> p_matrix[i_line][i_column];
- }
- }
- void fprint(ofstream& fout) {
- if (p_matrix == NULL) {
- fout << "in fprint():\n" << ERROR_MEMPTY;
- ERROR_LOG << "in fprint():\n" << ERROR_MEMPTY;
- return;
- }
- for (int i_line = 0; i_line < size_line; i_line++) {
- for (int i_column = 0; i_column < size_column; i_column++)
- fout << p_matrix[i_line][i_column] << '\t';
- fout << endl;
- }
- }
- result set_result() {
- result res;
- if (p_matrix == NULL) {
- ERROR_LOG << "in set_result():\n" << ERROR_MEMPTY;
- return res;
- }
- res.create();
- for (int i_line = 0; i_line < size_line; i_line++)
- for (int i_column = 0; i_column < size_column; i_column++)
- if (p_matrix[i_line][i_column] > 0) {
- (*res.count)++;
- *res.sum += p_matrix[i_line][i_column];
- }
- return res;
- }
- void fget_result(ofstream& fout, result res) {
- if (res.is_empty()) {
- fout << "in fget_result():\n" << ERROR_REMPTY;
- ERROR_LOG << "in fget_result():\n" << ERROR_REMPTY;
- return;
- }
- fout << "sum = " << *res.sum << endl;
- fout << "count = " << *res.count << endl;
- }
- };
- int main() {
- tm* ptr = new tm;
- time_t *lt = new time_t;
- *lt = time(NULL);
- localtime_s(ptr, lt);
- char nowtime[100];
- asctime_s(nowtime, ptr);
- ERROR_LOG << nowtime << endl;
- matrix A;
- ifstream fin(inputfilename);
- ofstream fout(outputfilename);
- A.finput(fin);
- A.fprint(fout);
- fout << endl;
- result res;
- res = A.set_result();
- A.fget_result(fout, res);
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement