Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <conio.h>
  8. using namespace std;
  9.  
  10.  
  11. int main()
  12. {
  13.     setlocale(LC_ALL, "Russian");
  14.     int n, counter = 0;
  15.     ios::pos_type i;
  16.     string s, file;
  17.     bool flag = false;
  18.     //istringstream ss;
  19.     ifstream F;
  20.     do
  21.     {
  22.         cout << "введите имя файла: ";
  23.         getline(cin, file, '\n');
  24.         F.open(file);
  25.         if (F.is_open() != true)
  26.             cout << "файла не существует!\n" << "введите заново имя файла" << endl;
  27.     } while (F.is_open() != true);
  28.     /*while (F >> s)
  29.     {
  30.         counter++;
  31.         ss.str(s);
  32.         ss >> n;
  33.         if (!ss.fail() && ss.eof())
  34.             ss.clear();
  35.         else
  36.         {
  37.             F.close();
  38.             cout << "ошибка в элементе \"" << s << "\" под номером " << counter;
  39.             _getch();
  40.             return 1;
  41.         }
  42.     }*/
  43.     while (!(F >> ws).eof())
  44.     {
  45.         char pr;
  46.         i = F.tellg();
  47.         F >> n;
  48.         counter++;
  49.         if (F.fail())    //-2147483648 до 2147483647
  50.         {
  51.             F.clear();
  52.             flag = true;
  53.             break;
  54.         }
  55.         if ((pr = F.peek()) != ' ' && pr != '\n' && pr != '\t')
  56.         {
  57.             flag = true;
  58.             break;
  59.         }
  60.     }
  61.     if (flag)
  62.     {
  63.         F.seekg(i);
  64.         F >> s;
  65.         F.close();
  66.         cout << "ошибка в элементе \"" << s << "\" под номером " << counter;
  67.         _getch();
  68.         return 1;
  69.     }
  70.     if (counter == 0)
  71.     {
  72.         F.close();
  73.         cout << "файл пустой!";
  74.         _getch();
  75.         return 1;
  76.     }
  77.     F.clear();
  78.     F.seekg(0);
  79.     cout << "Количество элементов: " << counter << endl;
  80.     int max, *ap;
  81.     try
  82.     {
  83.         ap = new int[counter];
  84.     }
  85.     catch (...)
  86.     {
  87.         F.close();
  88.         cout << "память не смогла выделиться!" << endl;
  89.         _getch();
  90.         return 1;
  91.     }
  92.     /*ap = new(nothrow) int[counter];
  93.     if (!ap)
  94.     {
  95.         F.close();
  96.         cout << "память не смогла выделиться!" << endl;
  97.         _getch();
  98.         return 1;
  99.     }*/
  100.     cout << endl << "Ваш массив: " << endl;
  101.     cout << setw(10) << "Номера: ";
  102.     for (int l = 1; l <= counter; l++)
  103.         cout << setw(5) << l;
  104.     cout << endl << setw(10) << "Элементы: ";
  105.     for (int l = 0; l < counter; l++)
  106.     {
  107.         F >> *(ap + l);
  108.         cout << setw(5) << ap[l];
  109.     }
  110.     n = 0;
  111.     max = ap[0];
  112.     for (int l = 1; l < counter; l++)
  113.     {
  114.         if (ap[l] > max)
  115.         {
  116.             n = l;
  117.             max = ap[l];
  118.         }
  119.     }
  120.     cout << endl << endl << "Максимальный элемент \"" << max << "\" под номером " << n + 1 << endl;
  121.     for (int l = 0; l < counter; l++)
  122.     {
  123.         if (ap[l] < 0)
  124.             ap[l] = max;
  125.     }
  126.     cout << endl << "Новый массив: " << endl;
  127.     cout << setw(10) << "Номера: ";
  128.     for (int l = 1; l <= counter; l++)
  129.         cout << setw(5) << l;
  130.     cout << endl << setw(10) << "Элементы: ";
  131.     for (int l = 0; l < counter; l++)
  132.     {
  133.         cout << setw(5) << ap[l];
  134.     }
  135.     cout << endl;
  136.     delete[] ap;
  137.     cout << endl << "нажмите любую клавишу для завершения программы" << endl;
  138.     _getch();
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement