Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. файл MyException.h
  2.  
  3. #pragma once
  4. #include <iostream>
  5. #include <string>
  6.  
  7. const int indent = 11;
  8.  
  9. using namespace std;
  10.  
  11. class MyException
  12. {
  13. protected:
  14. string message;
  15. public:
  16. MyException(string message = "ERR")
  17. {
  18. this->message = message;
  19. }
  20. virtual ~MyException() {}
  21. string getMessage() { return this->message; }
  22. };
  23.  
  24.  
  25. файл NumberException.h
  26.  
  27. #pragma once
  28. #include "MyException.h"
  29. class NumberException :
  30. public MyException
  31. {
  32. public:
  33. NumberException(string msg = "") :MyException() { this->message += 'N'; this->message += msg; }
  34. ~NumberException() {}
  35. };
  36.  
  37. int input_int(int low, int high);
  38. double input_double(double low, double high);
  39.  
  40. файл NumberException.cpp
  41.  
  42. #include "NumberException.h"
  43.  
  44.  
  45. int input_int(int low, int high)
  46. {
  47. {
  48. int number;
  49. bool flag = true;
  50. while (flag)
  51. {
  52. flag = 0;
  53. try
  54. {
  55. cin >> number;
  56. if (cin.peek() != '\n' || cin.fail())
  57. throw NumberException("132: use only integer numbers!");
  58. if (number < low || number > high)
  59. throw NumberException("133: wrong number");
  60. }
  61. catch (MyException &a)
  62. {
  63. cout << a.getMessage() << endl;
  64. flag = 1;
  65. rewind(stdin);
  66. cin.clear();
  67. }
  68. }
  69. return number;
  70. }
  71.  
  72. }
  73.  
  74. double input_double(double low, double high)
  75. {
  76. double number;
  77. bool flag = true;
  78. while (flag)
  79. {
  80. flag = 0;
  81. try
  82. {
  83. cin >> number;
  84. if (cin.peek() != '\n' || cin.fail())
  85. throw NumberException("134: use only double numbers!");
  86. if (number < low || number > high)
  87. throw NumberException("135: wrong number");
  88. }
  89. catch (MyException &a)
  90. {
  91. cout << a.getMessage() << endl;
  92. flag = 1;
  93. rewind(stdin);
  94. cin.clear();
  95. }
  96. }
  97. return number;
  98. }
  99.  
  100. файл StringException.h
  101.  
  102. #pragma once
  103. #include "MyException.h"
  104. class StringException :
  105. public MyException
  106. {
  107. public:
  108. StringException(string msg = "") { this->message += 'S'; this->message += msg; }
  109. ~StringException() {}
  110. };
  111. string input_string_wihout_numbers(int size = indent - 1);
  112. string input_string(int size = indent - 1);
  113.  
  114. файл StringException.cpp
  115.  
  116. #include "StringException.h"
  117.  
  118.  
  119. string input_string_wihout_numbers(int size)
  120. {
  121. string str;
  122. bool flag = true;
  123. while (flag)
  124. {
  125. flag = 0;
  126. try
  127. {
  128. cin >> str;
  129. if (str.size() > size)
  130. throw StringException("123: too big string");
  131. for (int i = 0; i < str.size(); i++)
  132. if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' || str[i] == '-');
  133. else throw StringException("124: use only english letters!");
  134. }
  135. catch (StringException &a)
  136. {
  137. cout << a.getMessage() << endl;
  138. flag = 1;
  139. rewind(stdin);
  140. cin.clear();
  141. }
  142. }
  143. return str;
  144. }
  145.  
  146. string input_string(int size)
  147. {
  148. string str;
  149. bool flag = true;
  150. while (flag)
  151. {
  152. flag = 0;
  153. try
  154. {
  155. cin >> str;
  156. if (str.size() > size)
  157. throw StringException("125: too big string");
  158. }
  159. catch (StringException&a)
  160. {
  161. cout << a.getMessage() << endl;
  162. flag = 1;
  163. rewind(stdin);
  164. cin.clear();
  165. }
  166. }
  167. return str;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement