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

domaci ukol 1, gitignore

parent 5981aa50
No related branches found
No related tags found
1 merge request!4stabilni verze semestralni prace, 3 cviceni, domaci ukol 1
*.exe
tasks.json
\ No newline at end of file
*.json
\ No newline at end of file
......@@ -11,18 +11,25 @@ int config_min = -99;
int config_max = 100;
int config_width = 3;
std::string config_align = "left";
int config_stretch = -1; // -1 indicates not set
int config_header = -1; // -1 indicates not set
void printConfig()
void printConfig(int width)
{
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";
std::cout << "config.width=" << width << "\n";
std::cout << "config.align=" << config_align << "\n";
if (config_stretch != -1)
std::cout << "config.stretch=" << config_stretch << "\n";
if (config_header != -1)
std::cout << "config.header=" << config_header << "\n";
std::cout << "\n";
}
bool isValidConfig()
{
if (config_min > config_max || config_width <= 0)
if (config_min > config_max || config_width <= 0 || (config_stretch != -1 && config_stretch != 0 && config_stretch != 1) || (config_header != -1 && config_header != 0 && config_header != 1))
{
std::cerr << "Invalid configuration\n";
return false;
......@@ -41,8 +48,11 @@ bool isCellWidthTooShort(const std::vector<std::vector<std::string>> &values)
int value = std::stoi(cell);
if (std::to_string(value).length() > static_cast<size_t>(config_width))
{
std::cerr << "Cell is too short\n";
return true;
if (config_stretch == -1)
{
std::cerr << "Cell is too short\n";
return true;
}
}
}
catch (const std::invalid_argument &)
......@@ -118,13 +128,44 @@ bool isOutOfRange(const std::vector<std::vector<std::string>> &values)
return false;
}
// Funkce pro zjištění maximální šířky buňky potřebné pro čísla
int calculateMaxCellWidth(const std::vector<std::vector<std::string>> &values)
{
int maxWidth = config_width;
if (config_stretch == 1)
{
for (const auto &row : values)
{
for (const auto &cell : row)
{
try
{
int value = std::stoi(cell);
int length = std::to_string(value).length();
if (length > maxWidth)
{
maxWidth = length;
}
}
catch (...)
{
// Ignoruje buňky, které nejsou čísla
}
}
}
}
return maxWidth;
}
void printTable(std::vector<std::vector<std::string>> &values)
{
if (isOutOfRange(values))
{
return;
}
printConfig();
int actualWidth = calculateMaxCellWidth(values);
printConfig(actualWidth);
size_t cols = 0;
for (const auto &row : values)
{
......@@ -140,43 +181,95 @@ void printTable(std::vector<std::vector<std::string>> &values)
}
}
// Určení skutečné šířky buňky (s ohledem na stretch)
std::string separator = "+";
for (size_t i = 0; i <= cols; i++)
if (config_header == 0)
{
separator += std::string(config_width + 2, '-') + "+";
for (size_t i = 0; i <= cols - 1; i++)
{
separator += std::string(actualWidth + 2, '-') + "+";
}
}
std::cout << separator << "\n| |";
for (size_t i = 0; i < cols; i++)
else
{
if (config_align == "right")
for (size_t i = 0; i <= cols; i++)
{
std::cout << " " << std::setw(config_width) << std::right << static_cast<char>('A' + i) << " |";
separator += std::string(actualWidth + 2, '-') + "+";
}
else
}
// Vykreslení hlavičky pouze pokud config_header != 0
if (config_header != 0)
{
std::cout << separator << "\n| " << std::setw(actualWidth) << " |";
for (size_t i = 0; i < cols; i++)
{
std::cout << " " << std::setw(config_width) << std::left << static_cast<char>('A' + i) << " |";
if (config_align == "right")
{
std::cout << " " << std::setw(actualWidth) << std::right << static_cast<char>('A' + i) << " |";
}
else
{
std::cout << " " << std::setw(actualWidth) << std::left << static_cast<char>('A' + i) << " |";
}
}
std::cout << "\n"
<< separator << "\n";
}
else
{
std::cout << separator << "\n";
}
std::cout << "\n"
<< separator << "\n";
for (size_t i = 0; i < values.size(); i++)
{
std::cout << "| " << std::setw(3) << i + 1 << " |";
// Vykreslení čísla řádku pouze pokud config_header != 0
if (config_header != 0)
{
std::cout << "| " << std::setw(actualWidth) << i + 1 << " |";
}
else
{
std::cout << "|";
}
for (auto &cell : values[i])
{
if (cell.find("SUM(") != std::string::npos)
{
cell = std::to_string(computeSum(values[i], cell));
}
if (config_align == "right")
bool isTooLong = false;
try
{
std::cout << " " << std::setw(config_width) << std::right << cell << " |";
int value = std::stoi(cell);
if (std::to_string(value).length() > static_cast<size_t>(config_width) && config_stretch == 0)
{
isTooLong = true;
}
}
catch (...)
{
// Není číslo, neřešíme
}
if (isTooLong)
{
// Vyplníme buňku znaky #
std::cout << " " << std::string(actualWidth, '#') << " |";
}
else
{
std::cout << " " << std::setw(config_width) << std::left << cell << " |";
if (config_align == "right")
{
std::cout << " " << std::setw(actualWidth) << std::right << cell << " |";
}
else
{
std::cout << " " << std::setw(actualWidth) << std::left << cell << " |";
}
}
}
std::cout << "\n"
......@@ -200,6 +293,10 @@ int main()
config_width = std::stoi(config.value);
else if (config.type == "align")
config_align = config.value;
else if (config.type == "stretch")
config_stretch = std::stoi(config.value);
else if (config.type == "header")
config_header = std::stoi(config.value);
}
}
......
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