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
{