2

I am working on a school assignment. What I want to do is not the focus of the assignment, merely something I would like to do better.

I have the following if statement where equation is a string

if( (equation[0]== "*") || (equation[0]== "/") || (equation[0]== "+") || (equation[0]== "-"))

All it does is check to see if the first and last characters of the string are one of 4 operators. This is long, bulky, and hopefully not the best way of doing this. Sadly I do not have access to regular expressions as this type of check would be very easy using them.

Is there a better way of writing the if statement without using regular expressions? Something along the lines of

if( equation[0] == ("/" || "*" || "+" || "-"))
Cœur
  • 37,241
  • 25
  • 195
  • 267
Fering
  • 322
  • 2
  • 18
  • 2
    No, there's no other way. If you have lots of similar `if` statements all after each other then you might change to a `switch` statement instead (though it will not work with strings, checking characters will work fine). – Some programmer dude Oct 05 '16 at 05:29
  • If `equation` is a `string`, then `equation[0]` is a `char`. – barak manos Oct 05 '16 at 05:33
  • As everyone said, it's not possible. However, for future reference the [Ternary Operator](https://en.wikipedia.org/wiki/%3F:) in a sense condenses `if` statements. – Gary Holiday Oct 05 '16 at 05:34
  • Your existing `if` expression won't work: `equation[0]` will evaluate to a `char` value, but double-quoted values are strings, so the `==` operator will be comparing a character value to a memory address, which is nonsensical. – Dai Oct 05 '16 at 05:34

4 Answers4

5

How about

if(std::string("*/+-").find(equation[0])!=std::string::npos)
Rohit Chatterjee
  • 3,048
  • 1
  • 15
  • 16
  • (I should add that I haven't written C++ in over five years but I think the syntax is correct...) – Rohit Chatterjee Oct 05 '16 at 05:35
  • This is good, but it has a run time of `O(n)` and the original `if` statement technically has `O(1)` not really a problem in this case but could potentially be a problem if used in a different example. – Gary Holiday Oct 05 '16 at 05:39
  • 1
    I would not say that this is "cleaner". It's definitely not more readable or clearer. A comment in the code explaining this would be mandatory IMO. – Some programmer dude Oct 05 '16 at 05:39
  • @GaryJohnson original if statement is not O(1) because each statement will be checked one by one, so it'll be same O(N), but with little less memory – Denis Sheremet Oct 05 '16 at 05:41
  • @DenisSheremet in this example that may be true. As I stated that it's not a problem. However, a different example could potentially cause a problem. Just something to be aware of – Gary Holiday Oct 05 '16 at 07:07
2

You may consider using switch operator

switch(equation[0]) {
  case '/':
  case '*':
  case '+':
  case '-':
    //your code
    break;
  default:
    //else statement
}
Denis Sheremet
  • 2,453
  • 2
  • 18
  • 34
  • 1
    If doing this with c++17 (if anyone is yet) then they should consider using the [`[[fallthrough]]`](http://en.cppreference.com/w/cpp/language/attributes) attribute. – Paul Rooney Oct 05 '16 at 05:36
1

I don't think it is possible to do what you're asking.

Best you can do is put equation[0] in another shorter name variable.

std::string a = equation[0];

if( (a"*") || (a== "/") || (a== "+") || (a== "-"))
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
pocpoc47
  • 95
  • 1
  • 16
0

No, that's not possible. But I think it's always cleaner and more readable to

switch (equation[0]) {
case '*':
case '/':
case '+':
case '-':
    //dostuff
    break;
default:
    //dostuff
}

This way, it's also really easy to replace (add or remove) new characters. If you would to it with a regular expression, that would be definetely more work to do.

Stack Danny
  • 7,754
  • 2
  • 26
  • 55