Advertisement
Kentoo

Barinov #1

Feb 13th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. #include <iomanip>
  7. #include "windows.h"
  8.  
  9. #define outputWidth 8
  10.  
  11. using namespace std;
  12.  
  13. void printArray(double* arr, unsigned int size) {
  14.     cout << setw(10) << "Номера: ";
  15.     for (unsigned int i = 0; i < size; i++) {
  16.         cout << setw(outputWidth) << i << " ";
  17.     }
  18.     cout << endl;
  19.  
  20.     cout << setw(10) << "Элементы: ";
  21.     for (unsigned int i = 0; i < size; i++) {
  22.         cout << setw(outputWidth) << arr[i] << " ";
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27. int main()
  28. {
  29.     system("cls");
  30.     SetConsoleCP(1251);
  31.     SetConsoleOutputCP(1251);
  32.  
  33.     string FNAME = "";
  34.     string s = "";
  35.     ifstream fin;
  36.     stringstream buf, res;
  37.  
  38.     double a = 0;
  39.     unsigned int n = 0;
  40.     bool broken = false;
  41.  
  42.     while (!fin.is_open()) {
  43.         cout << "Введите имя файла : ";
  44.         cin >> FNAME;
  45.         fin.open(FNAME);
  46.     }
  47.  
  48.     while (!fin.eof() && !broken) {
  49.         getline(fin, s);
  50.  
  51.         while (s.find("  ") != s.npos)
  52.             s.replace(s.find("  "), 2, " ");
  53.  
  54.         while (s.find(",") != s.npos)
  55.             s.replace(s.find(","), 1, ".");
  56.  
  57.         if (s.size() > 0) {
  58.             buf.str(string());
  59.             buf.clear();
  60.  
  61.             buf << s;
  62.  
  63.             while (buf >> s) {
  64.                 try {
  65.                     if (s.at(0) == '.')
  66.                         s = '0' + s;
  67.                     a = stod(s);
  68.                 }
  69.                 catch (exception e) {
  70.                     if (strcmp(e.what(), "invalid stod argument") == 0)
  71.                         cout << "Обнаружен недопустимый символ при вводе значения под номером " << n << endl
  72.                         << "Недопустимый ввод -> " << s << endl;
  73.                     if (strcmp(e.what(), "stod argument out of range") == 0)
  74.                         cout << "Обнаружено переполнение типа при вводе значения под номером " << n << endl;
  75.                     broken = true;
  76.                     break;
  77.                 }
  78.                 buf.ignore();
  79.                 if (!isdigit((unsigned char)s.at(s.size() - 1)) && s.at(s.size() - 1) != '.') {
  80.                     cout << "Обнаружен недопустимый символ при вводе значения под номером " << n << endl
  81.                         << "Недопустимый ввод -> " << s << endl;
  82.                     broken = true;
  83.                     break;
  84.                 }
  85.                 n++;
  86.                 res << a << ' ';
  87.             }
  88.         }
  89.     }
  90.     fin.close();
  91.     if (!broken) {
  92.         double* y = new(std::nothrow) double[n];
  93.  
  94.         if (y == nullptr) {
  95.             cout << "Ошибка выделения памяти" << endl;
  96.         }
  97.         else {
  98.             double* f = new(std::nothrow) double[n];
  99.  
  100.             if (f == nullptr) {
  101.                 cout << "Ошибка выделения памяти" << endl;
  102.             }
  103.             else {
  104.                 for (unsigned int i = 0; i < n; i++)
  105.                     res >> y[i];
  106.  
  107.                 for (unsigned int i = 0; i < n; i++) {
  108.                     f[i] = ((1 - y[i]) / (y[i] * y[i])) + ((y[i] <= 2) ? (2) : (3)) * y[i];
  109.                 }
  110.  
  111.                 cout << endl << "Массив y" << endl;
  112.                 printArray(y, n);
  113.  
  114.                 cout << endl << "Массив f" << endl;
  115.                 printArray(f, n);
  116.  
  117.                 cout << endl;
  118.  
  119.                 delete[] y;
  120.                 delete[] f;
  121.             }
  122.         }
  123.     }
  124.     system("pause");
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement