Binary Operations


General Statement: Read 2 lists of integer elements. Determine the results of the binary operations AND, OR, XOR, and NOR.

Input: The first line of data is list A, and the second line is list B. There are an unknown number of integers in each list. A –1 is used to indicate the end of each list. The next line is an integer that represents the number of operators that follow. Each operator is on a separate line.

Output: Output A, followed by the operator, followed by B =, and then the result with these numbers in ascending order. If an answer is “empty”, output the word NULL.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: The master set is the range 0..10.
Each input list contains at least 1 element. There are no duplicates within a list.

Discussion:
AND yields all elements occurring in both lists.
OR yields all elements occurring in either list.
XOR yields all elements occurring in either but not both lists.
NOR yields all elements in the master set not in either list.

Sample Input:
5 6 9 3 8 –1
2 3 1 5 4 9 6 8 –1
4
AND
OR
NOR
XOR

Sample Output:
A AND B = 3 5 6 8 9
A OR B = 1 2 3 4 5 6 8 9
A NOR B = 0 7
A XOR B = 1 2 4

Solutions :

#include <stdio.h>
#include <string.h>

int a [12];
int a_index = 0;
int b [12];
int b_index = 0;

void And ()
{
    printf ("A AND B =");

    bool flag = true;
    int temp [10] = {0};

    for ( int i = 0; i < a_index; i++ )
        temp [a [i]]++;

    for ( int i = 0; i < b_index; i++ )
        temp [b [i]]++;

    for ( int i = 0; i < 10; i++ ) {
        if ( temp [i] == 2 ) {
            printf (" %d", i);
            flag = false;
        }
    }

    if ( flag )
        printf (" NULL");

    printf ("\n");
}

void Or ()
{
    printf ("A OR B =");

    bool flag = true;
    int temp [10] = {0};

    for ( int i = 0; i < a_index; i++ )
        temp [a [i]]++;

    for ( int i = 0; i < b_index; i++ )
        temp [b [i]]++;

    for ( int i = 0; i < 10; i++ ) {
        if ( temp [i] > 0 ) {
            printf (" %d", i);
            flag = false;
        }
    }

    if ( flag )
        printf (" NULL");

    printf ("\n");
}

void Nor ()
{
    printf ("A Nor B =");

    bool flag = true;
    int temp [10] = {0};

    for ( int i = 0; i < a_index; i++ )
        temp [a [i]]++;

    for ( int i = 0; i < b_index; i++ )
        temp [b [i]]++;

    for ( int i = 0; i < 10; i++ ) {
        if ( temp [i] == 0 ) {
            printf (" %d", i);
            flag = false;
        }
    }

    if ( flag )
        printf (" NULL");

    printf ("\n");
}

void Xor ()
{
    printf ("A Xor B =");

    bool flag = true;
    int temp [10] = {0};

    for ( int i = 0; i < a_index; i++ )
        temp [a [i]]++;

    for ( int i = 0; i < b_index; i++ )
        temp [b [i]]++;

    for ( int i = 0; i < 10; i++ ) {
        if ( temp [i] == 1 ) {
            printf (" %d", i);
            flag = false;
        }
    }

    if ( flag )
        printf (" NULL");

    printf ("\n");
}

int main ()
{
    scanf ("%d", &a [a_index]);
    while ( a [a_index++] != -1 )
        scanf ("%d", &a [a_index]);

    scanf ("%d", &b [b_index]);
    while ( b [b_index++] != -1 )
        scanf ("%d", &b [b_index]);

    int dataset;
    scanf ("%d", &dataset);

    while ( dataset-- ) {
        char operation [5];
        scanf ("%s", operation);

        if ( !strcmp (operation, "AND") )
            And ();

        else if ( !strcmp (operation, "OR") )
            Or ();

        else if ( !strcmp (operation, "NOR") )
            Nor ();

        else
            Xor ();
    }

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s