BitcoinExchange.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* BitcoinExchange.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2026/01/16 19:29:59 by bchanot #+# #+# */
  9. /* Updated: 2026/01/16 19:35:25 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "BitcoinExchange.hpp"
  13. #include <fstream>
  14. #include <climits>
  15. void ft_getcsv(std::map<Date, float> &Data)
  16. {
  17. std::string line;
  18. std::ifstream ifs;
  19. std::string token;
  20. float price;
  21. Date tmp;
  22. ifs.open("data.csv");
  23. if (!ifs) {
  24. std::cout << "Error: could not open data.csv file." << std::endl;
  25. throw std::exception();
  26. }
  27. std::getline(ifs, line);
  28. while (std::getline(ifs, line)) {
  29. token = line.substr(0, line.find(','));
  30. tmp.year = atoi(token.substr(0, 4).c_str());
  31. tmp.month = atoi(token.substr(5, 2).c_str());
  32. tmp.day = atoi(token.substr(8, 2).c_str());
  33. price = atof(line.substr(line.find(',') + 1, line.find('\n')).c_str());
  34. if (tmp.year < 0 || tmp.month < 0 || tmp.month > 12 || tmp.day < 0 || tmp.day > 31) {
  35. std::cout << "Error : Error in csv dates." << std::endl;
  36. throw std::exception();
  37. }
  38. if (price < 0 || price > INT_MAX) {
  39. std::cout << "Error : Error in csv price." << std::endl;
  40. throw std::exception();
  41. }
  42. Data[tmp] = price;
  43. }
  44. }
  45. void ft_process(char **av, std::map<Date, float> &Data) {
  46. std::ifstream ifs;
  47. std::string line;
  48. std::string token;
  49. std::map<Date, float>::iterator it;
  50. float amount;
  51. Date tmp;
  52. ifs.open(av[1]);
  53. if (!ifs) {
  54. std::cout << "Error: could not open " << av[1] << "." << std::endl;
  55. throw std::exception();
  56. }
  57. std::getline(ifs, line);
  58. while (std::getline(ifs, line)) {
  59. token = line.substr(0, line.find('|'));
  60. tmp.year = atoi(token.substr(0, 4).c_str());
  61. tmp.month = atoi(token.substr(5, 2).c_str());
  62. tmp.day = atoi(token.substr(8, 2).c_str());
  63. amount = atof(line.substr(line.find('|') + 1, line.find('\n')).c_str());
  64. if (tmp.year < 0 || tmp.month < 0 || tmp.month > 12 || tmp.day < 0 || tmp.day > 31) {
  65. std::cout << "Error : Bad date." << std::endl;
  66. continue ;
  67. }
  68. if (amount < 0 || amount > INT_MAX) {
  69. std::cout << "Error : Bad price." << std::endl;
  70. continue ;
  71. }
  72. it = Data.lower_bound(tmp);
  73. if (it->first != tmp) {
  74. if (it == Data.begin())
  75. std::cout << "Cannot find a price at the value " << tmp << "." << std::endl;
  76. else
  77. std::cout << tmp << " => " << amount << " = " << (--it)->second * amount << std::endl;
  78. } else
  79. std::cout << it->first << " => " << amount << " = " << it->second * amount << std::endl;
  80. }
  81. }