Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <sstream>
- using namespace std;
- int main()
- {
- setlocale(LC_ALL, "ru");
- int i;
- streampos p;
- int b;
- string er;
- i = 0;
-
- ifstream file;
- int m; // статичная переменная
- int* C; //динамичная переменая
- do
- {
- m = 0;
- string name;
- cout << "Введите имя файла с массивом или \"*\", чтобы выйти из программы: ";
- getline(cin, name); //ввод имени файла
- if (name == "*")
- return 0;
- //открытие файла
- file.open(name);
- if (!file) {
- cout << endl << "Не удалось открыть файл " << "\"" << name << "\"!" << endl; // проверка на открытие файла
- continue;
- }
- while (!(file >> ws).eof()) {
- p = file.tellg();
- file >> b;
- char k;
- if (file.fail() || ((k = file.peek()) != ' ' && k != '\t' && k != '\n' && k != EOF)) {
- file.clear();
- file.seekg(p);
- file >> er;
- return false;
- }
- m++;
- }
- // проверка на наличие данных в файле
- if (m == 0) {
- file.close();
- cout << "Файл пуст!" << endl;
- continue;
- }
- C = new(nothrow) int[m];
- // вывод массива на экран
- cout << setw(10) << "Массив С" << endl;
- cout << setw(10) << "Номер";
- for (int i = 1; i <= m; i++)
- cout << setw(4) << i;
- cout << endl << setw(10) << "Элемент";
- file.clear();
- file.seekg(0);
- for (int i = 0; i < m; i++) {
- file >> C[i];
- cout << setw(4) << C[i];
- }
- file.close();
- cout << endl;
- int pos1 = 0;
- do {
- for (int i = 0; i < m; i++)
- if (C[i] == 0) {
- pos1 = i; i++;
- break;
- }
- } while (pos1 == 0);
- int pos2 = 0;
- do {
- for (pos1; pos1 < m; pos1++)
- if (C[pos1 + 1] == 0) {
- pos2 = pos1; pos1++;
- break;
- }
- } while (pos2 == 0);
- cout << "Преобразованный массив C: " << endl;
- for (i; i < pos1; i++) {
- cout << setw(4) << C[i];
- }
- for (i; (i > pos1)& (i < pos2); i++) {
- C[i] = 1;
- cout << setw(4) << C[i];
- break;
- }
- for (i; (i > pos2)& (i < m); i++) {
- cout << setw(4) << C[i];
- break;
- }
- cout << endl;
- } while (true);
- }
Advertisement
Add Comment
Please, Sign In to add comment