#include <iostream>
#include "expression.h"
-
+#include <stdexcept>
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;