Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- #include <iostream>
- using namespace std;
- void chooseInput(int& inputType)
- {
- bool incorrect;
- string line;
- do
- {
- incorrect = false;
- cout << "Do you want to input from file? (y/n)" << endl;
- cin >> line;
- if (line != "Y" && line != "y" && line != "N" && line != "n")
- {
- incorrect = true;
- cout << "Enter valid answer" << endl;
- }
- } while (incorrect);
- if (line == "N" || line == "n")
- inputType = 1;
- else
- inputType = 0;
- }
- string inputFileLocation()
- {
- bool incorrect = false;
- string location = "";
- cout << "Enter file location:" << endl;
- cin >> location;
- ifstream file(location);
- if (file.is_open())
- {
- cout << "File opened successfully\n";
- return location;
- }
- else
- {
- cout << "File with this location is not found" << endl;
- }
- file.close();
- }
- void getArrayFromFile(int** arrays, int& size)
- {
- bool incorrect = false;
- int j;
- string line;
- ifstream file(inputFileLocation());
- file.clear();
- file.seekg(0, ios::beg);
- for (int i = 0; i < size; i++)
- {
- for (j = 0; j < size; j++)
- {
- file >> arrays[i][j];
- }
- }
- getline(file, line, '\n');
- file.close();
- }
- void outputToFile(int** arrays, int& size)
- {
- ofstream file(inputFileLocation());
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- file << arrays[i][j] << endl;
- }
- cout << endl;
- }
- }
- void getArrayFromConsole(int** array, int& size)
- {
- int j;
- cout << "Enter the matrix\n";
- for (int i = 0; i < size; i++)
- {
- for (j = 0; j < size; j++)
- {
- cout << "\nenter the element number [" << i + 1 << "|" << j + 1 << "]: ";
- cin >> array[i][j];
- }
- }
- }
- int dimensionInput()
- {
- const int MAX_VALUE = 5;
- const int MIN_VALUE = 1;
- int size = 0;
- bool isIncorrect;
- do
- {
- cin >> size;
- isIncorrect = false;
- if (size < MIN_VALUE || size > MAX_VALUE)
- {
- isIncorrect = true;
- cout << "Please enter a natural value less than six\n";
- }
- } while (isIncorrect);
- return size;
- }
- void deleteZeroStrings(int** arrays, int& size, int previousSize)
- {
- int i = 0;
- int j;
- int prexSize = size;
- bool zeroFounded;
- while (i < size) {
- zeroFounded = false;
- j = 0;
- while ((j < previousSize & (!zeroFounded))) {
- if (arrays[i][j] == 0) {
- zeroFounded = true;
- for (int p = i; p < size - 1; p++) {
- arrays[p] = arrays[p + 1];
- }
- size--;
- i--;
- }
- j++;
- }
- i++;
- }
- }
- void print(int** arrays, int& size , int previousSize)
- {
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < previousSize; j++)
- cout << arrays[i][j] << " ";
- cout << endl;
- }
- }
- int main()
- {
- setlocale(LC_ALL, "RUSSIAN");
- cout << "Данная программа удаляет строки массива , содержащие нулевые элементы.\n";
- int chosenInput;
- chooseInput(chosenInput);
- cout << "Enter the matrix size\n";
- int size = dimensionInput();
- int** matrix = new int* [size];
- for (int i = 0; i < size; i++)
- matrix[i] = new int[size];
- if (chosenInput == 0)
- {
- getArrayFromFile(matrix, size);
- }
- else
- {
- getArrayFromConsole(matrix, size);
- }
- cout << endl;
- cout << "Original matrix" << endl;
- int previousSize = size;
- print(matrix, size, previousSize);
- double* variablesArray = new double[size];
- deleteZeroStrings(matrix, size, previousSize);
- cout << endl << "Modified matrix" << endl;
- print(matrix, size, previousSize);
- outputToFile(matrix, size);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement