Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef APP_HEADER_HPP
- #define APP_HEADER_HPP
- #include <windows.h>
- #include <algorithm>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include "decimal.h"
- using namespace std;
- using namespace dec;
- //---------������������---------
- #pragma region enumerations
- // ��������� ������
- // [IER_0] � ��� ������, ��������� �������� ����������.
- // [IER_1] � ��������� �������� �� ���������� ( N ����).
- // [IER_2] � ��������� �������� �� �����������. ������ �������� �����
- // ����� ����������������� ����������������� ���������� ��������
- // �����������.
- // [IER_3] � � ������� X ������� ������� ����������� ����������.
- // [IER_4] � �������� ��������� XX �� ����������� ������� [X1, Xn].
- // [IER_5] � ��������� ����� ���������� �������. ���� ���������� �����
- // ����������.
- // [IER_6] � �������� ����� �����������
- // [IER_7] � P[i] < P[i-1]
- enum class IER {
- IER_0,
- IER_1,
- IER_2,
- IER_3,
- IER_4,
- IER_5,
- IER_6,
- IER_7,
- };
- #pragma endregion enumerations
- //---------������---------
- #pragma region classes
- class AppException : public exception {
- public:
- IER type;
- AppException(IER type, const string& what)
- : exception(what.c_str()), type(type){};
- };
- #pragma endregion classes
- //---------���������---------
- #pragma region constants
- using custom_double = decimal<3>;
- //-----------�������� ���������--------------------------------------------+
- // [X] � ������ �������� ���������� � ������� ����������� (������ �����
- // ������������).
- // [Y] � ������ �������� ������� � ����� ������������.
- // [N] - ���������� ����� ������������, � ������� ������ �������� �������.
- // [XX] - �������� ���������, ��� ������� ����� �����������
- // ���������������� �������� �������. [EPS] � �������� ������� �������
- // ���������� �����������.
- // [EPS] - ���������� ����� ������������, � ������� ������ �������� �������.
- //
- //-----------�������� ���������--------------------------------------------+
- // [YY] � ����������� ���������������� �������� ������� � ����� XX.
- // [IER] � ��������� ������.
- namespace ParametersFields {
- inline static const string X = "X";
- inline static const string Y = "Y";
- inline static const string N = "N";
- inline static const string XX = "XX";
- inline static const string EPS = "EPS";
- inline static const string YY = "YY";
- inline static const string IER = "IER";
- }; // namespace ParametersFields
- static const string kFileInputName = "parameters.txt";
- static const string kFileOutputName = "result.txt";
- static const string kFileParameterDoubleEmptyField = ": ,\n";
- static const string kFileParameterVectorEmptyField = ": [],\n";
- static const string kFileParametersTemplate =
- ParametersFields::N + kFileParameterDoubleEmptyField + ParametersFields::X +
- kFileParameterVectorEmptyField + ParametersFields::Y +
- kFileParameterVectorEmptyField + ParametersFields::XX +
- kFileParameterDoubleEmptyField + ParametersFields::EPS +
- kFileParameterDoubleEmptyField;
- #pragma endregion constants
- //---------������---------
- #pragma region methods
- void removeSpaces(string& text) {
- text.erase(
- std::remove_if(
- text.begin(), text.end(),
- [](char c) { return std::isspace(static_cast<unsigned char>(c)); }),
- text.end());
- }
- void parseVector(const string& text, vector<custom_double>& parameters) {
- string temp = text;
- if (temp.find(',') == 0)
- temp = temp.substr(1, temp.size() - 1);
- stringstream textStream(temp);
- for (double value; textStream >> value;) {
- parameters.push_back(custom_double(value));
- if (textStream.peek() == ',')
- textStream.ignore();
- }
- }
- vector<custom_double> parseParameterVectorField(const string& text,
- const string& fieldName,
- const custom_double& N) {
- vector<custom_double> parameters;
- string tmp = text;
- if (tmp.find(fieldName + ": [") == string::npos || !tmp.ends_with("],"))
- throw AppException(
- IER::IER_5, "������� ����� ���������� �������. ���� ����� ����������.");
- tmp = tmp.substr(tmp.find('[') + 1, tmp.size() - tmp.find('[') - 3);
- removeSpaces(tmp);
- parseVector(tmp, parameters);
- if (parameters.empty())
- throw AppException(IER::IER_6, "�������� [" + fieldName + "] ����������.");
- if (parameters.size() != N)
- throw AppException(
- IER::IER_6, "�������� [" + fieldName +
- "] �������� �����������. ���������� ���������� ������ "
- "��������� ��������� [N].");
- if (fieldName == ParametersFields::X) {
- bool isGrowing = true;
- for (size_t i = 1; i < parameters.size(); i++)
- if (parameters[i] < parameters[i - 1]) {
- isGrowing = false;
- break;
- }
- if (!isGrowing)
- throw AppException(IER::IER_3,
- "�������� [" + fieldName +
- "] ������ ��������� ��������, ��������������� � "
- "������� �����������.");
- }
- return parameters;
- }
- custom_double parseParameterDoubleField(const string& text,
- const string& fieldName,
- const vector<custom_double>& X) {
- custom_double parameter;
- string tmp = text;
- if (text.find(fieldName + ':') == string::npos || !text.ends_with(',')) {
- throw AppException(
- IER::IER_5, "������� ����� ���������� �������. ���� ����� ����������.");
- }
- tmp = tmp.substr(text.find(':') + 1, tmp.size() - text.find(':') - 2);
- removeSpaces(tmp);
- if (tmp.empty())
- throw AppException(IER::IER_6, "�������� [" + fieldName + "] ����������.");
- parameter = custom_double(stod(tmp));
- if (fieldName == ParametersFields::XX) {
- if (parameter < X[0] || parameter > X[X.size() - 1]) {
- throw AppException(
- IER::IER_4, "�������� ��������� XX ������ ������������ ������� X.");
- }
- }
- return parameter;
- }
- custom_double parseParameterLongIntField(const string& text,
- const string& fieldName) {
- custom_double parameter;
- string tmp = text;
- if (text.find(fieldName + ':') == string::npos || !text.ends_with(','))
- throw AppException(
- IER::IER_5, "������� ����� ���������� �������. ���� ����� ����������.");
- tmp = tmp.substr(text.find(':') + 1, tmp.size() - text.find(':') - 2);
- removeSpaces(tmp);
- if (tmp.empty())
- throw AppException(IER::IER_6, "�������� [" + fieldName + "] ����������.");
- parameter = custom_double(stoi(tmp));
- if (parameter <= 0) {
- throw AppException(IER::IER_6, "�������� [" + fieldName +
- "] ������ ���� � ���������� (0;+inf).");
- }
- return parameter;
- }
- #pragma endregion methods
- #endif // !APP_HEADER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement