feat(09): ex00 finish
This commit is contained in:
parent
527821c120
commit
d80c6dc0e8
1 changed files with 62 additions and 14 deletions
|
|
@ -6,15 +6,11 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/08 12:15:32 by rparodi #+# #+# */
|
||||
/* Updated: 2025/04/12 23:22:11 by rparodi ### ########.fr */
|
||||
/* Updated: 2025/04/13 17:32:49 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "BitcoinExchange.hpp"
|
||||
#include <cmath>
|
||||
#include <regex.h>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
enum error_code {
|
||||
NO_ERROR = 0,
|
||||
|
|
@ -82,7 +78,7 @@ void _debug_print_input(const std::map<size_t, value>& data) {
|
|||
<< "\t" << CLR_YELLOW << "Key" << CLR_RESET
|
||||
<< "\t" << CLR_YELLOW << "Value" << CLR_RESET
|
||||
<< "\t " << CLR_YELLOW << "Date" << CLR_RESET
|
||||
<< "\t\t" << CLR_YELLOW << "Reason"
|
||||
<< "\t\t" << CLR_YELLOW << "Reason"<< CLR_RESET
|
||||
<< std::endl;
|
||||
|
||||
std::cout << CLR_BLUE << std::string(50, '-') << CLR_RESET << std::endl;
|
||||
|
|
@ -155,7 +151,7 @@ std::string nb_len(std::string nb) {
|
|||
return nb.substr(first_non_zero);
|
||||
}
|
||||
|
||||
float check_value(std::string value, enum error_code *error_code) {
|
||||
float check_value(bool more_than_1000, std::string value, enum error_code *error_code) {
|
||||
bool has_dot = false;
|
||||
if (value.at(0) == '-') {
|
||||
*error_code = NEGATIVE;
|
||||
|
|
@ -175,7 +171,7 @@ float check_value(std::string value, enum error_code *error_code) {
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
if (nb_len(value).size() >= 4 || atof(value.c_str()) > 1000) {
|
||||
if (more_than_1000 == false && (nb_len(value).size() >= 4 || atof(value.c_str()) > 1000)) {
|
||||
*error_code = TOO_LARGE;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -190,11 +186,15 @@ std::map<size_t, value>parse_input(std::string name) {
|
|||
std::map<size_t, value> to_ret;
|
||||
|
||||
std::string tmpLine;
|
||||
std::getline(file, tmpLine);
|
||||
std::string tmpDate;
|
||||
float tmpValue = 0;
|
||||
size_t line = 0;
|
||||
|
||||
std::getline(file, tmpLine);
|
||||
if (tmpLine != "date | value") {
|
||||
std::cerr << CLR_RED << "The first line of the file have to be `date | value`" << CLR_RESET << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
while (std::getline(file, tmpLine)) {
|
||||
tmpError = NO_ERROR;
|
||||
tmpDate.clear();
|
||||
|
|
@ -205,7 +205,7 @@ std::map<size_t, value>parse_input(std::string name) {
|
|||
to_ret.insert(std::make_pair(line, convertValue(tmpError, tmpValue, tmpLine)));
|
||||
} else {
|
||||
tmpDate = check_date(tmpLine.substr(0, limit), &tmpError);
|
||||
tmpValue = check_value(tmpLine.substr(limit + 3).c_str(), &tmpError);
|
||||
tmpValue = check_value(false, tmpLine.substr(limit + 3).c_str(), &tmpError);
|
||||
to_ret.insert(std::make_pair(line, convertValue(tmpError, tmpValue, tmpDate)));
|
||||
}
|
||||
line++;
|
||||
|
|
@ -213,6 +213,51 @@ std::map<size_t, value>parse_input(std::string name) {
|
|||
return to_ret;
|
||||
}
|
||||
|
||||
void print_result(std::map<size_t, value> input, std::map<std::string, float> db) {
|
||||
std::map<size_t, value>::iterator it_input = input.begin();
|
||||
std::map<std::string, float>::iterator it_db = db.begin();
|
||||
|
||||
while (it_input != input.end()) {
|
||||
if (it_input->second.reason == NO_ERROR) {
|
||||
while (it_db != db.end() && it_input->second.date > it_db->first)
|
||||
it_db++;
|
||||
if (it_db == db.begin()) {
|
||||
it_db--;
|
||||
}
|
||||
if (it_db == db.end()) {
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => no data found" << CLR_RESET << std::endl;
|
||||
} else {
|
||||
std::cout << it_input->second.date << " => " << it_input->second.value << " = " << it_input->second.value * it_db->second << std::endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
switch (it_input->second.reason) {
|
||||
case NEGATIVE:
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => is negative" << CLR_RESET << std::endl;
|
||||
break;
|
||||
case NO_FORMAT:
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => bad format" << CLR_RESET << std::endl;
|
||||
break;
|
||||
case NO_DATE:
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => bad date" << CLR_RESET << std::endl;
|
||||
break;
|
||||
case NO_FLOAT:
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => bad value" << CLR_RESET << std::endl;
|
||||
break;
|
||||
case NO_LIMIT:
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => bad input" << CLR_RESET << std::endl;
|
||||
break;
|
||||
case TOO_LARGE:
|
||||
std::cerr << CLR_MAGENTA << it_input->second.date << CLR_RED << " => value too large" << CLR_RESET << std::endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
it_input++;
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, float> get_db() {
|
||||
enum error_code tmpError;
|
||||
std::map<std::string, float> to_ret;
|
||||
|
|
@ -230,7 +275,7 @@ std::map<std::string, float> get_db() {
|
|||
|
||||
if (limit != std::string::npos) {
|
||||
tmpDate = check_date(tmpLine.substr(0, limit), &tmpError);
|
||||
tmpValue = check_value(tmpLine.substr(limit + 1).c_str(), &tmpError);
|
||||
tmpValue = check_value(true, tmpLine.substr(limit + 1).c_str(), &tmpError);
|
||||
if (tmpError != NEGATIVE)
|
||||
to_ret.insert(std::make_pair(tmpDate, tmpValue));
|
||||
}
|
||||
|
|
@ -238,6 +283,8 @@ std::map<std::string, float> get_db() {
|
|||
return to_ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 2) {
|
||||
std::cerr << CLR_RED << "Usage: " << argv[0] << " <filename>" << CLR_RESET << std::endl;
|
||||
|
|
@ -262,5 +309,6 @@ int main(int argc, char *argv[]) {
|
|||
std::map<size_t, value> user_db = parse_input(argv[1]);
|
||||
_debug_print_input(user_db);
|
||||
std::map<std::string, float> value_db = get_db();
|
||||
_debug_print_db(value_db);
|
||||
// _debug_print_db(value_db);
|
||||
print_result(user_db, value_db);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue