0

Hello everyone,

I'm writing a code and in there and I need to convert a char which contains any of "+", "-", "*", "/" into a real operator.
As an example let's say I have:

char myOp = '+';
int op1 = 10, op2 = 20;

cout << op1 myOp op2 << endl;

How could I do that.

Of course I know I could use "switch", but I am looking for a more professional way of doing it.("If there exist one!!!").

Thanks a lot.

3
Contributors
3
Replies
8
Views
7 Years
Discussion Span
Last Post by group256
Featured Replies
  • 1

    Someone asked something similar before in [URL="http://cboard.cprogramming.com/cplusplus-programming/107469-custom-loose-coupling-string.html"]another forum[/URL] and you could do something like this.[code=c++]class thing { private: typedef int (*_fptr)(int,int); // Typedef this for readibility of functino pointer std::map<std::string, _fptr> mfp; // Associate a string to a function pointer public: void add( std::string id, _fptr f ) { mfp[id] … Read More

  • 1
    __avd 1,826   7 Years Ago

    Token pasting operator. [code=cplusplus] #include <iostream> using namespace std; #define paster( n,m,k ) cout << "\n" << (token##n k token##m) int main() { int token9 =30; int token8=10; paster(9,8,-); paster(9,8,+); paster(9,8,*); return 0; } [/code] Read More

1

Someone asked something similar before in another forum and you could do something like this.

class thing {
private:
  typedef int (*_fptr)(int,int); // Typedef this for readibility of functino pointer
  std::map<std::string, _fptr> mfp; // Associate a string to a function pointer

public:
  void add( std::string id, _fptr f ) {
    mfp[id] = f; // Add a function pointer (shuld check if the function already exists)
  }

  int call(std::string func, int l, int r){
    return mfp[func]( l, r ); // Call the function (should check if function already exists)
  }

};

int add( int l, int r ) {
  return l+r;
}
int sub( int l, int r ) {
  return l-r;
}
// Simple functions

int main( void ) {
  thing t;
  t.add("add",add);
  t.add("sub",sub);// Add the functions to the class

  int l(5), r(3);
  std::string func;
  std::cout<< "Enter function (add,sub): "; // Ask for the function that's to be called
  std::cin >> func;
  std::cout<< t.call(func,l,r) << "\n"; // Call it
  
  return 0;
}

Maybe it's too much, maybe not... but I think it does what you want.

1

Token pasting operator.

#include <iostream>

using namespace std;
#define paster( n,m,k ) cout << "\n" << (token##n  k token##m)
int main()
{
  int token9 =30;
  int token8=10;
  paster(9,8,-);
  paster(9,8,+);
  paster(9,8,*);
  return 0;
}
Comments
nice!
0

Thank you very much twomers cause your solution is kind of what I had in mind but it's really long way for doing it.

Adatapost, that was one interesting solution. So I am gonna give it a go.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.