Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <locale>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- int checkConsole()
- {
- int tmp;
- cin >> tmp;
- while (!cin)
- {
- cout << "Введите число, а не текст!\n : ";
- cin >> tmp;
- }
- return tmp;
- }
- void inputConsoleMatrix(int **array, int size)
- {
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- array[i][j] = checkConsole();
- }
- bool check(char tmp[255])
- {
- int i;
- tmp[0] == '-' ? i = 1 : i = 0;
- for (; tmp[i] != '\0'; i++)
- if (!isdigit(tmp[i]))
- return false;
- return true;
- }
- bool inputFileMatrix(int **array, int size, int &i, int &j, ifstream &fin)
- {
- char tmp[255];
- for (i = 0; i < size; i++)
- for (j = 0; j < size;)
- {
- fin >> tmp;
- if (check(tmp))
- {
- array[i][j] = atoi(tmp);
- j++;
- }
- if (fin.eof() && j < size) // т.е. еще не все элементы матрицы были заполнены, но в потоке уже нет чисел
- return false;
- }
- return true;
- }
- void inputFalseMatrix(int **array, int size, int ii, int jj)
- {
- cout << "В потоке было недостаточно элементов для матрицы. Введите недостающие элементы:" << endl;
- int j = jj;
- for (int i = ii; i < size; i++)
- {
- for (; j < size; j++)
- {
- cout << "a[" << i + 1 << "][" << j + 1 << "] = ";
- array[i][j] = checkConsole();
- }
- j = 0;
- }
- }
- void outOfFile(ifstream &fin, ofstream &fout)
- {
- cout << "Вы хотите вывести на экран данные из выходного файла?\n1)Да\n2)Нет" << endl;
- int temp;
- cin >> temp;
- while (temp != 1 && temp != 2)
- {
- cout << "Введите один из вариантов.";
- cin >> temp;
- }
- if (temp == 1)
- {
- fin.close();
- fout.close();
- fin.open("output.txt");
- char a[255];
- while (!fin.eof())
- {
- fin.getline(a, 255);
- cout << a << endl;
- }
- }
- }
- void outarray(int **array, int size)
- {
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- cout << setw(4) << array[i][j] << " ";
- cout << endl;
- }
- cout << endl;
- }
- void outarray(int **array, int size, ofstream &fout)
- {
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- fout << setw(4) << array[i][j] << " ";
- fout << endl;
- }
- fout << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "russian");
- int size, ii = 0, jj = 0;
- ifstream fin;
- fin.open("input.txt");
- ofstream fout;
- fout.open("output.txt");
- if (!fin.is_open())
- {
- cout << "Файл не удалось открыть." << endl;
- return 1;
- }
- cout << "Введите размер матрицы: ";
- cin >> size;
- int **array = new int*[size];
- for (int i = 0; i < size; i++)
- array[i] = new int[size];
- if (!inputFileMatrix(array, size, ii, jj, fin))
- inputFalseMatrix(array, size, ii, jj);
- fout << "Вы ввели матрицу:" << endl;
- outarray(array, size, fout);
- outOfFile(fin, fout);
- for (int i = 0; i < size; i++)
- delete[]array[i];
- delete[]array;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement