From 481de18bbdddb28fb69f67acced7d021cf658ad5 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 16 Dec 2023 12:19:37 +0100 Subject: [PATCH] fix: do calcul in right order --- ex01/src/main.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 08f173f..3edc579 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -16,10 +16,10 @@ int calculate(std::stack& nums, std::stack& operators) return 0; int ret; - int a = nums.top(); - nums.pop(); int b = nums.top(); nums.pop(); + int a = nums.top(); + nums.pop(); switch (operators.top()) { case '+': @@ -60,17 +60,19 @@ int main(int ac, char** av) if (std::isdigit(av[1][i])) nums.push(av[1][i] - '0'); else if (std::strchr("/+-*", av[1][i]) != NULL) + { operators.push(av[1][i]); + if (calculate(nums, operators)) + { + std::cout << "error: division by 0" << std::endl; + return 1; + } + } else { std::cout << "error: '" << av[1][i] << "' unknown char" << std::endl; return 1; } - if (calculate(nums, operators)) - { - std::cout << "error: division by 0" << std::endl; - return 1; - } } if (nums.size() != 1) { @@ -79,7 +81,7 @@ int main(int ac, char** av) } if (operators.size() != 0) { - std::cout << "error: too manu operators" << std::endl; + std::cout << "error: too many operators" << std::endl; return 1; } std::cout << nums.top();