Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models / Table.cpp
diff options
context:
space:
mode:
Commit5f793307d00b33b1a424206da14b6492b2690f9e (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-05-30 19:43:07 +0300
Committer
 Syndamia <kamen@syndamia.com>
2022-05-30 19:43:07 +0300
Files35d5abbb1d7a3651a0f1a605f9b2b6ff4cf3ec87 /src/Models/Table.cpp
FMI-OOP-P2_Tables-5f793307d00b33b1a424206da14b6492b2690f9e.tar
FMI-OOP-P2_Tables-5f793307d00b33b1a424206da14b6492b2690f9e.tar.gz
FMI-OOP-P2_Tables-5f793307d00b33b1a424206da14b6492b2690f9e.zip
Parent7c0853dafc26855309487b713b26fc06d9601035 (diff)
Autocommit at 30.05.2022 @ 19:43
1 files changed, 22 insertions, 15 deletions
-rw-r--r--src/Models/Table.cpp37
diff --git a/src/Models/Table.cpp b/src/Models/Table.cpp
index afbccff..e8f954b 100644
--- a/src/Models/Table.cpp
+++ b/src/Models/Table.cpp
@@ -8,6 +8,8 @@
#include <exception>
#include <stdexcept>
+#define FLOATING_POINT_PRECISION 0.00001f
+
unsigned countOfCommas(std::ifstream& file) {
unsigned commaCount = 0;
while (file.peek() != '\n') {
@@ -26,19 +28,6 @@ bool containsNumber(const char*& str) {
(*str >= '0' && *str <= '9'); // + or -, followed by digit, or it just a digit
}
-Cell* Table::createCellByParsing(const char* rawValue) {
- if (containsNumber(rawValue)) {
- double doubleParse = atof(rawValue);
- unsigned intPart = abs(doubleParse);
-
- return (abs(doubleParse) - intPart > 0.00001)
- ? (Cell*)new CellDouble(doubleParse) : (Cell*)new CellInt(intPart);
- }
-
- return (*rawValue == '=')
- ? (Cell*)new CellFormula(rawValue, &cells) : (Cell*)new CellString(rawValue);
-}
-
void Table::readFromFile(std::ifstream& inFile) {
unsigned commaCount = countOfCommas(inFile);
unsigned colInd = 0;
@@ -56,7 +45,7 @@ void Table::readFromFile(std::ifstream& inFile) {
inFile.get();
}
else if (inFile.peek() == '-' || inFile.peek() == '+' || isDigit(inFile.peek())) {
- unsigned numberStart = inFile.tellg();
+ double number = 0;
if (inFile.peek() == '-' || inFile.peek() == '+') inFile.get();
if (isDigit(inFile.peek()))
@@ -67,6 +56,11 @@ void Table::readFromFile(std::ifstream& inFile) {
while (isDigit(inFile.peek())) inFile.get();
}
if (inFile.peek() != ' ' && inFile.peek() != ',' && inFile.peek() != '\n')
+ throw std::logic_error(fileLocationExceptionMsg("Error: Expected space, comma or newline but got something else at row ", cells.get_count(), inFile.tellg() % cells.get_count() + 1));
+ unsigned numberEnd = inFile.tellg();
+
+ inFile.seekg(numberStart, std::ios::beg);
+ cells[cells.get_count() - 1].add();
}
}
}
@@ -94,7 +88,20 @@ unsigned Table::get_cols() const {
}
void Table::putCell(unsigned row, unsigned col, const char* rawValue) {
- Cell* newCell = createCellByParsing(rawValue);
+ Cell* newCell;
+
+ if (containsNumber(rawValue)) {
+ double doubleParse = atof(rawValue);
+ unsigned intPart = abs(doubleParse);
+
+ newCell = (abs(doubleParse) - intPart > FLOATING_POINT_PRECISION)
+ ? (Cell*)new CellDouble(doubleParse) : (Cell*)new CellInt(intPart);
+ }
+ else {
+ newCell = (*rawValue == '=')
+ ? (Cell*)new CellFormula(rawValue, &cells) : (Cell*)new CellString(rawValue);
+ }
+
if (cells[row][col] != nullptr)
delete cells[row][col];