Advertisement
Sanlover

Untitled

Nov 9th, 2021
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.34 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. using ud_t = pair<size_t, size_t>;
  7. using first_check_t = pair<bool, ud_t>;
  8.  
  9. static const string menuMainText =
  10.     "Меню:\n1)Перевод из ДУ в РАД\n2)Первод из РАД в ДУ\nESC - выход\n";
  11. static const string menuFirstText = "";
  12. static const string inputFirstText = "Введите ДУ в формате(XX-XX или X-XX): ";
  13. static const string inputTextSuccess =
  14.     "\n\n Нажмите, если хотите:\n1 - Повторить ввод\n2 - Вернуться "
  15.     "назад\nESC - Выйти";
  16. static const string inputFirstTextError =
  17.     "Некорректный ввод.\nУдостоверьтесь,что ввод производился в формате(XX-XX "
  18.     "или X-XX).\n\nНажмите, если хотите:\n1 - Повторить ввод\n2 - Вернуться "
  19.     "назад\nESC - Выйти";
  20. static const string inputSecondText =
  21.     "Введите РАДИАНЫ в формате целых чисел по очереди(ГРАДУСЫ - enter, МИНУТы "
  22.     "- enter, СЕКУНДЫ - enter):\n";
  23.  
  24. static void cleanScreen() {
  25.   system("cls");
  26. }
  27. static void sleep(const size_t& milliseconds) {
  28.   Sleep(milliseconds);
  29. }
  30. static bool isPressed() {
  31.   return _kbhit();
  32. }
  33. static void printMenu() {
  34.   cout << menuMainText;
  35. }
  36. static void inputSymbol(char& symbol) {
  37.   symbol = _getch();
  38. }
  39. static void exitProgram() {
  40.   cleanScreen();
  41.   sleep(50);
  42.   cout << "Завершение программы";
  43.   sleep(1000);
  44.   cleanScreen();
  45.   exit(0);
  46. };
  47. static first_check_t isGoodFirstInput(const string& input) {
  48.   size_t separatorId = input.find('-');
  49.   if (separatorId == string::npos || input.length() != separatorId + 3) {
  50.     return first_check_t(false, ud_t());
  51.   }
  52.   string left = input.substr(0, separatorId);
  53.   string right = input.substr(separatorId + 1);
  54.   for (auto& it : left) {
  55.     if (!isdigit(it)) {
  56.       return first_check_t(false, ud_t());
  57.     }
  58.   }
  59.   for (auto& it : right) {
  60.     if (!isdigit(it)) {
  61.       return first_check_t(false, ud_t());
  62.     }
  63.   }
  64.   return first_check_t(true, ud_t(stoi(left), stoi(right)));
  65. }
  66. static bool isGoodSecondInput(const string& grads,
  67.                               const string& mins,
  68.                               const string& secs) {
  69.   for (auto& it : grads) {
  70.     if (!isdigit(it)) {
  71.       return false;
  72.     }
  73.   }
  74.   for (auto& it : mins) {
  75.     if (!isdigit(it)) {
  76.       return false;
  77.     }
  78.   }
  79.   for (auto& it : secs) {
  80.     if (!isdigit(it)) {
  81.       return false;
  82.     }
  83.   }
  84.   return true;
  85. }
  86. static void handleError(const char& symbol, bool& isDone, bool& isWorking) {
  87.   switch (symbol) {
  88.     case '1': {
  89.       isDone = true;
  90.     } break;
  91.     case '2': {
  92.       isWorking = false;
  93.       isDone = true;
  94.     } break;
  95.     case 27: {
  96.       exitProgram();
  97.     } break;
  98.   }
  99. }
  100. static void firstFunc();
  101. static void secondFunc();
  102. static void handleMainInput(const char& symbol) {
  103.   switch (symbol) {
  104.     case '1': {
  105.       firstFunc();
  106.     } break;
  107.     case '2': {
  108.       secondFunc();
  109.     } break;
  110.     case 27: {
  111.       exitProgram();
  112.     } break;
  113.   }
  114. }
  115. static void firstFunc() {
  116.   bool isWorking = true;
  117.   while (isWorking) {
  118.     sleep(150);
  119.     cleanScreen();
  120.     string input;
  121.     cout << inputFirstText;
  122.     getline(cin, input);
  123.  
  124.     first_check_t response = isGoodFirstInput(input);
  125.     if (response.first == true) {
  126.       int grads = response.second.first * 6;
  127.       double secondsTemp = response.second.second * 3.6 * 60;
  128.       int seconds, mins;
  129.       mins = int(secondsTemp / 60);
  130.       seconds = secondsTemp - (mins * 60);
  131.       int difference = int(mins / 60);
  132.       grads += difference;
  133.       mins = mins - difference * 60;
  134.  
  135.       sleep(350);
  136.       cleanScreen();
  137.       cout << "Результаты:" << endl;
  138.       cout << "Градусы: " << grads << endl;
  139.       cout << "Минуты: " << mins << endl;
  140.       cout << "Секунды: " << seconds << endl;
  141.       char symbol;
  142.       bool isDone = false;
  143.       while (!isDone) {
  144.         if (!isPressed()) {
  145.           cout << inputTextSuccess;
  146.           inputSymbol(symbol);
  147.           handleError(symbol, isDone, isWorking);
  148.         }
  149.       }
  150.     } else {
  151.       char symbol;
  152.       bool isDone = false;
  153.       while (!isDone) {
  154.         if (!isPressed()) {
  155.           cleanScreen();
  156.           cout << inputFirstTextError;
  157.           inputSymbol(symbol);
  158.           handleError(symbol, isDone, isWorking);
  159.         }
  160.       }
  161.     }
  162.   }
  163. }
  164.  
  165. static void secondFunc() {
  166.   bool isWorking = true;
  167.   while (isWorking) {
  168.     sleep(150);
  169.     cleanScreen();
  170.     string input;
  171.     cout << inputSecondText;
  172.     string gradsStr, secsStr, minsStr;
  173.     cout << "Градусы: ";
  174.     getline(cin, gradsStr);
  175.     cout << "Минуты: ";
  176.     getline(cin, minsStr);
  177.     cout << "Секунды: ";
  178.     getline(cin, secsStr);
  179.  
  180.     if (isGoodSecondInput(gradsStr, minsStr, secsStr) == true) {
  181.       int grads = stoi(gradsStr);
  182.       int mins = stoi(minsStr);
  183.       int secs = stoi(secsStr);
  184.  
  185.       int left, right;
  186.       left = grads / 6;
  187.       grads -= left * 6;
  188.       mins += grads * 60;
  189.       secs += mins * 60;
  190.       right = secs / 3.6 / 60;
  191.       left += right / 60;
  192.       right = right % 60;
  193.       string rightTmp =
  194.           right < 10 ? '0' + std::to_string(right) : std::to_string(right);
  195.       sleep(350);
  196.       cleanScreen();
  197.       cout << "Результаты:" << endl;
  198.       cout << "ДУ: " << left << '-' << rightTmp << endl;
  199.       char symbol;
  200.       bool isDone = false;
  201.       while (!isDone) {
  202.         if (!isPressed()) {
  203.           cout << inputTextSuccess;
  204.           inputSymbol(symbol);
  205.           handleError(symbol, isDone, isWorking);
  206.         }
  207.       }
  208.     } else {
  209.       char symbol;
  210.       bool isDone = false;
  211.       while (!isDone) {
  212.         if (!isPressed()) {
  213.           cleanScreen();
  214.           cout << inputFirstTextError;
  215.           inputSymbol(symbol);
  216.           handleError(symbol, isDone, isWorking);
  217.         }
  218.       }
  219.     }
  220.   }
  221. }
  222. int main() {
  223.   SetConsoleCP(1251);
  224.   SetConsoleOutputCP(1251);
  225.   char symbol = ' ';
  226.  
  227.   while (true) {
  228.     if (!isPressed()) {
  229.       cleanScreen();
  230.       printMenu();
  231.       inputSymbol(symbol);
  232.       handleMainInput(symbol);
  233.     }
  234.   }
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement