Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream fin;
- ofstream fout;
- int choose() {
- int chose;
- bool isIncorrect;
- do{
- cin >> chose;
- isIncorrect = false;
- if (cin.fail() || !(chose == 1 || chose == 0)){
- isIncorrect = true;
- cout << "Please, choose." << endl << "1 is console input/output, 0 is file input/output." << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return chose;
- }
- // консольный ввод и вывод
- int inputData() {
- int n = 0;
- bool isIncorrect;;
- do {
- cin >> n;
- isIncorrect = false;
- if (cin.fail()) {
- cout << "Please, enter a positive integer number:" << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return n;
- }
- int inputPositiveNumber() {
- int npos;
- const int MIN = 1;
- bool isIncorrect;
- do {
- npos = inputData();
- isIncorrect = false;
- if (npos < MIN) {
- cout << "Please, enter a positive number." << endl;;
- isIncorrect = true;
- }
- } while (isIncorrect);
- return npos;
- }
- int inputArraySize() {
- bool isIncorrect;
- int num = 0;
- do {
- num = inputData();
- isIncorrect = false;
- if (num % 2 != 0) {
- cout << "Please, enter an even number:" << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return num;
- }
- int** inputArray(int num) {
- int i = 0;
- int j = 0;
- int** arr = new int* [num];
- for (i = 0; i < num; i++)
- arr[i] = new int[num];
- for (i = 0; i < num; i++)
- for (j = 0; j < num; j++)
- arr[i][j] = inputData();
- return arr;
- }
- int* transferMatrixtoVector(int n, int** arr, int starti, int startj) {
- const int MIN = 4;
- int* quarter = new int[(n * n) / MIN];
- int k = 0;
- int i = 0;
- int j = 0;
- int halfn = n / 2;
- for (i = starti; i < starti + halfn; i++) {
- for (j = startj; j < startj + halfn; j++) {
- quarter[k] = arr[i][j];
- k++;
- }
- }
- return quarter;
- }
- void outputArray(int num, int** arr) {
- int i = 0;
- int j = 0;
- for (i = 0; i < num; i++) {
- for (j = 0; j < num; j++)
- cout << arr[i][j] << ' ';
- cout << '\n';
- }
- }
- //functions for files:
- string inputFilePath() {
- const int EXTENSION_SIZE = 4;
- string path;
- string ext;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Input path to file: " << endl;
- cin >> path;
- fin.open(path);
- if (path.size() > EXTENSION_SIZE) {
- ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
- }
- else {
- cout << "Incorrect file name." << endl;
- isIncorrect = true;
- } if (!isIncorrect && ext != ".txt") {
- cout << "Must have .txt!" << endl;
- isIncorrect = true;
- }
- else if (!isIncorrect && !fin.is_open()) {
- cout << "Wrong way to file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- fin.close();
- return path;
- }
- int inputSizeOfMatrixFromFile(string path) {
- int n;
- bool isIncorrect;
- const int MIN = 1;
- const int MAX = 10;
- do
- {
- isIncorrect = false;
- fin.open(path);
- fin >> n;
- if (n < MIN || n > MAX) {
- isIncorrect = true;
- cout << "Incorrect matrix size. Your matrix size must be from " << MIN << " to " << MAX << "." << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- return n;
- }
- int** inputMatrixFile(string path, int num)
- {
- ifstream fileIn(path);
- bool isIncorrect = false;
- int** arr;
- arr = new int* [num];
- for (int i = 0; i < num; i++)
- arr[i] = new int[num];
- for (int i = 0; (i < num) && (!isIncorrect); i++) {
- for (int j = 0; (j < num) && (!isIncorrect); j++) {
- do
- {
- isIncorrect = false;
- try {
- fin >> arr[i][j];
- }
- catch (fstream::failure& e) {
- cout << "Mistake of reading from file. Code of mistake " << e.code() << "\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- }
- return arr;
- }
- void fileOutput(int n, int** arr) {
- cout << "Input second file path for writing data there:" << endl;
- string path = inputFilePath();
- fout.open(path);
- int prod;
- for (int i = 0; i < n; i++)
- {
- prod = 1;
- for (int j = 0; j < n; j++)
- {
- prod *= arr[i][j];
- }
- if (prod != 0)
- {
- for (int j = 0; j < n; j++)
- {
- fout << arr[i][j] << ' ';
- }
- fout << '\n';
- }
- }
- cout << "Successful output in file." << endl;
- fout.close();
- }
- int main() {
- int n = 0;
- int** arr;
- cout << "Enter 1 or 0. " << endl << "1 is console input, 0 is file input." << endl;
- int chose = choose();
- if (chose == 1) {
- cout << "Input number of digits : " << endl;
- n = inputArraySize();
- cout << "Enter the numbers one by one: " << endl;
- arr = inputArray(n);
- }
- else {
- string str = inputFilePath();
- n = inputSizeOfMatrixFromFile(str);
- arr = inputMatrixFile(str, n);
- }
- int starti = 0;
- int startj = starti;
- int* bufferFor1 = transferMatrixtoVector(n, arr, starti, startj);
- starti = 0;
- startj = n / 2;;
- int* bufferFor2 = transferMatrixtoVector(n, arr, starti, startj);
- starti = n / 2;
- startj = 0;
- int* bufferFor3 = transferMatrixtoVector(n, arr, starti, startj);
- starti = n / 2;
- startj = starti;
- int* bufferFor4 = transferMatrixtoVector(n, arr, starti, startj);
- int i = 0;
- int j = 0;
- int k = 0;
- int l = 0;
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++) {
- if ((i < n / 2) && (j < n / 2)) {
- arr[i][j] = bufferFor4[k];
- }
- k = 0;
- if ((i < n / 2) && (j > n / 2 - 1)) {
- arr[i][j] = bufferFor3[k];
- }
- k = 0;
- if ((i > n / 2 - 1) && (j < n / 2)) {
- arr[i][j] = bufferFor1[k];
- }
- k = 0;
- if ((i > n / 2 - 1) && (j > n / 2 - 1)) {
- arr[i][j] = bufferFor2[k];
- k++;
- }
- }
- }
- cout << "Enter 1 or 0. " << endl << "1 is console output, 0 is file output." << endl;
- chose = choose();
- if (chose == 1) {
- outputArray(n, arr);
- }
- else {
- fileOutput(n, arr);
- }
- for (i = 0; i < n; i++)
- delete[] arr[i];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement