A beginner question on how to use a variable as an operator

I have a question could a variable act as an operator such as "+" to add two numbers if so how do i write that? so what I would like to do would be answer = (a b c)
where a is some number b is an operator (such as +) and c is another number, Im not sure how to write that so i could get that line to work and help would be really appreciated

basically trying to a calculator without all the cases
Last edited on
You can do it, but it's quite a lot more complicated that using a switch with four to six cases. You have to #include <functional> then in the create an std::function<int(int,int)> use whatever type your operands are instead of int if they aren't int. You would then write answer = function(left,right) with function being whatever you called your function object. You would have to set function to a different function depending on what the input was, which could shorten your cases, but it's not worth the hassle.

tl;dr Yes you can have a variable that acts like a function, not an operator though the difference is basically semantic. But the resulting code really really will not be worth your time.
Last edited on
Or you can just read operation into char and have switch with 4 cases.

Or a custom math expression parser (Similar to what Stroustrup uses in his book)
operators are methods that usually are used for non-primitive type . What I am saying is that
1 + 2 does not need you to overload the operator + . The moment the + or - operator becomes (in general speaking ... ) is when you sum matrices or vectors , which are not primitive type .

For a calculator exercice , you can try the parsing .
operators are methods that usually are used for non-primitive type .

No. Most operators are used for primitive types such as your example of 1+2.

What I am saying is that 1 + 2 does not need you to overload the operator +

This is entirely true but not entirely relevant. Just because you don't overload it does not mean it is not an operator, neither does it mean you cannot replicate its function.

The moment the + or - operator becomes (in general speaking ... ) is when you sum matrices or vectors , which are not primitive type .

This sentence doesn't make any sense and hence I can't really respond to it, but if you're saying that the moment + or - becomes an operator is when they're being used for non-primitive types is also purely false.

For a calculator exercise , you can try the parsing .

This has already been said. It also doesn't answer the question of how to implement it. (switch or map from char to function or whatever)
I agree on that , what I am trying to say it that we should see it like an arithmetic operation instead .

[quote]What I am saying is that 1 + 2 does not need you to overload the operator +


This is entirely true but not entirely relevant. Just because you don't overload it does not mean it is not an operator[/quote] Well I did not say it was not operator.

To answer to the parser thing there is an interressant link I found on Google :

http://www.technical-recipes.com/2011/a-mathematical-expression-parser-in-java-and-cpp/
@OP: Do you need just a single operation? Just add/substract etc.? The just use a simple soluttion like reading an operator and selecting needed operation by switch
https://en.wikipedia.org/wiki/KISS_principle
https://en.wikipedia.org/wiki/You_aren't_gonna_need_it

If you need or expect to need to handle complex expressions? Then make a parser. Use Ericool link for (rather unintuitive but good) shunting yard algorithm or Precedence Climbing for more simple parser:
http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm

Or you can try Pratt Parser
https://en.wikipedia.org/wiki/Pratt_parser
@miini i want to be able to just do one operation but selected by a user +,-,/,and *
and thanks for the replies guys!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    int a, b ;
    char oper ;

    std::cin >> a >> oper >> b ;

    int result = 0 ;

    switch(oper)
    {
        case '+' : result = a+b ; break ;
        case '-' : result = a-b ; break ;
        case '*' : result = a*b ; break ;
        // more cases..
        default:
            std::cerr << "unsupported operator '" << oper << "'\n" ;
            return 1 ;
    }

    std::cout << a << ' ' << oper << ' ' << b << " == " << result << '\n' ;
}
Topic archived. No new replies allowed.