patterncppMinor
A command-line calculator using infix to postfix in C++
Viewed 0 times
linepostfixinfixusingcommandcalculator
Problem
I was in the mood for some C++ and decided to write a command line calculator, which understands addition, subtraction, multiplication and parentheses. See what I have:
main.cpp
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::isspace;
using std::ostream_iterator;
using std::pair;
using std::runtime_error;
using std::stack;
using std::string;
using std::stringstream;
using std::vector;
enum class TokenType {
OPERAND,
OPERATOR
};
enum class Operator {
PLUS,
MINUS,
MULTIPLY,
LEFT_PARENTHESIS,
RIGHT_PARENTHESIS
};
static int get_operator_precedence(Operator _operator)
{
switch (_operator) {
case Operator::PLUS:
case Operator::MINUS:
return 1;
case Operator::MULTIPLY:
return 0;
default:
if (_operator == Operator::LEFT_PARENTHESIS)
{
throw runtime_error(
"Left parenthesis has no precedence assigned to it.");
}
if (_operator == Operator::RIGHT_PARENTHESIS)
{
throw runtime_error(
"Right parenthesis has no precedence assigned to it.");
}
throw std::runtime_error(
"Unknown operator. This should not happen.");
}
}
class Token
{
TokenType m_token_type;
Operator m_operator;
int m_operand;
public:
Token(int operand) : m_operand{operand},
m_token_type{TokenType::OPERAND} {}
Token(Operator _operator) : m_operand{0},
m_token_type(TokenType::OPERATOR),
m_operator{_operator} {}
TokenType get_token_type() const
{
return m_token_type;
}
Operator get_operator() const
{
return m_operator;
}
int get_operand() const
{
re
main.cpp
```
#include
#include
#include
#include
#include
#include
#include
#include
#include
using std::cin;
using std::copy;
using std::cout;
using std::endl;
using std::isspace;
using std::ostream_iterator;
using std::pair;
using std::runtime_error;
using std::stack;
using std::string;
using std::stringstream;
using std::vector;
enum class TokenType {
OPERAND,
OPERATOR
};
enum class Operator {
PLUS,
MINUS,
MULTIPLY,
LEFT_PARENTHESIS,
RIGHT_PARENTHESIS
};
static int get_operator_precedence(Operator _operator)
{
switch (_operator) {
case Operator::PLUS:
case Operator::MINUS:
return 1;
case Operator::MULTIPLY:
return 0;
default:
if (_operator == Operator::LEFT_PARENTHESIS)
{
throw runtime_error(
"Left parenthesis has no precedence assigned to it.");
}
if (_operator == Operator::RIGHT_PARENTHESIS)
{
throw runtime_error(
"Right parenthesis has no precedence assigned to it.");
}
throw std::runtime_error(
"Unknown operator. This should not happen.");
}
}
class Token
{
TokenType m_token_type;
Operator m_operator;
int m_operand;
public:
Token(int operand) : m_operand{operand},
m_token_type{TokenType::OPERAND} {}
Token(Operator _operator) : m_operand{0},
m_token_type(TokenType::OPERATOR),
m_operator{_operator} {}
TokenType get_token_type() const
{
return m_token_type;
}
Operator get_operator() const
{
return m_operator;
}
int get_operand() const
{
re
Solution
Quite nice. A couple of small things:
- you can use
emplace_backinstead ofpush_backwhen adding Tokens. Likewise for the stack withemplaceinstead ofpush
- for the sake of symetry, do
out
- check your IO: your mainloop should be while (getline(cin, expr)) { ... }
to detectEOF
- don't use _variable` for names.
Context
StackExchange Code Review Q#135672, answer score: 4
Revisions (0)
No revisions yet.