push ex01
This commit is contained in:
parent
0b56ac5cea
commit
f4fec9e326
|
|
@ -10,6 +10,5 @@ int main(int argc, char **argv)
|
||||||
BitcoinExchange btc;
|
BitcoinExchange btc;
|
||||||
btc.loadDatabase("data.csv");
|
btc.loadDatabase("data.csv");
|
||||||
btc.processInput(argv[1]);
|
btc.processInput(argv[1]);
|
||||||
std::cout << btc.isValidDate("2000-02-29") << std::endl;
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
SOURCES = Main.cpp BitcoinExchange.cpp
|
SOURCES = Main.cpp BitcoinExchange.cpp
|
||||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||||
NAME = btc
|
NAME = btv
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
|
|
|
||||||
BIN
ex00/obj/Main.o
BIN
ex00/obj/Main.o
Binary file not shown.
|
|
@ -1,2 +0,0 @@
|
||||||
dsd
|
|
||||||
2000-02-29 | 1
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "RPN.hpp"
|
||||||
|
|
||||||
|
int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
if (ac == 2)
|
||||||
|
{
|
||||||
|
RPN rpn;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::cout << rpn.compute(av[1]) << std::endl;
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << '\n';
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::cerr << "Error" << std::endl;
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
CXX = c++
|
||||||
|
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
|
OBJDIR = obj
|
||||||
|
SOURCES = Main.cpp RPN.cpp
|
||||||
|
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||||
|
NAME = RPN
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(OBJDIR):
|
||||||
|
@echo "📁 Creating obj directory..."
|
||||||
|
@mkdir -p $(OBJDIR)
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||||
|
@echo "🧠 Compiling $< ..."
|
||||||
|
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
@echo "✅ $@ ready!"
|
||||||
|
|
||||||
|
$(NAME): $(OBJECTS)
|
||||||
|
@echo "🔗 Linking $(NAME) ..."
|
||||||
|
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||||
|
@echo "🎉 $(NAME) is ready!"
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo "🧹 Cleaning object files..."
|
||||||
|
@rm -rf $(OBJDIR)
|
||||||
|
@echo "✨ Objects cleaned!"
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@echo "🗑️ Removing $(NAME)..."
|
||||||
|
@rm -f $(NAME)
|
||||||
|
@echo "💀 Full clean complete!"
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
#include "RPN.hpp"
|
||||||
|
|
||||||
|
RPN::RPN(){}
|
||||||
|
|
||||||
|
RPN::RPN(const RPN &other)
|
||||||
|
{
|
||||||
|
this->_stack = other._stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
RPN &RPN::operator=(const RPN &other)
|
||||||
|
{
|
||||||
|
this->_stack = other._stack;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
RPN::~RPN(){}
|
||||||
|
|
||||||
|
int RPN::compute(const std::string &string)
|
||||||
|
{
|
||||||
|
std::istringstream iss(string);
|
||||||
|
std::string token;
|
||||||
|
std::string ops = "+-*/";
|
||||||
|
int b;
|
||||||
|
int a;
|
||||||
|
while (iss >> token)
|
||||||
|
{
|
||||||
|
if (token.size() == 1)
|
||||||
|
{
|
||||||
|
if (isdigit(token[0]))
|
||||||
|
_stack.push(token[0] - '0');
|
||||||
|
else if (ops.find(token[0]) != std::string::npos)
|
||||||
|
{
|
||||||
|
if (_stack.size() <= 1)
|
||||||
|
throw std::runtime_error("Error");
|
||||||
|
b = _stack.top();
|
||||||
|
_stack.pop();
|
||||||
|
a = _stack.top();
|
||||||
|
_stack.pop();
|
||||||
|
switch (token[0])
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
_stack.push(a + b);
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
_stack.push(a - b);
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
_stack.push(a * b);
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
if (b == 0)
|
||||||
|
throw std::runtime_error("Error");
|
||||||
|
_stack.push(a / b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw std::runtime_error("Error");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw std::runtime_error("Error");
|
||||||
|
}
|
||||||
|
if (_stack.size() != 1)
|
||||||
|
throw std::runtime_error("Error");
|
||||||
|
return _stack.top();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef RPN_HPP
|
||||||
|
# define RPN_HPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stack>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class RPN
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RPN();
|
||||||
|
RPN(const RPN &other);
|
||||||
|
RPN &operator=(const RPN &other);
|
||||||
|
~RPN();
|
||||||
|
|
||||||
|
int compute(const std::string &string);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::stack<int> _stack;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue