Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <iomanip>
- #include "windows.h"
- #define outputWidth 8
- using namespace std;
- void printArray(double* arr, unsigned int size) {
- cout << setw(10) << "Номера: ";
- for (unsigned int i = 0; i < size; i++) {
- cout << setw(outputWidth) << i << " ";
- }
- cout << endl;
- cout << setw(10) << "Элементы: ";
- for (unsigned int i = 0; i < size; i++) {
- cout << setw(outputWidth) << arr[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- system("cls");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string FNAME = "";
- string s = "";
- ifstream fin;
- stringstream buf, res;
- double a = 0;
- unsigned int n = 0;
- bool broken = false;
- while (!fin.is_open()) {
- cout << "Введите имя файла : ";
- cin >> FNAME;
- fin.open(FNAME);
- }
- while (!fin.eof() && !broken) {
- getline(fin, s);
- while (s.find(" ") != s.npos)
- s.replace(s.find(" "), 2, " ");
- while (s.find(",") != s.npos)
- s.replace(s.find(","), 1, ".");
- if (s.size() > 0) {
- buf.str(string());
- buf.clear();
- buf << s;
- while (buf >> s) {
- try {
- if (s.at(0) == '.')
- s = '0' + s;
- a = stod(s);
- }
- catch (exception e) {
- if (strcmp(e.what(), "invalid stod argument") == 0)
- cout << "Обнаружен недопустимый символ при вводе значения под номером " << n << endl
- << "Недопустимый ввод -> " << s << endl;
- if (strcmp(e.what(), "stod argument out of range") == 0)
- cout << "Обнаружено переполнение типа при вводе значения под номером " << n << endl;
- broken = true;
- break;
- }
- buf.ignore();
- if (!isdigit((unsigned char)s.at(s.size() - 1)) && s.at(s.size() - 1) != '.') {
- cout << "Обнаружен недопустимый символ при вводе значения под номером " << n << endl
- << "Недопустимый ввод -> " << s << endl;
- broken = true;
- break;
- }
- n++;
- res << a << ' ';
- }
- }
- }
- fin.close();
- if (!broken) {
- double* y = new(std::nothrow) double[n];
- if (y == nullptr) {
- cout << "Ошибка выделения памяти" << endl;
- }
- else {
- double* f = new(std::nothrow) double[n];
- if (f == nullptr) {
- cout << "Ошибка выделения памяти" << endl;
- }
- else {
- for (unsigned int i = 0; i < n; i++)
- res >> y[i];
- for (unsigned int i = 0; i < n; i++) {
- f[i] = ((1 - y[i]) / (y[i] * y[i])) + ((y[i] <= 2) ? (2) : (3)) * y[i];
- }
- cout << endl << "Массив y" << endl;
- printArray(y, n);
- cout << endl << "Массив f" << endl;
- printArray(f, n);
- cout << endl;
- delete[] y;
- delete[] f;
- }
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement