determine if is operand

This is a discussion on determine if is operand within the C++ Programming forums, part of the General Programming Boards category; to determine if part of a string is an operand, i have something like this: Code: bool isOperand(int pos, string ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    7

    determine if is operand

    to determine if part of a string is an operand, i have something like this:
    Code:
    bool isOperand(int pos, string op)
    {
    	if (op.substr(pos, 1) == '(' ||
    		op.substr(pos, 1) == ')' ||
    		op.substr(pos, 1) == '+' ||
    		op.substr(pos, 1) == '-' ||
    		op.substr(pos, 1) == '*' ||
    		op.substr(pos, 1) == '/')
    		return true;
    	else
    		return false;
    }
    am i headed in the right direction or is there a built in function for something like this?
    thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    string::substr() returns a std::string. If all you're looking for is a single character, why not use the .at() function or even the array index operator []?

    Also, saving that character might make your job easier.
    Code:
    bool isOperand(int pos, string op)
    {
    	char c = op[0];
    	if (c == '(' ||
    		c == ')' ||
    		c == '+' ||
    		c == '-' ||
    		c == '*' ||
    		c == '/')
    		return true;
    	else
    		return false;
    }
    Finally, you can get rid of the if-else clause entirely if you want to. Just use return and then your big conditional expression.

    Or you could even use
    Code:
    bool isOperand(int pos, string op)
    {
    	return string("()+-*/").find(op[0]) != string::npos;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Why are you working with parts of strings? Don't you have a proper tokenizer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. determine if tree is strictly binary bst
    By ashir30000 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2010, 01:51 PM
  2. delete zero out its operand
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2008, 12:55 AM
  3. Determine length of an array
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 04-21-2007, 09:32 PM
  4. determine how many 0's are in a number (value)
    By pyazarian in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2005, 02:20 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21