Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /
- #include <iostream>
- #include <random>
- #include <time.h>
- #include <vector>
- #include <fstream>
- #include <istream>
- using namespace std;
- typedef vector<vector<int>> IntMatrix;
- int HexToInt(string buf)
- {
- int value = 0;
- for (int i = 0; i < 8; i++)
- {
- if (buf[i] != '0')
- {
- if (int(buf[i]) <= (int)'9')
- {
- value += (int(buf[i]) - int('0')) * pow(16, 7 - i);
- }
- else
- {
- value += (int(buf[i]) - int('A') + 10) * pow(16, 7 - i);
- }
- }
- }
- return value;
- }
- IntMatrix CreateRandomMatrix(int sizeX, int sizeY, int intRange)
- {
- IntMatrix tempMatrix;
- for (int i = 0; i < sizeY; i++)
- {
- tempMatrix.push_back(vector<int>());
- for (int j = 0; j < sizeX; j++)
- {
- tempMatrix[i].push_back(rand()%intRange);
- }
- }
- return tempMatrix;
- }
- void PrintMatrix(IntMatrix &matrix)
- {
- for (int i = 0; i < matrix.size(); i++)
- {
- for (int j = 0; j < matrix[i].size(); j++)
- {
- cout << (int*)matrix[i][j] << " ";
- }
- cout << endl;
- }
- }
- void SaveMatrix(IntMatrix matrix, const char* fileName)
- {
- ofstream f;
- f.open(fileName);
- for (int i = 0; i < matrix.size(); i++)
- {
- for (int j = 0; j < matrix[i].size(); j++)
- {
- f << (int*)matrix[j][i];
- }
- if (i+1 != matrix.size())
- {
- f << "\n";
- }
- }
- f.close();
- }
- void LoadMatrix(IntMatrix& matrix, const char* fileName)
- {
- matrix = {};
- string line;
- string buf = "";
- ifstream f;
- f.open(fileName);
- if (f.is_open())
- {
- while (!f.eof())
- {
- f >> line;
- for (int i = 0; i < line.length(); i++)
- {
- buf.push_back(line[i]);
- if (i % 8 == 7)
- {
- matrix.push_back(vector<int>());
- matrix[i / int(8)].push_back(HexToInt(buf));
- buf = "";
- }
- }
- }
- f.close();
- }
- }
- int main() {
- srand(time(NULL));
- IntMatrix matrix;
- //SaveMatrix(matrix, "file.txt");
- LoadMatrix(matrix, "file.txt");
- PrintMatrix(matrix);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement