Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <conio.h>
- #include <iostream>
- #include <string>
- using namespace std;
- using ud_t = pair<size_t, size_t>;
- using first_check_t = pair<bool, ud_t>;
- static const string menuMainText =
- "Меню:\n1)Перевод из ДУ в РАД\n2)Первод из РАД в ДУ\nESC - выход\n";
- static const string menuFirstText = "";
- static const string inputFirstText = "Введите ДУ в формате(XX-XX или X-XX): ";
- static const string inputTextSuccess =
- "\n\n Нажмите, если хотите:\n1 - Повторить ввод\n2 - Вернуться "
- "назад\nESC - Выйти";
- static const string inputFirstTextError =
- "Некорректный ввод.\nУдостоверьтесь,что ввод производился в формате(XX-XX "
- "или X-XX).\n\nНажмите, если хотите:\n1 - Повторить ввод\n2 - Вернуться "
- "назад\nESC - Выйти";
- static const string inputSecondText =
- "Введите РАДИАНЫ в формате целых чисел по очереди(ГРАДУСЫ - enter, МИНУТы "
- "- enter, СЕКУНДЫ - enter):\n";
- static void cleanScreen() {
- system("cls");
- }
- static void sleep(const size_t& milliseconds) {
- Sleep(milliseconds);
- }
- static bool isPressed() {
- return _kbhit();
- }
- static void printMenu() {
- cout << menuMainText;
- }
- static void inputSymbol(char& symbol) {
- symbol = _getch();
- }
- static void exitProgram() {
- cleanScreen();
- sleep(50);
- cout << "Завершение программы";
- sleep(1000);
- cleanScreen();
- exit(0);
- };
- static first_check_t isGoodFirstInput(const string& input) {
- size_t separatorId = input.find('-');
- if (separatorId == string::npos || input.length() != separatorId + 3) {
- return first_check_t(false, ud_t());
- }
- string left = input.substr(0, separatorId);
- string right = input.substr(separatorId + 1);
- for (auto& it : left) {
- if (!isdigit(it)) {
- return first_check_t(false, ud_t());
- }
- }
- for (auto& it : right) {
- if (!isdigit(it)) {
- return first_check_t(false, ud_t());
- }
- }
- return first_check_t(true, ud_t(stoi(left), stoi(right)));
- }
- static bool isGoodSecondInput(const string& grads,
- const string& mins,
- const string& secs) {
- for (auto& it : grads) {
- if (!isdigit(it)) {
- return false;
- }
- }
- for (auto& it : mins) {
- if (!isdigit(it)) {
- return false;
- }
- }
- for (auto& it : secs) {
- if (!isdigit(it)) {
- return false;
- }
- }
- return true;
- }
- static void handleError(const char& symbol, bool& isDone, bool& isWorking) {
- switch (symbol) {
- case '1': {
- isDone = true;
- } break;
- case '2': {
- isWorking = false;
- isDone = true;
- } break;
- case 27: {
- exitProgram();
- } break;
- }
- }
- static void firstFunc();
- static void secondFunc();
- static void handleMainInput(const char& symbol) {
- switch (symbol) {
- case '1': {
- firstFunc();
- } break;
- case '2': {
- secondFunc();
- } break;
- case 27: {
- exitProgram();
- } break;
- }
- }
- static void firstFunc() {
- bool isWorking = true;
- while (isWorking) {
- sleep(150);
- cleanScreen();
- string input;
- cout << inputFirstText;
- getline(cin, input);
- first_check_t response = isGoodFirstInput(input);
- if (response.first == true) {
- int grads = response.second.first * 6;
- double secondsTemp = response.second.second * 3.6 * 60;
- int seconds, mins;
- mins = int(secondsTemp / 60);
- seconds = secondsTemp - (mins * 60);
- int difference = int(mins / 60);
- grads += difference;
- mins = mins - difference * 60;
- sleep(350);
- cleanScreen();
- cout << "Результаты:" << endl;
- cout << "Градусы: " << grads << endl;
- cout << "Минуты: " << mins << endl;
- cout << "Секунды: " << seconds << endl;
- char symbol;
- bool isDone = false;
- while (!isDone) {
- if (!isPressed()) {
- cout << inputTextSuccess;
- inputSymbol(symbol);
- handleError(symbol, isDone, isWorking);
- }
- }
- } else {
- char symbol;
- bool isDone = false;
- while (!isDone) {
- if (!isPressed()) {
- cleanScreen();
- cout << inputFirstTextError;
- inputSymbol(symbol);
- handleError(symbol, isDone, isWorking);
- }
- }
- }
- }
- }
- static void secondFunc() {
- bool isWorking = true;
- while (isWorking) {
- sleep(150);
- cleanScreen();
- string input;
- cout << inputSecondText;
- string gradsStr, secsStr, minsStr;
- cout << "Градусы: ";
- getline(cin, gradsStr);
- cout << "Минуты: ";
- getline(cin, minsStr);
- cout << "Секунды: ";
- getline(cin, secsStr);
- if (isGoodSecondInput(gradsStr, minsStr, secsStr) == true) {
- int grads = stoi(gradsStr);
- int mins = stoi(minsStr);
- int secs = stoi(secsStr);
- int left, right;
- left = grads / 6;
- grads -= left * 6;
- mins += grads * 60;
- secs += mins * 60;
- right = secs / 3.6 / 60;
- left += right / 60;
- right = right % 60;
- string rightTmp =
- right < 10 ? '0' + std::to_string(right) : std::to_string(right);
- sleep(350);
- cleanScreen();
- cout << "Результаты:" << endl;
- cout << "ДУ: " << left << '-' << rightTmp << endl;
- char symbol;
- bool isDone = false;
- while (!isDone) {
- if (!isPressed()) {
- cout << inputTextSuccess;
- inputSymbol(symbol);
- handleError(symbol, isDone, isWorking);
- }
- }
- } else {
- char symbol;
- bool isDone = false;
- while (!isDone) {
- if (!isPressed()) {
- cleanScreen();
- cout << inputFirstTextError;
- inputSymbol(symbol);
- handleError(symbol, isDone, isWorking);
- }
- }
- }
- }
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- char symbol = ' ';
- while (true) {
- if (!isPressed()) {
- cleanScreen();
- printMenu();
- inputSymbol(symbol);
- handleMainInput(symbol);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement