Skip to content
Snippets Groups Projects
Commit 6ac72b42 authored by Husam-Ahmad's avatar Husam-Ahmad
Browse files

removing to look better

parent e0016186
No related branches found
No related tags found
No related merge requests found
#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;
File deleted
#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
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