Home > Back-end >  Multiple if c to judge how to simplify
Multiple if c to judge how to simplify

Time:09-30

From an input string to determine whether there is a "+" or "-" or "* or"/",
I wrote

 
String STR
If (STR), find (' + ')!=string. Npos)
{
The position=STR. Find (' + ');
.
}
Else if (STR), find (' - ')!=string. Npos)
{
The position=STR. Find (' - ');
.
}
Else if (STR), find ('/').=string. Npos)
{
The position=STR. Find ('/');
.
}
Else if (STR), find (' * ')!=string. Npos)
{
The position=STR. Find (' * ');
.
}

A symbol of the position, how to simplify

Not analytic expression, no prizes for guessing

CodePudding user response:

 # include & lt; Iostream> 
#include

using namespace std;

Int main (void) {
String STR, target="+ - */";
Auto positoin=string: : npos;
For (char c: target) {
If ((positoin=STR. Find (c)).=the string: : npos)
break;
}

return 0;
}
are for reference only

CodePudding user response:

 if (((position=STR. Find (' + '))!=the string: : npos) | | 
((position=STR. Find (' - '))!=the string: : npos) | |
((position=STR. Find (' * '))!=the string: : npos) | |
(the position=STR. Find ('/'))!=the string: : npos)
) {
//other code...
}

Save query again the steps, at the same time shrink into an if performed

CodePudding user response:

This is the or relationship because of you, can consider to use hash, so only need to traverse the STR once can safely draw the conclusion that the

 
Char str_hash [256]={0};
Str_hash [(unsigned char) '+']=1;
Str_hash [(unsigned char) '-']=1;
Str_hash [(unsigned char) '*']=1;
Str_hash [(unsigned char) '/']=1;

String STR="abcd - + sdfajjj";
Int get_operator_pos (string & amp; STR)
{
for(int i=0; i {
Char ch=STR [I];
If (str_hash [(unsigned char) ch]!=0)
return i;
}
}

CodePudding user response:

With regular expressions, so you judge has serious shortcomings, if the condition is more will be repeated traversal, seriously affect the efficiency

CodePudding user response:

Find the source code for Linux is a dance judge, high efficiency more than doubled, so much,

CodePudding user response:

Direct write debugging
 
Int to obtain position (string STR, char a)
{
If (STR), find (a)!=string. Npos)
Return the position=STR. Find (' + ');
}

CodePudding user response:

refer to 6th floor wise men know already should good karma response:
don't write directly debugging
 
Int to obtain position (string STR, char a)
{
If (STR), find (a)!=string. Npos)
Return the position=STR. Find (' + ');
}
not debugging don't sent to
What is the position?

CodePudding user response:

 bool findop (const string& STR, int ch) 
{
Char ch=0;
Sscanf (STR. C_str (), "% * [^ - + */] % c", & amp; Ch);
Return!!!!! ch;
}

CodePudding user response:

 bool findop (const string& STR) 
{
Char ch=0;
Sscanf (STR. C_str (), "% * [^ - + */] % c", & amp; Ch);
Return!!!!! ch;
}

Correct the second parameter is unnecessary

CodePudding user response:

There is a kind of optimization called "table driven"

CodePudding user response:

Thank you for your new method learn a lot
  • Related