-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
206 lines (187 loc) · 7.56 KB
/
menu.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#define NOMINMAX
#include <iostream>
#include <vector>
#include "string.h"
#include "string-id.h"
void print_strings(std::vector<String>& strings);
int get_id_from_console(std::vector<String>& strings);
String_ID get_string(std::vector<String>& strings);
int main() {
std::vector <String> strings;
const int CREATE_EMPTY_STRING = 1;
const int CREATE_COPY_OF_STRING_BY_ID = 2;
const int CREATE_STRING_FROM_INPUT_STRING = 3;
const int CREATE_STRING_FROM_INPUT_SYMBOL = 4;
const int EQUATE_STRINGS = 5;
const int ADD = 6;
const int SUBSTRACT = 7;
const int MORE = 8;
const int LESS = 9;
const int STRING_TO_LOWERCASE = 10;
const int STRING_TO_UPPERCASE = 11;
const int FIND_INDEX_OF_ENTRY = 12;
const int DELETE_STRING = 13;
const int GET_ALL_STRINGS = 14;
const int EXIT = 15;
while (true) {
print_strings(strings);
std::cout << "\n-----\n" << "Menu:\n" << "-----\n";
std::cout << "1. Create an empty string.\n2. Create copy of string by ID.\n3. Create string from input string.\n4. Create string from input symbol.\n";
std::cout << "5. (=) Equate strings by ID\n6. (+) Add string n1 + string n2.\n7. (-) Subtract string n1 from string n2.\n";
std::cout << "8. (>) String n1 > than string n2.\n9. (<) String n1 < than string n2.\n10. Convert string to lowercase.\n11. Convert string to uppercase.\n";
std::cout << "12. Find first entry of element X to string.\n13. Delete element by ID.\n";
std::cout << "14. Get all strings.\n15. Exit.\n\n";
std::cout << "\tSelect your choice: ";
int choice;
if (std::cin >> choice) {
switch (choice) {
case CREATE_EMPTY_STRING: {
strings.push_back(String_ID());
break;
}
case CREATE_COPY_OF_STRING_BY_ID: {
std::cout << "\nWrite a ID of string for copy: ";
String_ID string = get_string(strings);
strings.push_back(string);
break;
}
case CREATE_STRING_FROM_INPUT_STRING: {
std::cout << "\nWrite string: ";
std::cin.ignore();
std::string line;
std::getline(std::cin, line);
char* string = new char[line.size() + 1];
for (int i = 0; i < line.size() + 1; i++) {
string[i] = line[i];
}
strings.push_back(String_ID(string));
break;
}
case CREATE_STRING_FROM_INPUT_SYMBOL: {
std::cout << "\nWrite one symbol: ";
char symbol;
std::cin >> symbol;
strings.push_back(String_ID(symbol));
break;
}
case EQUATE_STRINGS: {
String_ID copy_string = get_string(strings);
std::cout << "\nCreate new string: " << copy_string.get_string();
strings.push_back(copy_string);
break;
}
case ADD: {
std::cout << "\nAdd string1 to string2";
String_ID first_string = get_string(strings);
String_ID second_string = get_string(strings);
strings.push_back(first_string + second_string);
break;
}
case SUBSTRACT: {
std::cout << "\nSubstract strings";
String_ID first_string = get_string(strings);
String_ID second_string = get_string(strings);
strings.push_back(first_string - second_string);
break;
}
case MORE: {
String_ID first_string = get_string(strings);
String_ID second_string = get_string(strings);
int result = first_string > second_string;
if (result == 1) {
std::cout << "\nYes, " << first_string.get_string() << " bigger than " << second_string.get_string();
}
else if (result == 0) {
std::cout << "\nNo, " << first_string.get_string() << " not bigger than " << second_string.get_string();
}
else std::cout << "\nStrings is equals: " << first_string.get_string() << "==" << second_string.get_string();
break;
}
case LESS: {
String_ID first_string = get_string(strings);
String_ID second_string = get_string(strings);
int result = first_string < second_string;
if (result == 1) {
std::cout << "\nYes, " << first_string.get_string() << " less than " << second_string.get_string();
}
else if (result == 0) {
std::cout << "\nNo, " << first_string.get_string() << " not less than " << second_string.get_string();
}
else std::cout << "\nStrings is equals: " << first_string.get_string() << "==" << second_string.get_string();
break;
}
case STRING_TO_LOWERCASE: {
int string_id = get_id_from_console(strings);
String_ID string = String_ID(strings[string_id - 1]);
string.lower();
strings[string_id - 1] = string;
break;
}
case STRING_TO_UPPERCASE: {
int string_id = get_id_from_console(strings);
String_ID string = String_ID(strings[string_id - 1]);
string.upper();
strings[string_id - 1] = string;
break;
}
case FIND_INDEX_OF_ENTRY: {
String_ID string = get_string(strings);
std::cout << "\nWrite string to find it in " << string.get_string() << " :";
char str;
std::cin >> str;
std::cout << "\nIndex=" << string.find(str);
break;
}
case DELETE_STRING: {
int string_id = get_id_from_console(strings);
strings.erase(strings.cbegin() + string_id - 1);
break;
}
case GET_ALL_STRINGS: {
print_strings(strings);
break;
}
case EXIT:
return 0;
default:
std::cout << "\nPlease enter a choice from Menu\n";
break;
}
}
else {
std::cout << "\nPlease enter a valid number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
void print_strings(std::vector<String>& strings) {
std::cout << "\nStrings:\n";
for (int i = 0; i < strings.size(); i++) {
std::cout << "\nN(" << i + 1 << ")=" << strings[i].get_string();
}
std::cout << "\n";
}
int get_id_from_console(std::vector<String>& strings) {
while (true) {
std::cout << "\nID=";
int id;
if (std::cin >> id) {
if (id >= 1 && id <= strings.size()) {
return id;
}
else {
std::cout << "\nPlease enter ID from list of all strings\n";
}
}
else {
std::cout << "\nPlease enter a valid ID\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
String_ID get_string(std::vector<String>& strings) {
int string_id = get_id_from_console(strings);
return (String_ID)strings[string_id - 1];
}