Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : MatrixBinary.cpp
- // Author : Eugene
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <fstream>
- using namespace std;
- class Matrix {
- public:
- void generateMatrix(int n, int m);
- int** getMatrix();
- void showMatrix();
- void getSizes(int* n, int* m);
- private:
- int n;
- int m;
- int** matrix;
- };
- void Matrix::getSizes(int* n, int* m) {
- *n = this->n;
- *m = this->m;
- }
- void Matrix::generateMatrix(int n, int m) {
- this->n = n;
- this->m = m;
- this->matrix = new int *[n];
- for (int i = 0; i < n; i++)
- this->matrix[i] = new int[m];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- this->matrix[i][j] = rand() % 10;
- }
- }
- }
- void Matrix::showMatrix() {
- for (int i = 0; i < this->n; i++) {
- for (int j = 0; j < this->m; j++) {
- cout << this->matrix[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- }
- int STRUCT_SIZE = sizeof(Matrix);
- class binaryFileManager {
- public:
- bool isExist(char* filename);
- Matrix* readSctruct(char* filename, int* count);
- void writeStructs(char* filename, Matrix* list, int count,
- bool toEnd);
- };
- bool binaryFileManager::isExist(char* filename) {
- ifstream f(filename);
- if (f.good()) {
- f.close();
- return true;
- } else {
- f.close();
- return false;
- }
- }
- Matrix* binaryFileManager::readSctruct(char* filename, int* count) {
- //ifstream ofs;
- ifstream ofs(filename, ios::binary | ios::in);
- cout << "Norm";
- *count = 0;
- Matrix* list = new Matrix[256];
- while (!ofs.eof()) {
- ofs.read((char*) &list[*count], STRUCT_SIZE);
- if (!ofs.eof())
- (*count)++;
- }
- ofs.close();
- return list;
- }
- void binaryFileManager::writeStructs(char* filename, Matrix* list,
- int count, bool toEnd) {
- if (!toEnd) {
- ofstream myFile(filename, ios::out | ios::binary);
- for (int i = 0; i < count; i++)
- myFile.write((char*) &list[i], STRUCT_SIZE);
- myFile.close();
- } else {
- ofstream myFile(filename, ios::app | ios::binary);
- for (int i = 0; i < count; i++)
- myFile.write((char*) &list[i], STRUCT_SIZE);
- myFile.close();
- }
- }
- class mainController {
- public:
- void mainLogic();
- private:
- binaryFileManager bfm;
- void checkIsExist();
- void enterFileNames();
- void generateFile(char* filename);
- void readFile(char* filename);
- char firstFile[256];
- char secondFile[256];
- };
- void mainController::checkIsExist() {
- if (!this->bfm.isExist(this->firstFile)) {
- cout << "File " << this->firstFile << " not exist. Generating.." << endl;
- this->generateFile(this->firstFile);
- }
- if (!this->bfm.isExist(this->secondFile)) {
- cout << "File " << this->secondFile << " not exist. Generating.." << endl;
- this->generateFile(this->secondFile);
- }
- }
- void mainController::enterFileNames() {
- cout << "Enter name of first file: ";
- cin >> this->firstFile;
- cout << "Enter name of second file: ";
- cin >> this->secondFile;
- }
- void mainController::mainLogic() {
- this->enterFileNames();
- this->checkIsExist();
- cout << "Content of file: " << this->firstFile << endl;
- this->readFile(this->firstFile);
- cout << "Content of file: " << this->secondFile << endl;
- this->readFile(this->secondFile);
- }
- void mainController::readFile(char* filename) {
- int count;
- Matrix* list = this->bfm.readSctruct(filename, &count);
- for (int i = 0; i < count; i++) {
- list[i].showMatrix();
- }
- }
- void mainController::generateFile(char* filename) {
- int count,m,n;
- cout << "Enter count of matrix: ";
- cin >> count;
- cout << "Enter n: ";
- cin >> n;
- cout << "Enter m: ";
- cin >> m;
- Matrix* list = new Matrix[count];
- for (int i =0;i<count;i++) {
- list[i].generateMatrix(n,m);
- }
- this->bfm.writeStructs(filename,list,count,0);
- }
- int main() {
- mainController mm;
- mm.mainLogic();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement