Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <conio.h>
- using namespace std;
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int n, counter = 0;
- ios::pos_type i;
- string s, file;
- bool flag = false;
- //istringstream ss;
- ifstream F;
- do
- {
- cout << "введите имя файла: ";
- getline(cin, file, '\n');
- F.open(file);
- if (F.is_open() != true)
- cout << "файла не существует!\n" << "введите заново имя файла" << endl;
- } while (F.is_open() != true);
- /*while (F >> s)
- {
- counter++;
- ss.str(s);
- ss >> n;
- if (!ss.fail() && ss.eof())
- ss.clear();
- else
- {
- F.close();
- cout << "ошибка в элементе \"" << s << "\" под номером " << counter;
- _getch();
- return 1;
- }
- }*/
- while (!(F >> ws).eof())
- {
- char pr;
- i = F.tellg();
- F >> n;
- counter++;
- if (F.fail()) //-2147483648 до 2147483647
- {
- F.clear();
- flag = true;
- break;
- }
- if ((pr = F.peek()) != ' ' && pr != '\n' && pr != '\t')
- {
- flag = true;
- break;
- }
- }
- if (flag)
- {
- F.seekg(i);
- F >> s;
- F.close();
- cout << "ошибка в элементе \"" << s << "\" под номером " << counter;
- _getch();
- return 1;
- }
- if (counter == 0)
- {
- F.close();
- cout << "файл пустой!";
- _getch();
- return 1;
- }
- F.clear();
- F.seekg(0);
- cout << "Количество элементов: " << counter << endl;
- int max, *ap;
- try
- {
- ap = new int[counter];
- }
- catch (...)
- {
- F.close();
- cout << "память не смогла выделиться!" << endl;
- _getch();
- return 1;
- }
- /*ap = new(nothrow) int[counter];
- if (!ap)
- {
- F.close();
- cout << "память не смогла выделиться!" << endl;
- _getch();
- return 1;
- }*/
- cout << endl << "Ваш массив: " << endl;
- cout << setw(10) << "Номера: ";
- for (int l = 1; l <= counter; l++)
- cout << setw(5) << l;
- cout << endl << setw(10) << "Элементы: ";
- for (int l = 0; l < counter; l++)
- {
- F >> *(ap + l);
- cout << setw(5) << ap[l];
- }
- n = 0;
- max = ap[0];
- for (int l = 1; l < counter; l++)
- {
- if (ap[l] > max)
- {
- n = l;
- max = ap[l];
- }
- }
- cout << endl << endl << "Максимальный элемент \"" << max << "\" под номером " << n + 1 << endl;
- for (int l = 0; l < counter; l++)
- {
- if (ap[l] < 0)
- ap[l] = max;
- }
- cout << endl << "Новый массив: " << endl;
- cout << setw(10) << "Номера: ";
- for (int l = 1; l <= counter; l++)
- cout << setw(5) << l;
- cout << endl << setw(10) << "Элементы: ";
- for (int l = 0; l < counter; l++)
- {
- cout << setw(5) << ap[l];
- }
- cout << endl;
- delete[] ap;
- cout << endl << "нажмите любую клавишу для завершения программы" << endl;
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement