Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int choiceCheck() {
- int choice;
- bool isIncorrect;
- do {
- isIncorrect = true;
- cin >> choice;
- if (cin.fail() || (cin.get() != '\n')) {
- cout << "Error! Input a number" << endl;
- cin.clear();
- while (cin.get() != '\n');
- } else {
- cin.clear();
- if (choice > 0 && choice < 3) {
- isIncorrect = false;
- } else {
- cout << "Error! Input 1 or 2" << endl;
- }
- }
- } while (isIncorrect);
- return choice;
- }
- int inputCheck() {
- bool isIncorrect;
- int n;
- cout << "Input n" << endl;
- do {
- isIncorrect = true;
- cin >> n;
- if (cin.fail() || (cin.get() != '\n')) {
- cout << "Error! Input a number" << endl;
- cin.clear();
- while (cin.get() != '\n');
- } else {
- cin.clear();
- if (n > 0) {
- isIncorrect = false;
- } else {
- cout << "Error! Input a number greater than 0" << endl;
- }
- }
- } while (isIncorrect);
- return n;
- }
- int **inputCheckMatrix(int n, int **matr) {
- int n1, i, j;
- bool isIncorrect;
- n1 = n + 1;
- for (j = 0; j < n; j++) {
- matr[0][j] = 0;
- }
- for (j = 0; j < n; j++) {
- matr[n1][j] = 0;
- }
- for (i = 1; i < n1; i++) {
- for (j = 0; j < n; j++) {
- do {
- isIncorrect = true;
- cin >> matr[i][j];
- if (cin.fail() || (cin.get() != '\n')) {
- cout << "Error! Input a number" << endl;
- cin.clear();
- while (cin.get() != '\n');
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- }
- }
- return matr;
- }
- string checkInputFilePath() {
- string path;
- bool isInCorrect;
- do {
- cout << "Input path to the file:\n";
- isInCorrect = false;
- cin >> path;
- ifstream fin(path);
- if (!fin.is_open()) {
- cout << "Could not open the file" << endl;
- isInCorrect = true;
- } else {
- fin.close();
- }
- } while (isInCorrect);
- return path;
- }
- int **fileCheckMatrix(int &n, int **matr) {
- string path;
- bool isIncorrect;
- int i, j, n1, n2;
- do {
- path = checkInputFilePath();
- ifstream fin(path);
- isIncorrect = false;
- fin >> n;
- if (fin.fail()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect && (n < 1)) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- if (!isIncorrect) {
- n1 = n + 1;
- n2 = n + 2;
- matr = new int *[n + 2];
- for (i = 0; i < n2; i++) {
- matr[i] = new int[n];
- }
- for (j = 0; j < n; j++) {
- matr[0][j] = 0;
- }
- for (j = 0; j < n; j++) {
- matr[n1][j] = 0;
- }
- for (i = 1; i < n1; i++) {
- for (j = 0; j < n; j++) {
- fin >> matr[i][j];
- if (fin.fail()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- }
- }
- if (!fin.eof()) {
- cout << "The data is incorrect" << endl;
- isIncorrect = true;
- }
- }
- fin.close();
- } while (isIncorrect);
- return matr;
- }
- int **inputChoice(int &n) {
- int choice, n2, i;
- int **matr;
- cout << "Choose input option:\n1.Input through console\n2.Input through file" << endl;
- choice = choiceCheck();
- if (choice == 1) {
- n = inputCheck();
- n2 = n + 2;
- matr = new int *[n2];
- for (i = 0; i < n2; i++) {
- matr[i] = new int[n];
- }
- matr = inputCheckMatrix(n, matr);
- } else {
- matr = fileCheckMatrix(n, matr);
- }
- return matr;
- }
- void swap(int &n, int **matr) {
- int n1, i, j;
- int helpArray1[n];
- int helpArray2[n];
- n1 = n + 2;
- for (j = 0; j < n; j++) {
- helpArray1[j] = matr[1][j];
- matr[1][j] = 0;
- }
- for (i = 2; i < n1; i++) {
- for (j = 0; j < n; j++) {
- helpArray2[j] = matr[i][j];
- matr[i][j] = helpArray1[j];
- helpArray1[j] = helpArray2[j];
- }
- }
- }
- string checkOutputFilePath() {
- string path;
- bool isIncorrect;
- cout
- << "Input file path and the name of the file for\nexample С:\\Projects\\Number\\FileName.txt. If the\nfile does not exist, then it will be created\nautomatically in the root folder of the program:\n";
- isIncorrect = false;
- cin >> path;
- ifstream fin(path);
- if (!fin.is_open()) {
- cout << "Could not open the file or it does not exist" << endl;
- isIncorrect = true;
- } else {
- fin.close();
- }
- if (isIncorrect) {
- cout << "File will be created in the root folder of the program\n";
- path = "Result.txt";
- }
- return path;
- }
- void outputMatrix(int n, int **matr) {
- int i, j, n1;
- n1 = n + 2;
- for (i = 0; i < n1; i++) {
- for (j = 0; j < n; j++) {
- cout << matr[i][j] << " ";
- }
- cout << endl;
- }
- }
- void outputFile(int n, int **matr) {
- string path;
- int i, j, n1;
- path = checkOutputFilePath();
- ofstream fout(path);
- n1 = n + 2;
- for (i = 0; i < n1; i++) {
- for (j = 0; j < n; j++) {
- fout << matr[i][j] << " ";
- }
- fout << endl;
- }
- fout.close();
- }
- void outputChoice(int &n, int **matr) {
- int choice;
- cout << "Choose output option:\n1.Output through console\n2.Output through file" << endl;
- choice = choiceCheck();
- if (choice == 1) {
- outputMatrix(n, matr);
- } else {
- outputFile(n, matr);
- }
- }
- int main() {
- int **matr;
- int n;
- matr = inputChoice(n);
- swap(n, matr);
- outputChoice(n, matr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement