Skip to content
Snippets Groups Projects
Commit 0eacf4fa authored by Vojtěch Novotný's avatar Vojtěch Novotný
Browse files

prvni pokus na du1

parent c8b7904f
No related branches found
No related tags found
1 merge request!2DU
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include "main.hpp"
#include "parse.hpp"
#include <regex>
// Výchozí konfigurace
int config_min = -99;
int config_max = 150;
int config_width = 3;
std::string config_align = "left";
void printConfig()
{
std::cout << "config.min=" << config_min << "\n";
std::cout << "config.max=" << config_max << "\n";
std::cout << "config.width=" << config_width << "\n";
std::cout << "config.align=" << config_align << "\n\n";
}
int computeSum(const std::vector<std::string> &row, const std::string &sumExpr)
{
std::regex sumRegex(R"(SUM\(([A-Z]):([A-Z])\))");
std::smatch match;
if (std::regex_match(sumExpr, match, sumRegex))
{
int start = match[1].str()[0] - 'A';
int end = match[2].str()[0] - 'A';
int sum = 0;
for (int i = start; i <= end && i < row.size(); ++i)
{
try
{
sum += std::stoi(row[i]);
}
catch (...)
{
return 0;
}
}
return sum;
}
return 0;
}
void printTable(std::vector<std::vector<std::string>> &values)
{
int cols = 0;
for (const auto &row : values)
{
if (row.size() > cols)
cols = row.size();
}
for (auto &row : values)
{
while (row.size() < cols)
{
row.push_back(" ");
}
}
std::string separator = "+";
for (int i = 0; i <= cols; i++)
{
separator += std::string(config_width + 2, '-') + "+";
}
std::cout << separator << "\n| |";
for (char c = 'A'; c < 'A' + cols; c++)
{
std::cout << " " << std::setw(config_width) << std::left << c << " |";
}
std::cout << "\n"
<< separator << "\n";
for (size_t i = 0; i < values.size(); i++)
{
std::cout << "| " << std::setw(3) << i + 1 << " |";
for (auto &cell : values[i])
{
if (cell.find("SUM(") != std::string::npos)
{
cell = std::to_string(computeSum(values[i], cell));
}
std::cout << " " << std::setw(config_width) << std::left << cell << " |";
}
std::cout << "\n"
<< separator << "\n";
}
}
int main()
{
std::string line;
while (std::getline(std::cin, line) && line != "=")
{
config_t config = getConfig(line);
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);
else if (config.type == "align")
config_align = config.value;
}
}
printConfig();
std::vector<std::vector<std::string>> values;
while (std::getline(std::cin, line))
{
std::vector<std::string> row;
size_t start = 0, end;
bool endsWithSemicolon = line.back() == ';';
while ((end = line.find(';', start)) != std::string::npos)
{
std::string cell = line.substr(start, end - start);
start = end + 1;
row.push_back(cell.empty() ? " " : cell);
}
row.push_back(line.substr(start)); // Přidání posledního prvku
if (endsWithSemicolon)
row.push_back(" "); // Pokud řádek končí ;, přidáme prázdnou buňku
values.push_back(row);
}
printTable(values);
return 0;
}
#ifndef MAIN_HPP
#define MAIN_HPP
#endif
\ No newline at end of file
#include <string>
#include <regex>
#include <vector>
#include "parse.hpp"
config_t getConfig(std::string text)
{
config_t config;
std::regex regexConfig("(\\w+).(\\w+)=([\\w-]+)");
std::smatch fn_match;
config.valid = false;
if (std::regex_search(text, fn_match, regexConfig))
{
if (fn_match[1].compare("config") == 0)
{
config.type = fn_match[2];
config.value = fn_match[3];
config.valid = true;
}
else
{
config.type = "not config";
}
}
return config;
}
sum_t getSum(std::string text, std::vector<int> numbers)
{
sum_t sum;
std::regex regexConfig("(\\w+)\\((\\w):(\\w)\\)");
std::smatch fn_match;
sum.valid = false;
sum.value = 0;
if (std::regex_search(text, fn_match, regexConfig))
{
if (fn_match[1].compare("SUM") == 0)
{
std::string temp = fn_match[2];
int from = temp[0] - 'A';
temp = fn_match[3];
int to = temp[0] - 'A';
if (to < (int)numbers.size())
{
for (int i = from; i <= to; i++)
{
sum.value += numbers[i];
}
sum.valid = true;
sum.width = std::to_string(sum.value).size();
}
else
{
sum.valid = false;
}
}
}
return sum;
}
\ No newline at end of file
#ifndef _PARSE_HPP_
#define _PARSE_HPP_
#include <string>
#include <vector>
/**
* Header file for parsing inputs and printing out the header and lines
*/
/**
* @brief Configuration struct
*/
typedef struct
{
std::string type; /**< Type of the config as string */
std::string value; /**< Value after '=' char */
bool valid; /**< true if this configuration is valid */
} config_t;
/**
* @brief Sum struct
*/
typedef struct
{
int value; /**< Sum of the cells */
bool valid; /**< true if this sum is valid */
int width; /**< Width of the text */
} sum_t;
/**
* @brief Parse input string to parse input configuration
*
* @param text Input string for parsing (one line)
* @retval Configuration struct (config_t) with parsed values
*
*/
config_t getConfig(std::string text);
/**
* @brief Parse sum cell and return value and its validity
*
* @param text Text for parsing in format SUM(FROM:TO)
* @param numbers Vector of numbers which can be summed
* @retval sum_t struct with the value, text width and valid information
*
*/
sum_t getSum(std::string text, std::vector<int> numbers);
#endif // __PARSE_HPP_
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment