This commit is contained in:
2025-04-28 20:17:29 +03:00
parent 1e23cedb7c
commit 6e8e69728e
7 changed files with 320 additions and 1 deletions
+34
View File
@@ -0,0 +1,34 @@
// #include <iostream>
#include <vector>
template <typename T>
size_t incremental_seq(const T& cnt) {
int max_len = 0;
auto iter = cnt.begin();
int last = *iter;
int now = 1;
while (iter != cnt.end()) {
if (last + 1 == *iter) {
++now;
} else {
max_len = std::max(max_len, now);
now = 1;
}
last = *iter;
++iter;
}
return max_len;
}
// int main() {
// std::vector v{ 4,1,2,3,4,3,4,5 };
// std::cout << incremental_seq(v) << std::endl;
// return 0;
// }
+38
View File
@@ -0,0 +1,38 @@
#include <vector>
#include <iostream>
#include <algorithm>
template <typename I, typename F>
I separate(I beg, I end, F pred) {
auto tmp = beg;
while (tmp != end) {
std::cout << *tmp << std::endl;
++tmp;
}
auto r = remove_if(beg, end, pred);
tmp = beg;
while (tmp != end) {
std::cout << *tmp << std::endl;
++tmp;
}
std::cout << "meow" << std::endl;
return r;
}
int main() {
std::vector v{ 1, -2, 3, -4, 5, -6 };
auto r = separate(v.begin(), v.end(), [](auto x) { return x >= 0; });
// std::cout << std::distance(v.begin(), r) << std::endl << std::endl;
for (auto& item : v) {
std::cout << item << std::endl;
}
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
#include <vector>
// #include <iostream>
#include <algorithm>
template <typename I, typename F>
I separate(I beg, I end, F pred) {
std::vector<int> mas_to;
std::copy_if(beg, end, std::back_inserter(mas_to), pred);
auto r = remove_if(beg, end, pred);
std::copy(mas_to.begin(), mas_to.end(), r);
return r;
}
// int main() {
// std::vector v{ 1, -2, 3, -4, 5, -6 };
// auto r = separate(v.begin(), v.end(), [](auto x) { return x >= 0; });
// // std::cout << std::distance(v.begin(), r) << std::endl << std::endl;
// std::cout << "meow" << std::endl;
// for (auto& item : v) {
// std::cout << item << std::endl;
// }
// return 0;
// }
+115
View File
@@ -0,0 +1,115 @@
#include <map>
#include <iostream>
bool is_num(std::string& s) {
auto iter = s.begin();
while (iter != s.end() && std::isdigit(*iter)) ++iter;
return iter == s.end();
}
enum TokenKind
{
T_EOF, // end of input stream
T_NUMBER, // int64 number
T_ID, // identifier
K_ALG,
K_ARG,
K_RES,
K_NACH,
K_KON,
K_TSEL,
K_VESHCH,
K_SIM,
K_LIT,
K_LOG,
K_TAB,
K_NTS,
K_KTS,
K_DLIN,
K_DANO,
K_NADO,
K_ESLI,
K_TO,
K_INACHE,
K_VSE,
K_POKA,
K_DLYA,
K_OT,
K_DO,
K_ZNACH,
K_I,
K_ILI,
K_NE,
K_DA,
K_NET,
K_PRI,
K_VYBOR,
K_VVOD,
K_VYVOD,
K_UTV,
T_SEMICOLON,
T_COMMA,
T_ASSIGN,
T_LBR,
T_RBR,
T_LSQBR,
T_RSQBR,
T_PLUS,
T_MINUS,
T_MUL,
T_DIV,
T_EQ,
T_NE,
T_GT,
T_GE,
T_LT,
T_LE
};
const std::string tokens[] =
{
"", // EOF
"", // NUMBER
"", // ID
"алг", "арг", "рез", "нач", "кон", "цел", "вещ", "сим", "лит", "лог",
"таб", "нц", "кц", "длин", "дано", "надо", "если", "то", "иначе", "все",
"пока", "для", "от", "до", "знач", "и", "или", "не", "да", "нет",
"при", "выбор", "ввод", "вывод", "утв",
";", ",", ":=", "(", ")", "[", "]", "+", "-", "*",
"/", "=", "!=", ">", ">=", "<", "<=",
};
int main() {
// строим словарь соответствий
std::map<std::string, TokenKind> mp;
int tmp = 0;
for (auto& wd : tokens) {
mp[wd] = TokenKind(tmp++);
}
// std::cout << mp["алг"] << " " << mp[";"];
// std::string s1("123");
// std::string s2("12a3");
// std::cout << is_num(s1) << is_num(s2);
// return -1;
std::string word;
while (std::cin >> word) {
if (mp.find(word) != mp.end()) {
std::cout << mp[word];
} else if (is_num(word)) {
std::cout << 1 << ':' << word;
} else {
std::cout << 2 << ':' << word;
}
std::cout << std::endl;
}
std::cout << 0 << std::endl;
return 0;
}
+103
View File
@@ -0,0 +1,103 @@
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
enum TokenKind
{
T_EOF, // end of input stream
T_NUMBER, // int64 number
T_ID, // identifier
K_ALG,
K_ARG,
K_RES,
K_NACH,
K_KON,
K_TSEL,
K_VESHCH,
K_SIM,
K_LIT,
K_LOG,
K_TAB,
K_NTS,
K_KTS,
K_DLIN,
K_DANO,
K_NADO,
K_ESLI,
K_TO,
K_INACHE,
K_VSE,
K_POKA,
K_DLYA,
K_OT,
K_DO,
K_ZNACH,
K_I,
K_ILI,
K_NE,
K_DA,
K_NET,
K_PRI,
K_VYBOR,
K_VVOD,
K_VYVOD,
K_UTV,
T_SEMICOLON,
T_COMMA,
T_ASSIGN,
T_LBR,
T_RBR,
T_LSQBR,
T_RSQBR,
T_PLUS,
T_MINUS,
T_MUL,
T_DIV,
T_EQ,
T_NE,
T_GT,
T_GE,
T_LT,
T_LE
};
const std::string tokens[] =
{
"", // EOF
"", // NUMBER
"", // ID
"алг", "арг", "рез", "нач", "кон", "цел", "вещ", "сим", "лит", "лог",
"таб", "нц", "кц", "длин", "дано", "надо", "если", "то", "иначе", "все",
"пока", "для", "от", "до", "знач", "и", "или", "не", "да", "нет",
"при", "выбор", "ввод", "вывод", "утв",
";", ",", ":=", "(", ")", "[", "]", "+", "-", "*",
"/", "=", "!=", ">", ">=", "<", "<=",
};
int main() {
// строим словарь соответствий
std::map<std::string, TokenKind> mp;
int tmp = 0;
for (auto& wd : tokens) {
mp[wd] = TokenKind(tmp++);
}
std::vector<std::pair<TokenKind, std::string>> v;
for (auto& h : mp) {
v.push_back(std::make_pair(h.second, h.first));
}
std::sort(v.begin(), v.end());
int n = 0;
for (auto& h : v) {
std::cout << h.first << " <=> " << h.second << "\t";
if (!(n++ % 4)) {
std::cout << std::endl;
}
}
return 0;
}