Skip to content

Commit 68308b6

Browse files
committed
Add initial implementation for BitcoinExchange project
- Created input.txt with sample data for testing. - Implemented main.cpp for the BitcoinExchange application. - Added Makefile for mutantstack example in ex01. - Added Makefile for BitcoinExchange application in ex02.
1 parent 18337a8 commit 68308b6

13 files changed

Lines changed: 1845 additions & 7 deletions

File tree

07/ex00/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $(NAME): $(BUILD_DIR) $(BUILD)
1010
@c++ $(BUILD) -o $(NAME)
1111

1212
$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
13-
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++23 -c $< -o $@
13+
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++17 -c $< -o $@
1414

1515
$(BUILD_DIR):
1616
@mkdir -p $(BUILD_DIR)

07/ex01/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $(NAME): $(BUILD_DIR) $(BUILD)
1010
@c++ $(BUILD) -o $(NAME)
1111

1212
$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
13-
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++23 -c $< -o $@
13+
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++17 -c $< -o $@
1414

1515
$(BUILD_DIR):
1616
@mkdir -p $(BUILD_DIR)

07/ex02/Array.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef ARRAY_HPP
2-
#define ARRAY_HPP
1+
#pragma once
32

43
#include <cstddef>
54
#include <exception>
@@ -54,5 +53,3 @@ class Array {
5453
// Size method
5554
unsigned int size() const { return _size; }
5655
};
57-
58-
#endif

07/ex02/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ $(NAME): $(BUILD_DIR) $(BUILD)
1010
@c++ $(BUILD) -o $(NAME)
1111

1212
$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
13-
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++23 -c $< -o $@
13+
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++17 -c $< -o $@
1414

1515
$(BUILD_DIR):
1616
@mkdir -p $(BUILD_DIR)

07/ex02/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ int main() {
2525
std::cout << "copy[" << i << "] = " << copy[i] << std::endl;
2626
}
2727

28+
// Scope test
29+
{
30+
Array<int> scopedArray(3);
31+
for (unsigned int i = 0; i < scopedArray.size(); i++) {
32+
scopedArray[i] = i + 1;
33+
std::cout << "scopedArray[" << i << "] = " << scopedArray[i] << std::endl;
34+
}
35+
std::cout << "Scoped array size: " << scopedArray.size() << std::endl;
36+
}
37+
2838
// Verify deep copy
2939
numbers[0] = 100;
3040
std::cout << "\nAfter modifying original:" << std::endl;

09/ex00/BitcoinExchange.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "BitcoinExchange.hpp"
2+
3+
#include <fstream>
4+
#include <iostream>
5+
#include <istream>
6+
#include <sstream>
7+
#include <stdexcept>
8+
9+
BitcoinExchange::BitcoinExchange() {
10+
// Constructor implementation
11+
}
12+
13+
BitcoinExchange::~BitcoinExchange() {
14+
// Destructor implementation
15+
}
16+
17+
BitcoinExchange::BitcoinExchange(const BitcoinExchange &other) : data(other.data) {
18+
// Copy constructor implementation
19+
}
20+
21+
BitcoinExchange &BitcoinExchange::operator=(const BitcoinExchange &other) {
22+
if (this != &other) {
23+
data = other.data;
24+
}
25+
return *this;
26+
}
27+
28+
void BitcoinExchange::parseLine(const std::string &line) {
29+
std::istringstream stream(line);
30+
std::string date, valueStr;
31+
if (std::getline(stream, date, ',') && std::getline(stream, valueStr)) {
32+
float value = std::stof(valueStr);
33+
validateDate(date);
34+
validateValue(value);
35+
addValue(date, value);
36+
}
37+
}
38+
39+
/**
40+
* @brief When loading data from the data.csv file, we need to do ',' splitting.
41+
* This is not the same as the splitting in the input.txt file, which uses '|' as a separator.
42+
*
43+
* @param filename
44+
* @param splitChars
45+
*/
46+
void BitcoinExchange::loadData(const std::string &filename, const std::string &splitChars) {
47+
std::ifstream file(filename);
48+
std::string line;
49+
50+
if (!file.is_open()) {
51+
throw std::runtime_error("Could not open file: " + filename);
52+
}
53+
54+
// check the first line (either "date, value" or "date | value")
55+
if (std::getline(file, line)) {
56+
if (line.find(',') != std::string::npos) {
57+
// If the line contains a comma, we assume it's a CSV format
58+
while (std::getline(file, line)) {
59+
parseLine(line);
60+
}
61+
} else if (line.find('|') != std::string::npos) {
62+
// If the line contains a pipe, we assume it's a different format
63+
while (std::getline(file, line)) {
64+
std::replace(line.begin(), line.end(), '|', ',');
65+
parseLine(line);
66+
}
67+
} else {
68+
throw std::runtime_error("Unknown file format in: " + filename);
69+
}
70+
}
71+
}

09/ex00/BitcoinExchange.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <map>
4+
#include <string>
5+
6+
class BitcoinExchange {
7+
public:
8+
BitcoinExchange();
9+
~BitcoinExchange();
10+
BitcoinExchange(const BitcoinExchange &other);
11+
BitcoinExchange &operator=(const BitcoinExchange &other);
12+
13+
void loadData(const std::string &filename, const std::string &splitChars);
14+
float getValue(const std::string &date) const;
15+
void printAllValues() const;
16+
void printValue(const std::string &date) const;
17+
void addValue(const std::string &date, float value);
18+
void removeValue(const std::string &date);
19+
void clearData();
20+
bool isValidValue(float value) const;
21+
std::map<std::string, float> getData() const;
22+
23+
private:
24+
std::map<std::string, float> data;
25+
26+
void parseLine(const std::string &line);
27+
void validateDate(const std::string &date) const;
28+
void validateValue(float value) const;
29+
};

09/ex00/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
NAME = mutantstack
2+
SRC = main.cpp
3+
HEADERS = MutantStack.hpp
4+
BUILD_DIR = build
5+
BUILD = $(addprefix $(BUILD_DIR)/, $(SRC:.cpp=.o))
6+
7+
all: $(NAME)
8+
9+
$(NAME): $(BUILD_DIR) $(BUILD)
10+
@c++ $(BUILD) -o $(NAME)
11+
12+
$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
13+
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++23 -c $< -o $@
14+
15+
$(BUILD_DIR):
16+
@mkdir -p $(BUILD_DIR)
17+
18+
clean:
19+
@rm -rf $(NAME)
20+
21+
fclean: clean
22+
@rm -rf $(BUILD_DIR)
23+
24+
re: fclean all
25+
26+
format:
27+
@clang-format -i $(SRC) $(HEADERS)
28+
29+
run: all
30+
@printf "\n🤖 08/ex02 $(NAME) output:\n\n"
31+
@./$(NAME)
32+
33+
.PHONY: all clean fclean re

0 commit comments

Comments
 (0)