-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.cpp
More file actions
109 lines (86 loc) · 3.02 KB
/
Copy pathsource.cpp
File metadata and controls
109 lines (86 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <limits>
#include "magic_square.h"
constexpr int kMinMenuChoice = 1;
constexpr int kMaxMenuChoice = 3;
constexpr int kMinSquareSize = 1;
void DisplayMenu() {
std::cout << " ========== Меню ==========\n";
std::cout << " 1) Создать магический квадрат\n";
std::cout << " 2) Что такое магический квадрат?\n";
std::cout << " 3) Выход\n";
std::cout << "Введите номер пункта:\n>> ";
}
int GetMagicSquareSize() {
int size = 0;
std::cout << "Введите нечетный порядок квадрата:\n>> ";
while (true) {
if (!(std::cin >> size)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Ошибка! Введите число от 3 до 2,147,483,646 :\n>> ";
continue;
}
if (size <= kMinSquareSize) {
std::cout << "Порядок должен быть положительным! Попробуйте снова:\n>> ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
if (size % 2 == 0) {
std::cout << "Порядок должен быть нечетным! Попробуйте снова:\n>> ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
break;
}
return size;
}
bool PromptToContinue() {
char choice = '\0';
std::cout << "\nВыполнить еще одну операцию? (y/n): ";
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return !(choice == 'n' || choice == 'N');
}
void WaitForEnter() {
std::cout << "\nНажмите Enter для возврата в меню...";
std::cin.get();
}
int main() {
int menu_choice = 0;
bool exit_program = false;
while (!exit_program) {
DisplayMenu();
while (!(std::cin >> menu_choice) ||
menu_choice < kMinMenuChoice ||
menu_choice > kMaxMenuChoice) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Неверный ввод! Выберите пункт 1-3:\n>> ";
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (menu_choice) {
case 1: {
const int square_size = GetMagicSquareSize();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
GenerateMagicSquare(square_size);
if (!PromptToContinue()) {
exit_program = true;
}
break;
}
case 2:
DisplaySquareInfo();
WaitForEnter();
break;
case 3:
exit_program = true;
break;
default:
std::cerr << "Неожиданный выбор меню: " << menu_choice << "\n";
break;
}
}
std::cout << "Программа завершена. До свидания!\n";
return 0;
}