diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..e839ef33b19f61c837c6fb11a026e9fa59fdcb43 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,57 @@ +{ + "files.associations": { + "ostream": "cpp", + "iostream": "cpp", + "sstream": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "regex": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "fstream": "cpp", + "iomanip": "cpp" + }, + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000000000000000000000000000000000000..05054c5cd812079b58e2374f06a334dd79d53898 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/du.hpp b/du.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0fb206f0b0ebe5477442454861c9088728623129 --- /dev/null +++ b/du.hpp @@ -0,0 +1,14 @@ +#include <iostream> +#include <vector> +#include <iomanip> +#include <regex> +#include <sstream> +#include <iomanip> +#include <string> +#include <limits> + +typedef struct config { + std::string value; + std::string type; + bool valid; +} config_t; diff --git a/main b/main new file mode 100755 index 0000000000000000000000000000000000000000..e31a4919dc5a54908823cefad926b3b93cd718b0 Binary files /dev/null and b/main differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..787d287368489c67ac8d007b219064e518427d3f --- /dev/null +++ b/main.cpp @@ -0,0 +1,248 @@ +#include "du.hpp" + +// using namespace std; + +int config_min = -99; +int config_max = 100; +int config_width = 3; +std::string config_align = "left"; + +std::vector<std::vector<std::string>> table; // data from the table + +bool isoutofrange() +{ + for (const auto &row : table) + { + for (const auto &cell : row) + { + try + { + int value = std::stoi(cell); + if (value < config_min || value > config_max) + { + std::cerr << "Out of range\n"; + return true; + } + } + catch (...) + { + if (cell.find("SUM(") == std::string::npos) + { + std::cerr << "Invalid input\n"; + exit(101); + } + } + } + } + return false; +} + +void errorhere(const std::string& message, int code) { + std::cerr << message << std::endl; + exit(code); +} + +int colToIndex(char c) +{ + return toupper(c) - 'A'; +} + +void printconfig() +{ + std::cout << "config.min=" << config_min << std::endl; + std::cout << "config.max=" << config_max << std::endl; + std::cout << "config.width=" << config_width << std::endl; + std::cout << "config.align=" << config_align << std::endl; + std::cout << "\n"; +} + +void dothesum() +{ + for (auto& row : table) + { + for (size_t i = 0; i < row.size(); i++) + { + if (row[i].rfind("SUM(", 0) == 0) + { + char col1 = row[i][4]; + char col2 = row[i][6]; + if (!isalpha(col1) || !isalpha(col2) || row[i][5] != ':') + errorhere("Invalid input", 101); + int start = colToIndex(col1); + int end = colToIndex(col2); + if (start > end || static_cast<size_t>(end) >= row.size()) + errorhere("Invalid input", 101); + int sum = 0; + for (int j = start; j <= end; j++) + { + try + { + sum += stoi(row[j]); + } + catch (...) + { + errorhere("Invalid input", 101); + } + } + row[i] = std::to_string(sum); + } + } + } +} + +void printthedamntable() +{ + size_t maxcols = 0; + for (const auto& row : table) + { + if (row.size() > maxcols) + maxcols = row.size(); + } + std::string border = "+-" + std::string(config_width, '-') + "-"; + for (size_t i = 0; i < maxcols; i++) + border += "+-" + std::string(config_width, '-') + "-"; + border += "+"; + std::cout << border << std::endl; + std::cout << "| " << std::string(config_width, '-') << " |"; + for (size_t i = 0; i < maxcols; i++) + std::cout << " " << std::setw(config_width) << (config_align == "left" ? std::left : std::right) << static_cast<char>('A' + i) << " |"; + std::cout << std::endl << border << std::endl; + for (size_t i = 0; i < table.size(); i++) + { + std::cout << "| " << std::setw(config_width) << (config_align == "left" ? std::left : std::right) << i + 1 << " |"; + for (size_t j = 0; j < maxcols; j++) + std::cout << " " << std::setw(config_width) << (config_align == "left" ? std::left : std::right) << (j < table[i].size() ? table[i][j] : "") << " |"; + std::cout << std::endl << border << std::endl; + } +} + +config_t configureit(std::string init) +{ + config_t config; + std::regex regexconfig ("(\\w+).(\\w+)=([\\w-]+)"); + std::smatch match; + config.valid = false; + + if (std::regex_search(init, match, regexconfig)) + { + // std::cout << match[0] << '\n' << match[1]; + if (match[1].compare("config") == 0) + { + config.type = match[2]; + config.value = match[3]; + config.valid = true; + } + else + { + config.type = "not configed"; + } + } + return config; +} + + +// int getlongest(std::string s) +// { +// int longest = 0; +// int i = 0; +// int length = 1; + +// while (i < s.size()) +// { +// std::cout << s[i] << std::endl; +// if (s[i] == ';') +// { +// length++; +// } +// if (s[i] == '\n') +// { +// if (length > longest) +// { +// longest = length; +// } +// length = 1; +// } +// i++; +// } +// // cout << longest; +// return (longest); +// } + +void draw_line(int columns, int width) +{ + std::cout << '+'; + for (int i = 0; i < columns; i++) + { + for (int j = 0; j < width + 2; j++) + { + std::cout << '-'; + } + std::cout << '+'; + } + std::cout << '\n'; +} + +int main() +{ + std::string init, svalue, cell; + int longest; + + while(std::getline(std::cin, init) && init != "=") + { + config_t config = configureit(init); + if (config.valid) + { + if (config.type == "min") + config_min = std::stoi(config.value); + else if (config.type == "max") + config_max = std::stoi(config.value); + else if (config.type == "width") + { + config_width = std::stoi(config.value); + if (config_width < 1) + errorhere("Invalid configuration", 102); + } + else if (config.type == "align") + config_align = config.value; + } + else + errorhere("Invalid configuration", 102); + } + // std::vector<std::vector<std::string>> values; + while (getline(std::cin, svalue)) + { + std::vector<std::string> row; + size_t end; + int start = 0; + bool endswithsemicolon = init.back() == ';'; + while ((end = svalue.find(';', start)) != std::string::npos) + { + cell = svalue.substr(start, end - start); + if (static_cast<int>(cell.length()) > config_width) + errorhere("Cell is too short", 100); + start = 1 + end; + row.push_back(svalue.empty() ? " " : cell); + } + row.push_back(svalue.substr(start)); // to add the last element + if (endswithsemicolon) + row.push_back(" "); + table.push_back(row); + } + if (isoutofrange()) + return 100; + printconfig(); + dothesum(); + printthedamntable(); +} + +// // -40;-50;-60;-70 +// // 90;80;-20;-30 + + +/*config.min=-99 +config.max=150 +config.width=3 +config.align=left += +10;20;20;SUM(A:C) +-10;-20;-30;-40;-50*/ \ No newline at end of file