test
This commit is contained in:
parent
84e23db429
commit
0b56ac5cea
Binary file not shown.
|
|
@ -16,3 +16,110 @@ BitcoinExchange &BitcoinExchange::operator=(const BitcoinExchange &other)
|
|||
this->_data = other._data;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
bool BitcoinExchange::isValidDate(const std::string &date) const
|
||||
{
|
||||
if (date.length() != 10)
|
||||
return (false);
|
||||
if (date[4] != '-' || date[7] != '-')
|
||||
return (false);
|
||||
int year = std::atoi(date.substr(0, 4).c_str());
|
||||
int month = std::atoi(date.substr(5, 2).c_str());
|
||||
int day = std::atoi(date.substr(8, 2).c_str());
|
||||
if (month < 1 || month > 12 || day < 1)
|
||||
return false;
|
||||
if (month != 2 && day > (30 + (month + month / 8) % 2))
|
||||
return false;
|
||||
if (month == 2)
|
||||
{
|
||||
if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
|
||||
if (day > 29)
|
||||
return false;
|
||||
} else if (day > 28)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void BitcoinExchange::loadDatabase(const std::string &filename)
|
||||
{
|
||||
std::ifstream file(filename.c_str());
|
||||
|
||||
if (!file.is_open())
|
||||
return;
|
||||
|
||||
std::string line;
|
||||
float value = 0.0;
|
||||
std::getline(file, line); //skip header
|
||||
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
value = std::atof(line.substr(12).c_str());
|
||||
if (isValidDate(line.substr(0, 10)) && value >= 0)
|
||||
_data[line.substr(0, 10)] = value;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
float BitcoinExchange::getRate(const std::string &date) const
|
||||
{
|
||||
std::map<std::string, float>::const_iterator it = _data.upper_bound(date);
|
||||
if (it == _data.begin())
|
||||
return (-1); //not found
|
||||
--it;
|
||||
return (it->second);
|
||||
}
|
||||
|
||||
bool BitcoinExchange::is_valide_input(const std::string &line, float &value)
|
||||
{
|
||||
if (line.length() < 14 || line.find('|') == std::string::npos) {
|
||||
std::cerr << "Error: bad input => " << line << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!isValidDate(line.substr(0, 10))) {
|
||||
std::cerr << "Error: bad input => " << line << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::string valStr = line.substr(13);
|
||||
char *end;
|
||||
value = std::strtof(valStr.c_str(), &end);
|
||||
if (*end != '\0')
|
||||
{
|
||||
std::cerr << "Error: bad input => " << line << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (value > 1000) {
|
||||
std::cerr << "Error: too large a number." << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (value < 0) {
|
||||
std::cerr << "Error: not a positive number." << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void BitcoinExchange::processInput(const std::string &filename)
|
||||
{
|
||||
std::ifstream file(filename.c_str());
|
||||
|
||||
if (!file.is_open())
|
||||
return;
|
||||
|
||||
float value = 0.0;
|
||||
std::string line;
|
||||
std::getline(file, line); // skip header
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
if (!is_valide_input(line, value))
|
||||
continue;
|
||||
std::string date = line.substr(0, 10);
|
||||
float rate = getRate(date);
|
||||
if (rate < 0) {
|
||||
std::cerr << "Error: bad input => " << date << std::endl;
|
||||
continue;
|
||||
}
|
||||
std::cout << date << " => " << value << " = " << value * rate << std::endl;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
#ifndef BITCOINEXCHANGE_HPP
|
||||
#define BITCOINEXCHANGE_HPP
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
|
@ -16,7 +19,7 @@ class BitcoinExchange
|
|||
void processInput(const std::string &filename);
|
||||
float getRate(const std::string &date) const;
|
||||
bool isValidDate(const std::string &date) const;
|
||||
bool isValidValue(const std::string &value, float &out) const;
|
||||
bool is_valide_input(const std::string &line, float &value);
|
||||
|
||||
private:
|
||||
std::map<std::string, float> _data;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
#include "BitcoinExchange.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
std::cerr << "Error: could not open file." << std::endl;
|
||||
return (1);
|
||||
}
|
||||
BitcoinExchange btc;
|
||||
btc.loadDatabase("data.csv");
|
||||
btc.processInput(argv[1]);
|
||||
std::cout << btc.isValidDate("2000-02-29") << std::endl;
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
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
|
||||
2009-01-02 | 1
|
||||
2009-01-01 | 1
|
||||
2022-03-29 | 1000
|
||||
2022-03-29 | 1000.01
|
||||
2022-03-29 | 0
|
||||
2022-03-29 | -0.01
|
||||
2022-03-29 | 999.99
|
||||
2015-06-15 | 0.5
|
||||
2013-12-25 | 100
|
||||
2010-03-01 | 10
|
||||
abc | 1
|
||||
2011-01-03 | abc
|
||||
2011-01-03 |
|
||||
| 1
|
||||
2011-13-01 | 1
|
||||
2011-00-01 | 1
|
||||
2011-01-32 | 1
|
||||
2011-01-00 | 1
|
||||
2011-02-29 | 1
|
||||
2012-02-29 | 1
|
||||
2012-02-30 | 1
|
||||
2011-04-31 | 1
|
||||
2011-06-31 | 1
|
||||
2011-09-31 | 1
|
||||
2011-11-31 | 1
|
||||
2016-02-29 | 1
|
||||
2100-02-29 | 1
|
||||
2000-02-29 | 1
|
||||
1900-02-29 | 1
|
||||
2011-01-03 | -0
|
||||
2011-01-03 | 0.0
|
||||
2011-01-03 | 0.001
|
||||
2022-03-29 | 999
|
||||
hello world
|
||||
2011-01-03|1
|
||||
2011/01/03 | 1
|
||||
01-01-2011 | 1
|
||||
2011-1-3 | 1
|
||||
2011-01-03 | 1.1.1
|
||||
2011-01-03 | 1e2
|
||||
2011-01-03 | +1
|
||||
2008-12-31 | 1
|
||||
2025-12-31 | 1
|
||||
2009-01-02 | 0.001
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
int days = 30 + (month + month / 8) % 2;
|
||||
|
||||
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,2 @@
|
|||
dsd
|
||||
2000-02-29 | 1
|
||||
Loading…
Reference in New Issue