From 569ea9fca506bb8df390fac9093675cc12ee5a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=81=D1=82=D0=B0=D1=81=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=A0=D1=8B=D0=B1=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Fri, 20 Sep 2024 11:06:46 +0300 Subject: [PATCH] lab2 update --- Lab2CPP/lab2.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 48d314a486..9775fa16bb 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -50,8 +50,10 @@ int evaluate_example(const std::string& example) { else if (example[i] == ')') { while (!stack_empty(operators) && stack_get(operators) != '(') { + if (stack_empty(values)) throw std::runtime_error("Invalid expression"); int val2 = stack_get(values); stack_pop(values); + if (stack_empty(values)) throw std::runtime_error("Invalid expression"); int val1 = stack_get(values); stack_pop(values); char op = stack_get(operators); @@ -66,8 +68,10 @@ int evaluate_example(const std::string& example) { else if (example[i] == '+' || example[i] == '-' || example[i] == '*' || example[i] == '/') { while (!stack_empty(operators) && precedence(stack_get(operators)) >= precedence(example[i])) { + if (stack_empty(values)) throw std::runtime_error("Invalid expression"); int val2 = stack_get(values); stack_pop(values); + if (stack_empty(values)) throw std::runtime_error("Invalid expression"); int val1 = stack_get(values); stack_pop(values); char op = stack_get(operators); @@ -80,8 +84,10 @@ int evaluate_example(const std::string& example) { while (!stack_empty(operators)) { + if (stack_empty(values)) throw std::runtime_error("Invalid expression"); int val2 = stack_get(values); stack_pop(values); + if (stack_empty(values)) throw std::runtime_error("Invalid expression"); int val1 = stack_get(values); stack_pop(values); char op = stack_get(operators);