From: Alma Dejus Date: Tue, 31 Oct 2023 09:52:06 +0000 (+0100) Subject: main file X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=ef64b177e73a8a8d0010096ed2192d71f76a2bbb;p=TDDC76.git main file --- diff --git a/src/uppgift13.cc b/src/uppgift13.cc index 64b494b..3c65e51 100644 --- a/src/uppgift13.cc +++ b/src/uppgift13.cc @@ -1,6 +1,6 @@ #include #include "expression.h" - +#include int main() { @@ -12,29 +12,39 @@ int main() while (std::getline(std::cin, line)) { - if (line == ":prefix") - { - std::cout << e.to_prefix_string() << std::endl; - } - else if (line == ":infix") - { - std::cout << e.to_infix_string() << std::endl; - } - else if (line == ":postfix") - { - std::cout << e.to_postfix_string() << std::endl; - } - else if (line == ":calc") - { - std::cout << "Value: " << e.evaluate() << std::endl; - } - else if (line == ":quit") - { - break; + if (line[0] == ':') { + if (line == ":prefix") + { + std::cout << e.to_prefix_string() << std::endl; + } + else if (line == ":infix") + { + std::cout << e.to_infix_string() << std::endl; + } + else if (line == ":postfix") + { + std::cout << e.to_postfix_string() << std::endl; + } + else if (line == ":calc") + { + std::cout << "Value: " << e.evaluate() << std::endl; + } + else if (line == ":quit") + { + break; + } + else { + std::cout << "Uknown command, try again!\n"; + } } else { - e = line; + try{ + e = line; + } + catch(std::logic_error&) { + std::cout << "Input can only be command (start with ':') or expression (numbers and operators). Try again!\n"; + } } } return 0;