ex00 edge cases and execute function
This commit is contained in:
@@ -63,8 +63,116 @@ void BitcoinExchange::readDatabase(std::ifstream &db)
|
|||||||
date = line.substr(0, pos);
|
date = line.substr(0, pos);
|
||||||
price = line.substr(pos + 1);
|
price = line.substr(pos + 1);
|
||||||
|
|
||||||
|
|
||||||
this->data[date] = std::strtod(price.c_str(), NULL);
|
this->data[date] = std::strtod(price.c_str(), NULL);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BitcoinExchange::execute(std::ifstream &db)
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
std::getline(db, line);
|
||||||
|
|
||||||
|
if (line != "date | value")
|
||||||
|
throw std::exception();
|
||||||
|
|
||||||
|
size_t pos;
|
||||||
|
std::string date;
|
||||||
|
std::string value;
|
||||||
|
|
||||||
|
while (std::getline(db, line))
|
||||||
|
{
|
||||||
|
pos = line.find('|');
|
||||||
|
if (pos == std::string::npos)
|
||||||
|
{
|
||||||
|
cout << "Error: bad input => " << line << endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
date = line.substr(0, pos - 1);
|
||||||
|
value = line.substr(pos + 2);
|
||||||
|
|
||||||
|
if (!validateAll(date, value))
|
||||||
|
continue ;
|
||||||
|
|
||||||
|
std::map<std::string, double>::iterator it = this->data.lower_bound(date);
|
||||||
|
if (it == this->data.begin() && it->first != date)
|
||||||
|
{
|
||||||
|
cout << "Error: bad input => " << date << endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (it->first != date)
|
||||||
|
it--;
|
||||||
|
|
||||||
|
cout << date << " => " << value << " = " << it->second * std::strtod(value.c_str(), NULL) << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BitcoinExchange::validateAll(const std::string &date, const std::string &value)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!validateDate(date))
|
||||||
|
{
|
||||||
|
cout << "Error: bad input => " << date << endl;
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
else if (validateValue(value) == 2)
|
||||||
|
{
|
||||||
|
cout << "Error: not a positive number." << endl;
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
else if (validateValue(value) == 3)
|
||||||
|
{
|
||||||
|
cout << "Error: too large a number." << endl;
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BitcoinExchange::validateDate(const std::string &date)
|
||||||
|
{
|
||||||
|
int maxDay = 31;
|
||||||
|
int year;
|
||||||
|
int month;
|
||||||
|
int day;
|
||||||
|
char dash;
|
||||||
|
bool bisiesto;
|
||||||
|
|
||||||
|
|
||||||
|
if (date.length() != 10 || date[4] != '-' || date[7] != '-')
|
||||||
|
return (false);
|
||||||
|
|
||||||
|
std::istringstream ss(date);
|
||||||
|
ss >> year >> dash >> month >> dash >> day;
|
||||||
|
bisiesto = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
|
||||||
|
|
||||||
|
if (year < 2008)
|
||||||
|
return (false);
|
||||||
|
if (month < 1 || month > 12)
|
||||||
|
return (false);
|
||||||
|
if (month == 2)
|
||||||
|
{
|
||||||
|
if (bisiesto)
|
||||||
|
maxDay = 29;
|
||||||
|
else
|
||||||
|
maxDay = 28;
|
||||||
|
}
|
||||||
|
if (day < 1 || day > maxDay)
|
||||||
|
return (false);
|
||||||
|
|
||||||
|
return (true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int BitcoinExchange::validateValue(const std::string &value)
|
||||||
|
{
|
||||||
|
char *endptr;
|
||||||
|
double val = std::strtod(value.c_str(), &endptr);
|
||||||
|
|
||||||
|
if (*endptr != '\0')
|
||||||
|
return (1);
|
||||||
|
if (val < 0)
|
||||||
|
return (2);
|
||||||
|
else if (val > 1000)
|
||||||
|
return (3);
|
||||||
|
return (0);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,10 @@ class BitcoinExchange
|
|||||||
private:
|
private:
|
||||||
std::map<std::string, double> data;
|
std::map<std::string, double> data;
|
||||||
|
|
||||||
|
bool validateAll(const std::string &date, const std::string &value);
|
||||||
|
bool validateDate(const std::string &date);
|
||||||
|
int validateValue(const std::string &value);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BitcoinExchange();
|
BitcoinExchange();
|
||||||
BitcoinExchange(const BitcoinExchange &other);
|
BitcoinExchange(const BitcoinExchange &other);
|
||||||
@@ -33,6 +37,7 @@ class BitcoinExchange
|
|||||||
~BitcoinExchange();
|
~BitcoinExchange();
|
||||||
|
|
||||||
void readDatabase(std::ifstream &db);
|
void readDatabase(std::ifstream &db);
|
||||||
|
void execute(std::ifstream &db);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Binary file not shown.
10
ex00/input.txt
Normal file
10
ex00/input.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
date | value
|
||||||
|
2011-01-03 | 3
|
||||||
|
2011-01-03 | 2
|
||||||
|
2011-01-03 | 1
|
||||||
|
2011-01-03 | 1.2
|
||||||
|
2011-01-09 | 1
|
||||||
|
2012-01-11 | -1
|
||||||
|
2001-42-42
|
||||||
|
2012-01-11 | 1
|
||||||
|
2012-01-11 | 2147483648
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/04/23 19:58:34 by aortigos #+# #+# */
|
/* Created: 2026/04/23 19:58:34 by aortigos #+# #+# */
|
||||||
/* Updated: 2026/04/23 20:48:10 by aortigos ### ########.fr */
|
/* Updated: 2026/04/24 00:32:28 by aortigos ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -21,18 +21,21 @@ int main(int argc, char **argv)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream inFile(argv[1]);
|
std::ifstream database("data.csv");
|
||||||
|
std::ifstream inputFile(argv[1]);
|
||||||
|
|
||||||
if (!inFile)
|
|
||||||
|
if (!database || !inputFile)
|
||||||
{
|
{
|
||||||
cout << "Error: could not open database." << endl;
|
cout << "Error: could not open file." << endl;
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
BitcoinExchange bc;
|
BitcoinExchange bc;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bc.readDatabase(inFile);
|
bc.readDatabase(database);
|
||||||
|
bc.execute(inputFile);
|
||||||
} catch (std::exception &e)
|
} catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cout << e.what() << std::endl;
|
std::cout << e.what() << std::endl;
|
||||||
|
|||||||
BIN
ex00/main.o
BIN
ex00/main.o
Binary file not shown.
Reference in New Issue
Block a user