「🔨」 fix: fixed double number not working
This commit is contained in:
30
ex01/RPN.cpp
30
ex01/RPN.cpp
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/03 12:07:59 by adjoly #+# #+# */
|
||||
/* Updated: 2025/07/03 14:15:57 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/07/03 20:15:20 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -26,21 +26,15 @@ int div(int first, int second) {
|
||||
return first / second;
|
||||
}
|
||||
|
||||
void calc(int calc(int, int), std::string::const_iterator ,
|
||||
std::stack<int> &stack) {
|
||||
int first, second;
|
||||
void calc(int (*op)(int, int), std::stack<int> &stack) {
|
||||
if (stack.size() < 2)
|
||||
throw std::range_error("not enough operands");
|
||||
|
||||
if (stack.size() != 2)
|
||||
throw std::range_error("need 2 at least two number");
|
||||
second = stack.top();
|
||||
int second = stack.top();
|
||||
stack.pop();
|
||||
first = stack.top();
|
||||
int first = stack.top();
|
||||
stack.pop();
|
||||
if (stack.size() != 0)
|
||||
throw std::range_error("too much number");
|
||||
stack.push(calc(first, second));
|
||||
// if (*(c + 1) != ' ' || *(c + 1) != '\0')
|
||||
// throw std::invalid_argument("");
|
||||
stack.push(op(first, second));
|
||||
}
|
||||
|
||||
void handle(std::string::const_iterator c, std::stack<int> &stack) {
|
||||
@ -49,13 +43,13 @@ void handle(std::string::const_iterator c, std::stack<int> &stack) {
|
||||
else if (std::isdigit(*c))
|
||||
stack.push(*c - '0');
|
||||
else if (*c == '+')
|
||||
calc(add, c, stack);
|
||||
calc(add, stack);
|
||||
else if (*c == '-')
|
||||
calc(subst, c, stack);
|
||||
calc(subst, stack);
|
||||
else if (*c == '*')
|
||||
calc(mult, c, stack);
|
||||
calc(mult, stack);
|
||||
else if (*c == '/')
|
||||
calc(div, c, stack);
|
||||
calc(div, stack);
|
||||
else
|
||||
throw std::invalid_argument("invalid caracter detected");
|
||||
}
|
||||
@ -73,5 +67,7 @@ void rpn(char *av) {
|
||||
throw std::runtime_error(str.str());
|
||||
}
|
||||
}
|
||||
if (stack.size() != 1)
|
||||
throw std::runtime_error("Error: leftover operands in stack");
|
||||
std::cout << stack.top() << std::endl;
|
||||
}
|
||||
|
Reference in New Issue
Block a user