ex00 finished

This commit is contained in:
2026-04-24 12:36:15 +02:00
parent c28623442e
commit 50cfaa5fcc
5 changed files with 48 additions and 1628 deletions

View File

@@ -11,6 +11,8 @@
/* ************************************************************************** */
#include "BitcoinExchange.hpp"
using std::cout;
using std::endl;
//////////////////
// Constructors //
@@ -87,8 +89,13 @@ void BitcoinExchange::execute(std::ifstream &db)
cout << "Error: bad input => " << line << endl;
continue;
}
date = line.substr(0, pos - 1);
value = line.substr(pos + 2);
date = line.substr(0, pos);
value = line.substr(pos + 1);
while (date.size() && date[date.size() - 1] == ' ')
date.erase(date.size() - 1);
while (value.size() && value[0] == ' ')
value.erase(0, 1);
if (!validateAll(date, value))
continue ;
@@ -108,18 +115,25 @@ void BitcoinExchange::execute(std::ifstream &db)
bool BitcoinExchange::validateAll(const std::string &date, const std::string &value)
{
int res;
res = validateValue(value);
if (!validateDate(date))
{
cout << "Error: bad input => " << date << endl;
return (false);
}
else if (validateValue(value) == 2)
else if (res == 1)
{
cout << "Error: not a number." << endl;
return (false);
}
else if (res == 2)
{
cout << "Error: not a positive number." << endl;
return (false);
}
else if (validateValue(value) == 3)
else if (res == 3)
{
cout << "Error: too large a number." << endl;
return (false);
@@ -133,22 +147,24 @@ bool BitcoinExchange::validateDate(const std::string &date)
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;
year = std::atoi(date.substr(0, 4).c_str());
month = std::atoi(date.substr(5, 2).c_str());
day = std::atoi(date.substr(8, 2).c_str());
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 (month == 4 || month == 6 || month == 9 || month == 11)
maxDay = 30;
else if (month == 2)
{
if (bisiesto)
maxDay = 29;

View File

@@ -17,9 +17,7 @@
# include <fstream>
# include <map>
# include <cstdlib>
using std::cout;
using std::endl;
# include <sstream>
class BitcoinExchange
{

File diff suppressed because it is too large Load Diff

View File

@@ -7,4 +7,20 @@ date | value
2012-01-11 | -1
2001-42-42
2012-01-11 | 1
2012-01-11 | 2147483648
2012-01-11 | 2147483648
2011-04-31 | 5
2011-06-31 | 3
2009-02-29 | 1
2008-02-29 | 1
2010-13-01 | 1
2010-00-01 | 1
2007-01-01 | 1
2011-01-00 | 1
2011-01-32 | 1
2011-11-30 | 10
2011-11-31 | 10
somegarbage
2012-01-11 | 0
2012-01-11 | 1000
2012-01-11 | 1000.1
2012-01-11 | abc

View File

@@ -6,19 +6,22 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/23 19:58:34 by aortigos #+# #+# */
/* Updated: 2026/04/24 00:32:28 by aortigos ### ########.fr */
/* Updated: 2026/04/24 12:15:14 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "BitcoinExchange/BitcoinExchange.hpp"
using std::cout;
using std::endl;
int main(int argc, char **argv)
{
if (argc != 2)
{
cout << "Usage: ./btc <database>" << endl;
return (0);
cout << "Error: could not open file." << endl;
return (1);
}
std::ifstream database("data.csv");