Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification in Section 5.10 of K&R 2

A pattern recognition program must print all lines containing the patter if the input is find pattern. If the input is find -x pattern, the program must print all lines except the lines containing pattern.

// .....
switch(c)
{
case 'x':
    except=1;
    break;
// ......
}

// ......
while(getline(line,MAXLINE)>0)
    {
    line_num++;
    if( (strstr(line,*argv)!=NULL) != except)
        {
        if(number)
            printf("%ld:",linenum);
        printf("%s",line);
        found++;
        }
    }
// ......

In the above code from K&R except can either be 1 or 0. How does if(strstr...) block functions effectively to handle -x ?

like image 275
ThunderPunch Avatar asked Jan 29 '16 07:01

ThunderPunch


People also ask

What is a 510 K clearance?

A 510(k) is a premarket submission made to FDA to demonstrate that the device to be marketed is as safe and effective, that is, substantially equivalent, to a legally marketed device (section 513(i)(1)(A) FD&C Act).

What should a 510 K include?

A 510(k) submission must include labeling information, including final draft copies of all proposed labels, labeling, user instructions and service manuals, as well as copies of proposed promotional materials and/or advertising.

What is a 510 K summary?

The 510(k) Statement is a certification that the 510(k) owner will provide safety and effectiveness information supporting the FDA finding of substantial equivalence to ANY person within 30 days of a written request.

What is a 510 K number?

A 510(k) number is a premarket notification number that is required for certain classes of medical devices in the US. Generally, devices that are higher risk require premarket notification to the FDA prior to bringing the devices to market.


1 Answers

The logic is simple. If the pattern is "-x" we should print all lines that do not contain the pattern.

For this pattern except is equal to 1.

So lines that contain the pattern satisfy the condition

strstr(line,*argv)!=NULL

that is this condition will be always equal to 1 if a line contains the pattern.

Thus if except is equal to 1 and the condition strstr(line,*argv)!=NULL is equal to 1 we should skip the pattern.

Otherwise if the condition strstr(line,*argv)!=NULL is not equal to 1 that is if the pattern is not found then the if statement

if( (strstr(line,*argv)!=NULL) != except)

yields true and its compound statement is executed.

On the other hand if except is equal to 0 then to achieve that the condition in the if statement would evaluate to true we need that the condition strstr(line,*argv)!=NULL would be equal to 1.

In fact you can rewrite the if statement

if( (strstr(line,*argv)!=NULL) != except)

the following way

if( ( ( strstr(line,*argv) != NULL ) == 1 && except == 0 ) ||
    ( ( strstr(line,*argv) != NULL ) == 0 && except == 1 ) )

Shortly speaking the if statement does the work if either

1 and 0

or

0 and 1

If either

1 and 1

or

0 and 0

then the if statement will not be executed.

Here 1 and 0 are results of evaluating of the two sub expressions in the if statement.

like image 120
Vlad from Moscow Avatar answered Sep 21 '22 00:09

Vlad from Moscow