「🔨」 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> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/06/03 12:07:59 by adjoly #+# #+# */
|
/* 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;
|
return first / second;
|
||||||
}
|
}
|
||||||
|
|
||||||
void calc(int calc(int, int), std::string::const_iterator ,
|
void calc(int (*op)(int, int), std::stack<int> &stack) {
|
||||||
std::stack<int> &stack) {
|
if (stack.size() < 2)
|
||||||
int first, second;
|
throw std::range_error("not enough operands");
|
||||||
|
|
||||||
if (stack.size() != 2)
|
int second = stack.top();
|
||||||
throw std::range_error("need 2 at least two number");
|
|
||||||
second = stack.top();
|
|
||||||
stack.pop();
|
stack.pop();
|
||||||
first = stack.top();
|
int first = stack.top();
|
||||||
stack.pop();
|
stack.pop();
|
||||||
if (stack.size() != 0)
|
stack.push(op(first, second));
|
||||||
throw std::range_error("too much number");
|
|
||||||
stack.push(calc(first, second));
|
|
||||||
// if (*(c + 1) != ' ' || *(c + 1) != '\0')
|
|
||||||
// throw std::invalid_argument("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle(std::string::const_iterator c, std::stack<int> &stack) {
|
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))
|
else if (std::isdigit(*c))
|
||||||
stack.push(*c - '0');
|
stack.push(*c - '0');
|
||||||
else if (*c == '+')
|
else if (*c == '+')
|
||||||
calc(add, c, stack);
|
calc(add, stack);
|
||||||
else if (*c == '-')
|
else if (*c == '-')
|
||||||
calc(subst, c, stack);
|
calc(subst, stack);
|
||||||
else if (*c == '*')
|
else if (*c == '*')
|
||||||
calc(mult, c, stack);
|
calc(mult, stack);
|
||||||
else if (*c == '/')
|
else if (*c == '/')
|
||||||
calc(div, c, stack);
|
calc(div, stack);
|
||||||
else
|
else
|
||||||
throw std::invalid_argument("invalid caracter detected");
|
throw std::invalid_argument("invalid caracter detected");
|
||||||
}
|
}
|
||||||
@ -73,5 +67,7 @@ void rpn(char *av) {
|
|||||||
throw std::runtime_error(str.str());
|
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;
|
std::cout << stack.top() << std::endl;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user