Advertisement
Sanlover

Untitled

Sep 24th, 2021
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.25 KB | None | 0 0
  1. #ifndef APP_HEADER_HPP
  2. #define APP_HEADER_HPP
  3.  
  4. #include <windows.h>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11. #include "decimal.h"
  12. using namespace std;
  13. using namespace dec;
  14.  
  15. //---------������������---------
  16. #pragma region enumerations
  17. // ��������� ������
  18. // [IER_0] � ��� ������, ��������� �������� ����������.
  19. // [IER_1] � ��������� �������� �� ���������� ( N ����).
  20. // [IER_2] � ��������� �������� �� �����������. ������ �������� �����
  21. //           ����� ����������������� ����������������� ���������� ��������
  22. //           �����������.
  23. // [IER_3] � � ������� X ������� ������� ����������� ����������.
  24. // [IER_4] � �������� ��������� XX �� ����������� ������� [X1, Xn].
  25. // [IER_5] � ��������� ����� ���������� �������. ���� ���������� �����
  26. //           ����������.
  27. // [IER_6] � �������� ����� �����������
  28. // [IER_7] � P[i] < P[i-1]
  29. enum class IER {
  30.   IER_0,
  31.   IER_1,
  32.   IER_2,
  33.   IER_3,
  34.   IER_4,
  35.   IER_5,
  36.   IER_6,
  37.   IER_7,
  38. };
  39. #pragma endregion enumerations
  40. //---------������---------
  41. #pragma region classes
  42. class AppException : public exception {
  43.  public:
  44.   IER type;
  45.   AppException(IER type, const string& what)
  46.       : exception(what.c_str()), type(type){};
  47. };
  48.  
  49. #pragma endregion classes
  50.  
  51. //---------���������---------
  52. #pragma region constants
  53. using custom_double = decimal<3>;
  54.  
  55. //-----------�������� ���������--------------------------------------------+
  56. // [X]   � ������ �������� ���������� � ������� ����������� (������ �����
  57. // ������������).
  58. // [Y]   � ������ �������� ������� � ����� ������������.
  59. // [N]   - ���������� ����� ������������, � ������� ������ �������� �������.
  60. // [XX]  - �������� ���������, ��� ������� ����� �����������
  61. // ���������������� �������� �������. [EPS] �  �������� ������� �������
  62. // ���������� �����������.
  63. // [EPS]   - ���������� ����� ������������, � ������� ������ �������� �������.
  64. //
  65. //-----------�������� ���������--------------------------------------------+
  66. // [YY]  � ����������� ���������������� �������� ������� � ����� XX.
  67. // [IER] � ��������� ������.
  68. namespace ParametersFields {
  69. inline static const string X = "X";
  70. inline static const string Y = "Y";
  71. inline static const string N = "N";
  72. inline static const string XX = "XX";
  73. inline static const string EPS = "EPS";
  74. inline static const string YY = "YY";
  75. inline static const string IER = "IER";
  76. };  // namespace ParametersFields
  77.  
  78. static const string kFileInputName = "parameters.txt";
  79. static const string kFileOutputName = "result.txt";
  80. static const string kFileParameterDoubleEmptyField = ": ,\n";
  81. static const string kFileParameterVectorEmptyField = ": [],\n";
  82.  
  83. static const string kFileParametersTemplate =
  84.     ParametersFields::N + kFileParameterDoubleEmptyField + ParametersFields::X +
  85.     kFileParameterVectorEmptyField + ParametersFields::Y +
  86.     kFileParameterVectorEmptyField + ParametersFields::XX +
  87.     kFileParameterDoubleEmptyField + ParametersFields::EPS +
  88.     kFileParameterDoubleEmptyField;
  89.  
  90. #pragma endregion constants
  91.  
  92. //---------������---------
  93. #pragma region methods
  94. void removeSpaces(string& text) {
  95.   text.erase(
  96.       std::remove_if(
  97.           text.begin(), text.end(),
  98.           [](char c) { return std::isspace(static_cast<unsigned char>(c)); }),
  99.       text.end());
  100. }
  101. void parseVector(const string& text, vector<custom_double>& parameters) {
  102.   string temp = text;
  103.   if (temp.find(',') == 0)
  104.     temp = temp.substr(1, temp.size() - 1);
  105.  
  106.   stringstream textStream(temp);
  107.   for (double value; textStream >> value;) {
  108.     parameters.push_back(custom_double(value));
  109.     if (textStream.peek() == ',')
  110.       textStream.ignore();
  111.   }
  112. }
  113.  
  114. vector<custom_double> parseParameterVectorField(const string& text,
  115.                                                 const string& fieldName,
  116.                                                 const custom_double& N) {
  117.   vector<custom_double> parameters;
  118.   string tmp = text;
  119.   if (tmp.find(fieldName + ": [") == string::npos || !tmp.ends_with("],"))
  120.     throw AppException(
  121.         IER::IER_5, "������� ����� ���������� �������. ���� ����� ����������.");
  122.  
  123.   tmp = tmp.substr(tmp.find('[') + 1, tmp.size() - tmp.find('[') - 3);
  124.   removeSpaces(tmp);
  125.  
  126.   parseVector(tmp, parameters);
  127.  
  128.   if (parameters.empty())
  129.     throw AppException(IER::IER_6, "�������� [" + fieldName + "] ����������.");
  130.  
  131.   if (parameters.size() != N)
  132.     throw AppException(
  133.         IER::IER_6, "�������� [" + fieldName +
  134.                         "] �������� �����������. ���������� ���������� ������ "
  135.                         "��������� ��������� [N].");
  136.   if (fieldName == ParametersFields::X) {
  137.     bool isGrowing = true;
  138.     for (size_t i = 1; i < parameters.size(); i++)
  139.       if (parameters[i] < parameters[i - 1]) {
  140.         isGrowing = false;
  141.         break;
  142.       }
  143.     if (!isGrowing)
  144.       throw AppException(IER::IER_3,
  145.                          "�������� [" + fieldName +
  146.                              "] ������ ��������� ��������, ��������������� � "
  147.                              "������� �����������.");
  148.   }
  149.   return parameters;
  150. }
  151.  
  152. custom_double parseParameterDoubleField(const string& text,
  153.                                         const string& fieldName,
  154.                                         const vector<custom_double>& X) {
  155.   custom_double parameter;
  156.   string tmp = text;
  157.   if (text.find(fieldName + ':') == string::npos || !text.ends_with(',')) {
  158.     throw AppException(
  159.         IER::IER_5, "������� ����� ���������� �������. ���� ����� ����������.");
  160.   }
  161.  
  162.   tmp = tmp.substr(text.find(':') + 1, tmp.size() - text.find(':') - 2);
  163.   removeSpaces(tmp);
  164.  
  165.   if (tmp.empty())
  166.     throw AppException(IER::IER_6, "�������� [" + fieldName + "] ����������.");
  167.   parameter = custom_double(stod(tmp));
  168.  
  169.   if (fieldName == ParametersFields::XX) {
  170.     if (parameter < X[0] || parameter > X[X.size() - 1]) {
  171.       throw AppException(
  172.           IER::IER_4, "�������� ��������� XX ������ ������������ ������� X.");
  173.     }
  174.   }
  175.   return parameter;
  176. }
  177.  
  178. custom_double parseParameterLongIntField(const string& text,
  179.                                          const string& fieldName) {
  180.   custom_double parameter;
  181.   string tmp = text;
  182.   if (text.find(fieldName + ':') == string::npos || !text.ends_with(','))
  183.     throw AppException(
  184.         IER::IER_5, "������� ����� ���������� �������. ���� ����� ����������.");
  185.  
  186.   tmp = tmp.substr(text.find(':') + 1, tmp.size() - text.find(':') - 2);
  187.   removeSpaces(tmp);
  188.  
  189.   if (tmp.empty())
  190.     throw AppException(IER::IER_6, "�������� [" + fieldName + "] ����������.");
  191.  
  192.   parameter = custom_double(stoi(tmp));
  193.   if (parameter <= 0) {
  194.     throw AppException(IER::IER_6, "�������� [" + fieldName +
  195.                                        "] ������ ���� � ���������� (0;+inf).");
  196.   }
  197.   return parameter;
  198. }
  199. #pragma endregion methods
  200. #endif  // !APP_HEADER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement