Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- void PrintCondition()
- {
- std::cout << "Goal: Input Matrix And add Rows into position 0 And n+1 with elements equal To 1 (use shift Rows).\n";
- }
- void PrintMatrix(std::ostream& outStream, int** matrix, int rows, int columns, std::string header)
- {
- outStream << header;
- for (int row = 0; row < rows; row++)
- {
- for (int column = 0; column < columns; column++)
- {
- outStream << matrix[row][column];
- outStream << ' ';
- }
- outStream << '\n';
- }
- }
- int InputNumUnlimited()
- {
- int number;
- bool isCorrect = false;
- do
- {
- std::cin >> number;
- if (std::cin.fail())
- {
- std::cin.clear();
- std::cerr << "\nUnexpected symbol entered\n";
- while (std::cin.get() != '\n');
- }
- else if (std::cin.get() != '\n')
- {
- std::cout << "\nIncorrect value\n";
- while (std::cin.get() != '\n');
- }
- else
- {
- isCorrect = true;
- }
- } while (!isCorrect);
- return number;
- }
- int InputNumber(int min, int max)
- {
- int number;
- bool isCorrect = false;
- do
- {
- number = InputNumUnlimited();
- if (number < min)
- {
- std::cerr << "\nMinimal allowed value is " << min << "\n";
- }
- else if (number > max)
- {
- std::cerr << "\nMaximum allowed value is " << max << "\n";
- }
- else
- {
- isCorrect = true;
- }
- } while (!isCorrect);
- return number;
- }
- int** CreateMatrix(int rows, int columns, int defaultValue = 0)
- {
- int** matrix;
- matrix = new int* [rows];
- for (int row = 0; row < rows; row++)
- {
- matrix[row] = new int[columns];
- for (int column = 0; column < columns; column++)
- {
- matrix[row][column] = defaultValue;
- }
- }
- return matrix;
- }
- void CopyRow(int* sourceRow, int* destRow, int columns)
- {
- for (int column = 0; column < columns; column++)
- {
- destRow[column] = sourceRow[column];
- }
- }
- void CopyMatrix(int** sourceMatrix, int** destMatrix, int rows, int columns)
- {
- for (int row = 0; row < rows; row++)
- {
- CopyRow(sourceMatrix[row], destMatrix[row], columns);
- }
- }
- void DeleteMatrix(int** matrix, int rows)
- {
- for (int row = 0; row < rows; row++)
- {
- delete[] matrix[row];
- }
- delete[] matrix;
- }
- void ResizeMatrix(int**& matrix, int rows, int columns, int etraRows, int defaultValue)
- {
- int** outMatrix;
- int matrixRange;
- int outMatrixRows;
- outMatrixRows = rows + 2;
- outMatrix = CreateMatrix(outMatrixRows, columns, defaultValue);
- CopyMatrix(matrix, outMatrix, rows, columns);
- DeleteMatrix(matrix, rows);
- matrix = outMatrix;
- }
- char ReadCondition()
- {
- char condition;
- std::cout << "\nWould you like to enter matrix manually [m] or get from file [f]?\n";
- bool selected = false;
- do
- {
- std::cin >> condition;
- if (condition != 'm' && condition != 'f')
- {
- while (std::cin.get() != '\n');
- std::cout << "\nPlease type [m] or [f]\n";
- }
- else
- {
- if (std::cin.get() != '\n')
- {
- while (std::cin.get() != '\n');
- std::cout << "\nPlease type [m] or [f]\n";
- }
- else
- {
- selected = true;
- }
- }
- } while (!selected);
- return condition;
- }
- int InputMatrixRange()
- {
- int matrixRange;
- std::cout << "\nInput matrix range (1...5): ";
- matrixRange = InputNumber(1, 5);
- return matrixRange;
- }
- int** InputSourceMatrixManually(int& matrixRange)
- {
- int** matrix;
- constexpr int imin = std::numeric_limits<int>::min();
- constexpr int imax = std::numeric_limits<int>::max();
- matrixRange = InputMatrixRange();
- matrix = CreateMatrix(matrixRange, matrixRange);
- std::cout << "\nInput matrix values:\n";
- for (int row = 0; row < matrixRange; row++)
- {
- for (int column = 0; column < matrixRange; column++)
- {
- std::cout << "\nInput value for row: " << row << " column: " << column << "\n";
- matrix[row][column] = InputNumUnlimited();
- }
- }
- return matrix;
- }
- bool IsFileExist(std::string fileName)
- {
- std::ifstream in(fileName);
- return in.good();
- }
- bool HasTxtExtension(std::string fileName)
- {
- int length = fileName.length();
- if (length < 5)
- {
- return false;
- }
- if (fileName[length - 4] != '.')
- {
- return false;
- }
- if (fileName[length - 3] != 't')
- {
- return false;
- }
- if (fileName[length - 2] != 'x')
- {
- return false;
- }
- if (fileName[length - 1] != 't')
- {
- return false;
- }
- }
- std::string InputFilePath(bool checkExistence)
- {
- std::string fileName;
- bool isCorrect = false;
- do
- {
- std::cout << "\nInput file path (*.txt):\n";
- std::getline(std::cin, fileName);
- if (fileName.length() < 5)
- {
- std::cerr << '\n' << "invalid file name\n";
- }
- else if (!HasTxtExtension(fileName))
- {
- std::cerr << '\n' << "invalid file extension \n";
- }
- else if (checkExistence && !IsFileExist(fileName))
- {
- std::cerr << '\n' << fileName << " is not found \n";
- }
- else
- {
- isCorrect = true;
- }
- } while (!isCorrect);
- return fileName;
- }
- int GetMatrixRange(std::ifstream& inStream)
- {
- int matrixWidth = 0;
- try
- {
- inStream >> matrixWidth;
- if (inStream.fail())
- {
- return 0;
- }
- else
- {
- return matrixWidth;
- }
- }
- catch (...)
- {
- return 0;
- }
- }
- bool ReadRow(std::ifstream& inStream, int* row, int matrixWidth)
- {
- try
- {
- int divider;
- for (int index = 0; index < matrixWidth; index++)
- {
- inStream >> row[index];
- if (inStream.fail())
- {
- return false;
- }
- }
- if (inStream.eof())
- {
- return true;
- }
- divider = inStream.get();
- if (divider != '\n')
- {
- return false;
- }
- return true;
- }
- catch (const std::exception&)
- {
- return false;
- }
- }
- int** ReadMatrix(std::ifstream& in, int matrixRange)
- {
- int** matrix = nullptr;
- matrix = CreateMatrix(matrixRange, matrixRange);
- bool isValid = true;
- for (int rowIndex = 0; rowIndex < matrixRange && isValid == true; rowIndex++)
- {
- bool isValid = ReadRow(in, matrix[rowIndex], matrixRange);
- if (!isValid)
- {
- std::cerr << "Fail to read row:" << rowIndex + 1 << "\n";
- DeleteMatrix(matrix, matrixRange);
- matrix = nullptr;
- return matrix;
- }
- }
- return matrix;
- }
- int** GetSourceMatrixFromFile(int& matrixRange)
- {
- int** matrix;
- std::string filename;
- filename = InputFilePath(true);
- try
- {
- std::ifstream in(filename);
- if (in.is_open())
- {
- matrixRange = GetMatrixRange(in);
- if (matrixRange == 0)
- {
- std::cerr << "\nMatrix size error";
- in.close();
- return nullptr;
- }
- std::cout << "\nMatrix dimension: " << matrixRange << "x" << matrixRange << "\n";
- matrix = ReadMatrix(in, matrixRange);
- return matrix;
- in.close();
- }
- }
- catch (...)
- {
- std::cerr << "Matrix read error";
- return nullptr;
- }
- }
- int** GetSourceMatrix(int& matrixRange)
- {
- char condition = ReadCondition();
- if (condition == 'm')
- {
- return InputSourceMatrixManually(matrixRange);
- }
- else
- {
- return GetSourceMatrixFromFile(matrixRange);
- }
- }
- void ShiftMatrix(int** matrix, int rows, int columns)
- {
- int* tempRow = new int[columns];
- CopyRow(matrix[rows - 1], tempRow, columns);
- for (int row = rows - 1; row > 0; row--)
- {
- CopyRow(matrix[row - 1], matrix[row], columns);
- }
- CopyRow(tempRow, matrix[0], columns);
- delete[] tempRow;
- }
- bool SaveMatrix(std::string filename, int** matrix, int rows, int columns)
- {
- std::ofstream outf(filename);
- if (!outf)
- {
- std::cerr << filename << " could not be opened for writing!\n";
- return false;
- }
- PrintMatrix(outf, matrix, rows, columns, "");
- outf.close();
- return true;
- }
- void InputFilePathAndSaveMatrix(int** matrix, int rows, int columns)
- {
- std::string path;
- bool isCorrect = false;
- do
- {
- path = InputFilePath(false);
- isCorrect = SaveMatrix(path, matrix, rows, columns);
- } while (!isCorrect);
- std::cout << "\nCompleted!\n";
- }
- int main()
- {
- int** inMatrix;
- int matrixRange;
- int extraRows;
- int defaultValue;
- defaultValue = 1;
- extraRows = 2;
- std::string outFilename;
- PrintCondition();
- inMatrix = GetSourceMatrix(matrixRange);
- if (inMatrix == nullptr)
- {
- return 0;
- }
- PrintMatrix(std::cout, inMatrix, matrixRange, matrixRange, "\nSource Matrix:\n");
- ResizeMatrix(inMatrix, matrixRange, matrixRange, extraRows, defaultValue);
- ShiftMatrix(inMatrix, matrixRange + extraRows, matrixRange);
- PrintMatrix(std::cout, inMatrix, matrixRange + extraRows, matrixRange, "\nShifted Matrix:\n");
- InputFilePathAndSaveMatrix(inMatrix, matrixRange + extraRows, matrixRange);
- DeleteMatrix(inMatrix, matrixRange + extraRows);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement