/
xverizex
/
byte-calc
Обзор
Документация
Войти
/
xverizex
/
byte-calc
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
calc.cpp
652 строки
21 KB
Dmitry
init
11 июл 2026, 17:38
11 июл 2026, 17:38
7a2ad86
Код
Авторство
О чём код?
#include <iostream> #include <filesystem> #include <fstream> #include <vector> void calc (int *a, int ia, char sym, int& sum, int sym_pos) { if (ia == 2) { switch (sym) { case '*': sum += a[0] * a[1]; break; case '+': sum += a[0] + a[1]; break; case '-': sum += a[0] - a[1]; break; case '/': sum += a[0] / a[1]; default: return; } } if (ia == 1) { switch (sym) { case '*': if (sym_pos == 1) { sum = a[0] * sum; } else { sum *= a[0]; } break; case '+': if (sym_pos == 1) { sum = a[0] + sum; } else { sum += a[0]; } break; case '-': if (sym_pos == 1) { sum = a[0] - sum; } else { sum -= a[0]; } break; case '/': if (sym_pos == 1) { sum = a[0] / sum; } else { sum /= a[0]; } break; default: return; } } return; } int is_exp (char sym) { if ((sym == '*') || (sym == '+') || (sym == '-') || (sym == '/')) return 1; return 0; } int is_add (char sym) { if ((sym == '+') || (sym == '-')) return 1; return 0; } int is_mul (char sym) { if ((sym == '*') || (sym == '/')) return 1; return 0; } void parse_calc (std::string& arr, int& indx, int& sum, bool debug) { if (arr.size () > 0) { int a[2]; int ia = 0; int sym_pos = 0; char math_ = 0; bool is_first = true; bool is_new_math = false; int sz = arr.size (); int pos = 0; if ((indx == 0) && (arr[indx] != '(')) { indx = 1; } if (debug) { std::cout << "\tindx: " << indx << std::endl; } if (debug) { std::cout << "\tsum: " << sum << std::endl; } for (int k = indx; k < sz; k += 2) { sym_pos = 0; if (arr[k] == '(') { if (debug) { std::cout << "#0: " << std::endl; } int tmp = 0; int tmpk = k; k += 2; parse_calc (arr, k, tmp, debug); sum += tmp; k -= 2; } else if (arr[k] == ')') { if (debug) { std::cout << "#< " << std::endl; } indx = k + 1; return; } else if (((k + 1) < sz) && (arr[k + 1] == '(')) { if (debug) { std:: cout << "#1: " << std::endl; } int tmp = 0; int tmpk = k; k++; k += 2; parse_calc (arr, k, tmp, debug); int max = k; if (debug) { std::cout << "tmp: " << tmp << "; k: " << k << std::endl; } ia = 0; if ((arr[k] == '*') || (arr[k] == '/')) { if (debug) { std::cout << " ##0: " << std::endl; } if (arr[k + 1] == '(') { if (debug) { std::cout << " ##1: " << std::endl; } ia = 0; #if 0 { a[ia++] = sum; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); if (debug) { std::cout << "\t{sum: " << sum << "; tmp: " << tmp << "}" << std::endl; } #endif int f = k; k++; k += 2; int tmpp = 0; parse_calc (arr, k, tmpp, debug); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << "; tmpp: " << tmpp << std::endl; } max = k; k = f; ia = 0; #if 0 a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } calc (a, ia, math_, sum, sym_pos); #else a[ia++] = tmpp; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } calc (a, ia, math_, tmp, sym_pos); sum = tmp; #endif k += 2; if (debug) { std::cout << "\tsum 0: " << sum << std::endl; } k = max; } else { a[ia++] = arr[k + 1] - '0'; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } calc (a, ia, math_, tmp, sym_pos); k += 2; } if (debug) { std::cout << "\ttmp: " << tmp << std::endl; } } while ((arr[k] == '*') || (arr[k] == '/')) { if (debug) { std::cout << " ##1: " << std::endl; } ia = 0; a[ia++] = arr[k + 1] - '0'; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } char sym = arr[k]; math_ = sym; calc (a, ia, math_, tmp, sym_pos); k += 2; if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << std::endl; } sum = tmp; } max = k; k = tmpk; if (debug) { std::cout << "\ttmpk: " << tmpk << std::endl; } ia = 0; if (pos == 0) { if (debug) { std::cout << "\tpos == 0" << std::endl; } a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; sym_pos = 1; if (debug) { std::cout << "\ta[0]: " << a[0] << "; tmp: " << tmp << std::endl; } calc (a, ia, math_, tmp, sym_pos); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << std::endl; } sum = tmp; } else { if (debug) { std::cout << "\tpos == 1" << std::endl; } a[ia++] = sum; if (debug) { std::cout << "\ta[0]: " << a[0] << "; tmp: " << tmp << std::endl; } char sym = arr[k]; math_ = sym; sym_pos = 1; calc (a, ia, math_, tmp, sym_pos); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << std::endl; } sum = tmp; } k = max - 2; pos = 1; } else if ((pos == 0) && ((k + 2) < sz) && (((arr[k] == '+') || (arr[k] == '-')) && ((arr[k + 2] == '*') || (arr[k + 2] == '/')))) { if (debug) { std::cout << "#2: " << std::endl; } pos = 1; int tmp = sum; ia = 0; sum = 0; int tmpk = k; int max = tmpk; k += 2; if ((arr[k] == '*') || (arr[k] == '/')) { ia = 0; if (arr[k + 1] == '(') { int tmp = 0; int f = k; k++; k += 2; parse_calc (arr, k, tmp, debug); sum += tmp; max = k; k = f; ia = 0; a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k += 2; k = max; } else { ia = 0; a[ia++] = arr[k - 1] - '0'; a[ia++] = arr[k + 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k += 2; } } while ((arr[k] == '*') || (arr[k] == '/')) { if (arr[k + 1] == '(') { int tmp = 0; int f = k; k++; k += 2; parse_calc (arr, k, tmp, debug); sum += tmp; max = k; k = f; ia = 0; a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k += 2; k = max; } else { ia = 0; a[ia++] = arr[k + 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k += 2; } } k -= 2; int indxk = k; k = tmpk; ia = 0; a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k = indxk; } else if (((k + 2) < sz) && (((arr[k] == '+') || (arr[k] == '-')) && ((arr[k + 2] == '*') || (arr[k + 2] == '/')))) { if (debug) { std::cout << "#3: " << std::endl; } { pos = 1; ia = 0; int tmpk = k; int max = tmpk; k += 2; int tmp = 0; if ((arr[k] == '*') || (arr[k] == '/')) { if (debug) { std::cout << " ##0: " << std::endl; } ia = 0; if (arr[k + 1] == '(') { if (debug) { std::cout << " ##1: " << std::endl; } int f = k; k++; k += 2; parse_calc (arr, k, tmp, debug); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << std::endl; } sum += tmp; if (debug) { std::cout << "\tsumr: " << sum << std::endl; } max = k; k = f; ia = 0; a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } calc (a, ia, math_, sum, sym_pos); k += 2; if (debug) { std::cout << "\tsum 0: " << sum << std::endl; } k = max; } else { if (debug) { std::cout << " ##2: " << std::endl; } ia = 0; a[ia++] = arr[k - 1] - '0'; a[ia++] = arr[k + 1] - '0'; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << "; a[1]: " << a[1] << "; sum: " << sum << std::endl; } calc (a, ia, math_, tmp, sym_pos); k += 2; if (debug) { std::cout << "\tsum 1: " << sum << "; tmp: " << tmp << std::endl; } } } while ((arr[k] == '*') || (arr[k] == '/')) { if (debug) { std::cout << " ##3: " << std::endl; } if (arr[k + 1] == '(') { if (debug) { std::cout << " ##4: " << std::endl; } int tmp0 = 0; int f = k; k++; k += 2; parse_calc (arr, k, tmp0, debug); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << "; tmp0: " << tmp0 << std::endl; } //sum += tmp; if (debug) { std::cout << "\tsum 2: " << sum << std::endl; } max = k; k = f; ia = 0; a[ia++] = tmp; char sym = arr[k]; math_ = sym; sym_pos = 1; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } calc (a, ia, math_, tmp0, sym_pos); k += 2; k = max; if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << "; tmp0: " << tmp0 << std::endl; } tmp = tmp0; if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << "; k: " << k << std::endl; } } else { if (debug) { std::cout << " ##5: " << std::endl; } ia = 0; a[ia++] = arr[k + 1] - '0'; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } calc (a, ia, math_, tmp, sym_pos); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << std::endl; } k += 2; } } k -= 2; if (debug) { std::cout << " ##6: " << std::endl; } int indxk = k; k = tmpk; if (debug) { std::cout << "\ttmpk: " << tmpk << std::endl; } ia = 0; a[ia++] = sum; char sym = arr[k]; math_ = sym; if (debug) { std::cout << "\ta[0]: " << a[0] << std::endl; } if (debug) { std::cout << "\ttmp: " << tmp << std::endl; } sym_pos = 1; calc (a, ia, math_, tmp, sym_pos); if (debug) { std::cout << "\tsum: " << sum << "; tmp: " << tmp << std::endl; } k = indxk; sum = tmp; if (debug) { std::cout << "\tsum: " << sum << std::endl; } } } else if ((pos == 0) && (is_exp (arr[k]) && (is_exp (arr[k + 1])))) { if (debug) { std::cout << "#4: " << std::endl; } pos = 1; if (is_add (arr[k]) && is_mul (arr[k + 2])) { int tmp = sum; ia = 0; int tmpk = k; k += 2; while ((arr[k] == '*') || (arr[k] == '/')) { ia = 0; a[ia++] = arr[k + 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k += 2; } k -= 2; //sum += tmp; int indxk = k; k = tmpk; ia = 0; a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; sym_pos = 1; calc (a, ia, math_, sum, sym_pos); k = indxk; } else { ia = 0; a[ia++] = arr[k - 1] - '0'; char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); k -= 1; } } else if (pos == 0) { if (debug) { std::cout << "#5: " << std::endl; } pos = 1; ia = 0; a[ia++] = arr[k - 1] - '0'; a[ia++] = arr[k + 1] - '0'; if (debug) { std::cout << "\t" << a[0] << std::endl; std::cout << "\t" << a[1] << std::endl; } char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); if (debug) { std::cout << "\t" << "sum: " << sum << "; k: " << k << std::endl; } } else { if (debug) { std::cout << "#6: " << std::endl; } ia = 0; a[ia++] = arr[k + 1] - '0'; if (debug) { std::cout << "\ta[0]: " << a[0] << "; arr[k + 1]: " << arr[k + 1] << std::endl; } char sym = arr[k]; math_ = sym; calc (a, ia, math_, sum, sym_pos); if (debug) { std::cout << "\tsum: " << sum << "; k: " << k << std::endl; } } pos = 1; //std::cout << sum; } //calc (a, ia, math_, sum, sym_pos); //std::cout << sum; } } int parse_equation (std::string& buf, bool debug) { if (debug) { std::cout << buf << std::endl; } else { std::cout << buf; } int sum = 0; int indx = 0; parse_calc (buf, indx, sum, debug); return sum; }