Advertisement
lithie_oce

lab 3.1 c++

Nov 30th, 2023
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int choiceCheck() {
  8. int choice;
  9. bool isIncorrect;
  10. do {
  11. isIncorrect = true;
  12. cin >> choice;
  13. if (cin.fail() || (cin.get() != '\n')) {
  14. cout << "Error! Input a number" << endl;
  15. cin.clear();
  16. while (cin.get() != '\n');
  17. } else {
  18. cin.clear();
  19. if (choice > 0 && choice < 3) {
  20. isIncorrect = false;
  21. } else {
  22. cout << "Error! Input 1 or 2" << endl;
  23. }
  24. }
  25.  
  26. } while (isIncorrect);
  27. return choice;
  28. }
  29.  
  30. string checkInputFilePath() {
  31. string path;
  32. bool isInCorrect;
  33. do {
  34. cout << "Input path to the file:\n";
  35. isInCorrect = false;
  36. cin >> path;
  37. ifstream fin(path);
  38. if (!fin.is_open()) {
  39. cout << "Could not open the file" << endl;
  40. isInCorrect = true;
  41. } else {
  42. fin.close();
  43. }
  44.  
  45. } while (isInCorrect);
  46. return path;
  47. }
  48.  
  49. string inputCheck() {
  50. bool isIncorrect;
  51. string str;
  52. int i;
  53. do {
  54. cout
  55. << "Input the string consisting of numbers, letters of the English alphabet and symbols '-', '+', '.' "
  56. << endl;
  57. isIncorrect = false;
  58. cin >> str;
  59. for (i = 0; i < str.length(); i++) {
  60. if (!(str[i] == 43 || str[i] == 45 || str[i] == 46 || (str[i] > 47 && str[i] < 58) ||
  61. (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123))) {
  62. cout << "Incorrect string format\n";
  63. isIncorrect = true;
  64. }
  65. }
  66. } while (isIncorrect);
  67. return str;
  68. }
  69.  
  70. string fileCheckString() {
  71. string path, str;
  72. bool isIncorrect;
  73. int i;
  74. do {
  75. path = checkInputFilePath();
  76. ifstream fin(path);
  77. isIncorrect = false;
  78. fin >> str;
  79. if (fin.fail()) {
  80. cout << "The data is incorrect" << endl;
  81. isIncorrect = true;
  82. }
  83. if (!isIncorrect && (str.length() < 1)) {
  84. cout << "The string is empty" << endl;
  85. isIncorrect = true;
  86. }
  87. if (!fin.eof()) {
  88. cout << "There should be only one string" << endl;
  89. isIncorrect = true;
  90. }
  91. if (!isIncorrect) {
  92. for (i = 0; i < str.length(); i++) {
  93. if (!((str[i] == 43 || str[i] == 45 || str[i] == 46 || (str[i] > 47 && str[i] < 58) ||
  94. (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)))) {
  95. isIncorrect = true;
  96. }
  97. }
  98. }
  99. if (isIncorrect) {
  100. cout << "Incorrect string format" << endl;
  101. }
  102. fin.close();
  103. } while (isIncorrect);
  104. return str;
  105. }
  106.  
  107. string inputChoice() {
  108. int choice;
  109. string str;
  110. cout << "Choose input option:\n1.Input through console\n2.Input through file" << endl;
  111. choice = choiceCheck();
  112. if (choice == 1) {
  113. str = inputCheck();
  114. } else {
  115. str = fileCheckString();
  116. }
  117. return str;
  118. }
  119.  
  120. string checkOutputFilePath() {
  121. string path;
  122. bool isIncorrect;
  123. cout
  124. << "Input file path and the name of the file for\nexample C:\\Projects\\Number\\FileName.txt. If the\nfile does not exist, then it will be created\nautomatically in the root folder of the program:\n";
  125. isIncorrect = false;
  126. cin >> path;
  127. ifstream fin(path);
  128. if (!fin.is_open()) {
  129. cout << "Could not open the file or it does not exist" << endl;
  130. isIncorrect = true;
  131. } else {
  132. fin.close();
  133. }
  134. if (isIncorrect) {
  135. cout << "File will be created in the root folder of the program\n";
  136. path = "Result.txt";
  137. }
  138. return path;
  139. }
  140.  
  141. void findConsoleSubStr(string str) {
  142. string subStr;
  143. int i;
  144. bool isFound;
  145. isFound = false;
  146. subStr = "";
  147. for (i = 0; i < str.length(); i++) {
  148. if (str[i] == '+' || str[i] == '-') {
  149. if (subStr.length() > 1) {
  150. cout << subStr << endl;
  151. } else {
  152. if (i>1) && (str[i - 1] == '0') {
  153. cout << "0" << endl;
  154. }
  155. }
  156. subStr = "";
  157. subStr += str[i];
  158. isFound = true;
  159. } else {
  160. if (str[i] == 46 || (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)) {
  161. isFound = false;
  162. if (subStr.length() > 1) {
  163. cout << subStr << endl;
  164. } else {
  165. if (i>1) && (str[i - 1] == '0') {
  166. cout << "0" << endl;
  167. }
  168. }
  169. subStr = "";
  170. } else {
  171. if (str[i] == 48) {
  172. if (isFound && !(subStr[subStr.length() - 1] == '+' || subStr[subStr.length() - 1] == '-')) {
  173. subStr += str[i];
  174. }
  175. } else {
  176. if (str[i] > 48 && str[i] < 58) {
  177. if (isFound) {
  178. subStr += str[i];
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. if (isFound && subStr.length() > 1) {
  186. cout << subStr << endl;
  187. }
  188. }
  189.  
  190. void findFileSubStr(string str) {
  191. string subStr, path;
  192. int i;
  193. bool isFound;
  194. isFound = false;
  195. subStr = "";
  196. path = checkOutputFilePath();
  197. ofstream fout(path);
  198. subStr = "";
  199. for (i = 0; i < str.length(); i++) {
  200. if (str[i] == '+' || str[i] == '-') {
  201. if (subStr.length() > 1) {
  202. fout << subStr << endl;
  203. } else {
  204. if (i>1) && (str[i - 1] == '0') {
  205. fout << "0" << endl;
  206. }
  207. }
  208. subStr = "";
  209. subStr += str[i];
  210. isFound = true;
  211. } else {
  212. if (str[i] == 46 || (str[i] > 64 && str[i] < 91) || (str[i] > 96 && str[i] < 123)) {
  213. isFound = false;
  214. if (subStr.length() > 1) {
  215. fout << subStr << endl;
  216. } else {
  217. if (i>1) && (str[i - 1] == '0') {
  218. fout << "0" << endl;
  219. }
  220. }
  221. subStr = "";
  222. } else {
  223. if (str[i] == 48) {
  224. if (isFound && !(subStr[subStr.length() - 1] == '+' || subStr[subStr.length() - 1] == '-')) {
  225. subStr += str[i];
  226. }
  227. } else {
  228. if (str[i] > 48 && str[i] < 58) {
  229. if (isFound) {
  230. subStr += str[i];
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. if (isFound && subStr.length() > 1) {
  238. fout << subStr << endl;
  239. }
  240. cout << "Successful output\n";
  241. }
  242.  
  243. void outputChoice(string str) {
  244. int choice;
  245. cout << "Choose output option:\n1.Output through console\n2.Output through file" << endl;
  246. choice = choiceCheck();
  247. if (choice == 1) {
  248. findConsoleSubStr(str);
  249. } else {
  250. findFileSubStr(str);
  251. }
  252. }
  253.  
  254. int main() {
  255. string str;
  256. str = inputChoice();
  257. outputChoice(str);
  258. return 0;
  259. };
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement