Page 1 of 6 123 ... LastLast
Results 1 to 10 of 60

Thread: Using character as relational operator in C/C++

  1. #1
    Join Date
    Mar 2008
    Beans
    29

    Question Using character as relational operator in C/C++

    Hello there

    How can I use a character entered by the user as a relational operator. Something like:

    Code:
    int var1 = 30;
    int var2 = 20;
    char op_char = cin.get();         // User enters '<'
    
    if (var1 op_char var)
    {
    Do something...
    }
    I want to avoid a lot of switch case statements.
    Any help?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Using character as relational operator in C/C++

    Quote Originally Posted by control_guy View Post
    Hello there

    How can I use a character entered by the user as a relational operator. Something like:

    Code:
    int var1 = 30;
    int var2 = 20;
    char op_char = cin.get();         // User enters '<'
    
    if (var1 op_char var)
    {
    Do something...
    }
    I want to avoid a lot of switch case statements.
    Any help?

    Thanks.
    you really can't ...
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    May 2008
    Location
    France
    Beans
    120
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Using character as relational operator in C/C++

    You'd need an equivalent of the "eval" function found in some languages...

    Edit : or map the chars to the desired functions ?
    Last edited by Bichromat; June 2nd, 2008 at 04:26 PM.
    À la chasse au boson intermédiaire

  4. #4
    Join Date
    Mar 2008
    Beans
    29

    Re: Using character as relational operator in C/C++

    Quote Originally Posted by Bichromat View Post
    You'd need an equivalent of the "eval" function found in some languages...
    Yes thats what I am looking for in C/C++. It is in Matlab.

  5. #5
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Using character as relational operator in C/C++

    Eval is a very high-level language feature... you just don't have it in statically compiled languages like C or C++. You'd have to essentially run some kind of interpreter on top of your own code.
    LambdaGrok. | #ubuntu-programming on FreeNode

  6. #6
    Join Date
    May 2007
    Location
    Belgrade, Serbia
    Beans
    172
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Using character as relational operator in C/C++

    No, a hash map + pointers to functions will do the trick .

    Aspect-oriented programming exists because of situations like that .
    Last edited by dempl_dempl; June 2nd, 2008 at 04:46 PM.
    http://www.stosha.net/ - Collection of linux widgets and libraries.

  7. #7
    Join Date
    Mar 2008
    Beans
    29

    Re: Using character as relational operator in C/C++

    Quote Originally Posted by dempl_dempl View Post
    No, a hash map + pointers to functions will do the trick .
    Could you kindly post some example code?

  8. #8
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Using character as relational operator in C/C++

    Quote Originally Posted by dempl_dempl View Post
    No, a hash map + pointers to functions will do the trick .
    It's still a glorified switch/case statement
    LambdaGrok. | #ubuntu-programming on FreeNode

  9. #9
    Join Date
    May 2007
    Location
    Belgrade, Serbia
    Beans
    172
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Using character as relational operator in C/C++

    This is aspect-oriented example.


    Create a factory.h file :

    Code:
    /*This is a file factory.h  */
    
    
    
    typedef bool (*funcPtr)(int , int);
    
    
    #include <map>
    
    class OperatorsFactory
    {
     private:
     	
     	//Did not have time for generic singleton :)
     	/ nor generic factory :)
     	OperatorsFactory() {}
     	~OperatorsFactory() {} 
     	
     	
     	std::map< char, funcPtr >   operationsMap;
      public:
      	OperatorsFactory& getInstance()   	
      	{ 
      		static OperatorsFactory of;
      		return of
      	}
      	
      	
      	void register(  char op , funcPtr   fp )  	
      	{
      		operationsMap.insert (  make_pair( op , fp )  );
      	}
      	
      	
      	
      	funcPtr getByOperator( char op )  	
      	{
      		return operationsMap.find( op.second );
      	}
    }
    
    //--------------End of Factory.h -----------------------------------///



    Next , create a file called less_than.cpp and add it to project

    Code:
    /*This is less_than.cpp */
    
    #include "factory.h"
    
    
    //Anonymous namespace , common practise , for this things , I'll explain why some other time :)
    namespace 
    {
    	bool less_than_function( int a , int b )
    	{
    		return a < b;
    	}
    	
    	
    	OperatorsFactory::getInstance().register( '<' , less_than_function);	
    }

    Create main.cpp , and add it to project :


    Code:
    /*This is less_than.cpp */
    
    
    
    //Note the fact I did NOT use  #include less_than.h or anything !
    #include "factory.h"
    
    int main()
    {
    	char c;
    	cin >>c ;
    	
    	
    	OperatorsFactory& opFactory = OperatorsFactory::getInstance().
    	
    	funcPtr fp = opFactory.getByOperator(c);
    	
    	if(fp(2,3))
    		cout << "Yes! it\'s alive! " << endl;
    	
    }
    
    //--------------End of main.cpp -----------------------------------///
    Now here's the interesting part
    If you wany yo create another operator, create a file called , for example , equals.cpp
    and add it to project



    Code:
    /*This is l equals.cpp  */
    
    #include "factory.h"
    
    
    //Anonymous namespace , common practise , for this things , I'll explain why some other time :)
    namespace 
    {
    	bool equals( int a , int b )
    	{
    		return a < b;
    	}
    	
    	
    	OperatorsFactory::getInstance().register( '==' , equals);	
    }
    
    
    //--------------End of equals.cpp -----------------------------------///
    Now, if you want to use operator '==' , you don't have to change main.cpp file,
    just add equals.cpp to project, and it'll self-register the '==' operator to main.cpp

    You can do this for as many operators as you would like.

    Of course, you can put as many operators as you want in one cpp file.

    Of course, this aspect-oriented example is used for some larger projects , which need this plugin-based architecture,
    this is a little bit of over-kill .

    The whole strength in this example is that you don't change your main.cpp code.

    When you want a new operator , you add a new module ( you add the code , you don't change it )

    It also has an advantage of code being a lot less coupled, unlike switch statement, which has to #include all
    the code in one file. Less external depencies makes program a lot easier to maintain .


    Btw, did I've scared you with this code ? You've asked for dynamic operators , and I've thrown you the aspect-oriented programming

    Sorry if I did

    Cheers!
    Last edited by dempl_dempl; June 2nd, 2008 at 06:06 PM.
    http://www.stosha.net/ - Collection of linux widgets and libraries.

  10. #10
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Using character as relational operator in C/C++

    If this is just a simple "a ? b" kind of program, yes you could use a hash or a simple switch case if you just use 4 operations.

    If you eventually want to do a parse an expression, you could use a tree:

    Code:
                *
               / \
              +   *
            / |   | \
           3  |   5  14
              -
             / \
            x   3
    
    innode: 3 + (x - 3) * (5 * 14)
    This is useful when you need to store a formula for later, depends on what you're trying to accomplish.

    Also, here is the python "one-liner"

    print input("command:")
    Last edited by Can+~; June 2nd, 2008 at 05:34 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

Page 1 of 6 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •