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

funkcni prvni cast domaciho ukolu (3b)

parent 3b4415f8
No related branches found
No related tags found
1 merge request!4stabilni verze semestralni prace, 3 cviceni, domaci ukol 1
......@@ -8,7 +8,7 @@
// Výchozí konfigurace
int config_min = -99;
int config_max = 150;
int config_max = 100;
int config_width = 3;
std::string config_align = "left";
......@@ -20,16 +20,59 @@ void printConfig()
std::cout << "config.align=" << config_align << "\n\n";
}
bool isValidConfig()
{
if (config_min > config_max || config_width <= 0)
{
std::cerr << "Invalid configuration\n";
return false;
}
return true;
}
bool isCellWidthTooShort(const std::vector<std::vector<std::string>> &values)
{
for (const auto &row : values)
{
for (const auto &cell : row)
{
try
{
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;
}
}
catch (const std::invalid_argument &)
{
// Ignore non-integer cells
}
catch (const std::out_of_range &)
{
// Ignore non-integer cells
}
}
}
return false;
}
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';
size_t start = match[1].str()[0] - 'A';
size_t end = match[2].str()[0] - 'A';
if (end >= row.size())
{
std::cerr << "Invalid input\n";
exit(101);
}
int sum = 0;
for (int i = start; i <= end && i < row.size(); ++i)
for (size_t i = start; i <= end && i < row.size(); ++i)
{
try
{
......@@ -37,17 +80,52 @@ int computeSum(const std::vector<std::string> &row, const std::string &sumExpr)
}
catch (...)
{
return 0;
std::cerr << "Invalid input\n";
exit(101);
}
}
return sum;
}
return 0;
std::cerr << "Invalid input\n";
exit(101);
}
bool isOutOfRange(const std::vector<std::vector<std::string>> &values)
{
for (const auto &row : values)
{
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 printTable(std::vector<std::vector<std::string>> &values)
{
int cols = 0;
if (isOutOfRange(values))
{
return;
}
printConfig();
size_t cols = 0;
for (const auto &row : values)
{
if (row.size() > cols)
......@@ -63,15 +141,22 @@ void printTable(std::vector<std::vector<std::string>> &values)
}
std::string separator = "+";
for (int i = 0; i <= cols; i++)
for (size_t i = 0; i <= cols; i++)
{
separator += std::string(config_width + 2, '-') + "+";
}
std::cout << separator << "\n| |";
for (char c = 'A'; c < 'A' + cols; c++)
for (size_t i = 0; i < cols; i++)
{
std::cout << " " << std::setw(config_width) << std::left << c << " |";
if (config_align == "right")
{
std::cout << " " << std::setw(config_width) << std::right << static_cast<char>('A' + i) << " |";
}
else
{
std::cout << " " << std::setw(config_width) << std::left << static_cast<char>('A' + i) << " |";
}
}
std::cout << "\n"
<< separator << "\n";
......@@ -85,7 +170,14 @@ void printTable(std::vector<std::vector<std::string>> &values)
{
cell = std::to_string(computeSum(values[i], cell));
}
std::cout << " " << std::setw(config_width) << std::left << cell << " |";
if (config_align == "right")
{
std::cout << " " << std::setw(config_width) << std::right << cell << " |";
}
else
{
std::cout << " " << std::setw(config_width) << std::left << cell << " |";
}
}
std::cout << "\n"
<< separator << "\n";
......@@ -111,7 +203,10 @@ int main()
}
}
printConfig();
if (!isValidConfig())
{
return 102;
}
std::vector<std::vector<std::string>> values;
while (std::getline(std::cin, line))
......@@ -131,6 +226,16 @@ int main()
values.push_back(row);
}
if (isOutOfRange(values))
{
return 100;
}
if (isCellWidthTooShort(values))
{
return 103;
}
printTable(values);
return 0;
}
}
\ 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