Advertisement
Guest User

Untitled

a guest
May 28th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. //============================================================================
  2. // Name        : MatrixBinary.cpp
  3. // Author      : Eugene
  4. // Version     :
  5. // Copyright   : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <fstream>
  13. using namespace std;
  14.  
  15. class Matrix {
  16. public:
  17.     void generateMatrix(int n, int m);
  18.     int** getMatrix();
  19.     void  showMatrix();
  20.     void getSizes(int* n, int* m);
  21. private:
  22.     int n;
  23.     int m;
  24.     int** matrix;
  25. };
  26.  
  27. void Matrix::getSizes(int* n, int* m) {
  28.     *n = this->n;
  29.     *m = this->m;
  30. }
  31.  
  32. void Matrix::generateMatrix(int n, int m) {
  33.     this->n = n;
  34.     this->m = m;
  35.     this->matrix = new int *[n];
  36.     for (int i = 0; i < n; i++)
  37.         this->matrix[i] = new int[m];
  38.  
  39.     for (int i = 0; i < n; i++) {
  40.         for (int j = 0; j < m; j++) {
  41.             this->matrix[i][j] = rand() % 10;
  42.         }
  43.     }
  44. }
  45. void Matrix::showMatrix() {
  46.     for (int i = 0; i < this->n; i++) {
  47.         for (int j = 0; j < this->m; j++) {
  48.             cout << this->matrix[i][j] << " ";
  49.         }
  50.         cout << endl;
  51.     }
  52.     cout << endl;
  53. }
  54.  
  55. int STRUCT_SIZE = sizeof(Matrix);
  56.  
  57. class binaryFileManager {
  58. public:
  59.     bool isExist(char* filename);
  60.     Matrix* readSctruct(char* filename, int* count);
  61.     void writeStructs(char* filename, Matrix* list, int count,
  62.             bool toEnd);
  63. };
  64.  
  65. bool binaryFileManager::isExist(char* filename) {
  66.     ifstream f(filename);
  67.     if (f.good()) {
  68.         f.close();
  69.         return true;
  70.     } else {
  71.         f.close();
  72.         return false;
  73.     }
  74. }
  75.  
  76. Matrix* binaryFileManager::readSctruct(char* filename, int* count) {
  77.     //ifstream ofs;
  78.     ifstream ofs(filename, ios::binary | ios::in);
  79.     cout << "Norm";
  80.     *count = 0;
  81.     Matrix* list = new Matrix[256];
  82.     while (!ofs.eof()) {
  83.         ofs.read((char*) &list[*count], STRUCT_SIZE);
  84.         if (!ofs.eof())
  85.             (*count)++;
  86.     }
  87.     ofs.close();
  88.     return list;
  89. }
  90. void binaryFileManager::writeStructs(char* filename, Matrix* list,
  91.         int count, bool toEnd) {
  92.     if (!toEnd) {
  93.         ofstream myFile(filename, ios::out | ios::binary);
  94.         for (int i = 0; i < count; i++)
  95.             myFile.write((char*) &list[i], STRUCT_SIZE);
  96.         myFile.close();
  97.     } else {
  98.         ofstream myFile(filename, ios::app | ios::binary);
  99.         for (int i = 0; i < count; i++)
  100.             myFile.write((char*) &list[i], STRUCT_SIZE);
  101.         myFile.close();
  102.     }
  103. }
  104.  
  105. class mainController {
  106. public:
  107.     void mainLogic();
  108. private:
  109.     binaryFileManager bfm;
  110.  
  111.     void checkIsExist();
  112.     void enterFileNames();
  113.     void generateFile(char* filename);
  114.     void readFile(char* filename);
  115.     char firstFile[256];
  116.     char secondFile[256];
  117. };
  118.  
  119. void mainController::checkIsExist() {
  120.     if (!this->bfm.isExist(this->firstFile)) {
  121.         cout << "File " << this->firstFile << " not exist. Generating.." << endl;
  122.         this->generateFile(this->firstFile);
  123.     }
  124.     if (!this->bfm.isExist(this->secondFile)) {
  125.         cout << "File " << this->secondFile << " not exist. Generating.." << endl;
  126.         this->generateFile(this->secondFile);
  127.     }
  128. }
  129.  
  130. void mainController::enterFileNames() {
  131.     cout << "Enter name of first file: ";
  132.     cin >> this->firstFile;
  133.     cout << "Enter name of second file: ";
  134.     cin >> this->secondFile;
  135. }
  136.  
  137. void mainController::mainLogic() {
  138.     this->enterFileNames();
  139.     this->checkIsExist();
  140.     cout << "Content of file: " << this->firstFile << endl;
  141.     this->readFile(this->firstFile);
  142.     cout << "Content of file: " << this->secondFile << endl;
  143.     this->readFile(this->secondFile);
  144. }
  145.  
  146. void mainController::readFile(char* filename) {
  147.     int count;
  148.     Matrix* list = this->bfm.readSctruct(filename, &count);
  149.     for (int i = 0; i < count; i++) {
  150.         list[i].showMatrix();
  151.     }
  152. }
  153.  
  154. void mainController::generateFile(char* filename) {
  155.     int count,m,n;
  156.     cout << "Enter count of matrix: ";
  157.     cin >> count;
  158.     cout << "Enter n: ";
  159.     cin >> n;
  160.     cout << "Enter m: ";
  161.     cin >> m;
  162.     Matrix* list = new Matrix[count];
  163.     for (int i =0;i<count;i++) {
  164.         list[i].generateMatrix(n,m);
  165.     }
  166.     this->bfm.writeStructs(filename,list,count,0);
  167. }
  168.  
  169. int main() {
  170.     mainController mm;
  171.     mm.mainLogic();
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement