0

Here is a program I have build :

int mynand(int a, int b);
int mynor(int a, int b);
int myxor(int a, int b);
int report(CallBack f , char *gates);
int main( )
{
    CallBack myfunctions[] = {myand, myor, myxor, mynand, mynor};
    char gatename[N][9+1] = {"AndGate" , "OrGate" , "XorGate" , "NandGate" , "NorGate"};
    int i;
    int size = sizeof(myfunctions)/sizeof(CallBack);
    Name temp[N];

    for(i=0; i<size; i++)
    {
        strcpy(temp[i].gates , gatename[i]);
        temp[i].gatename=myfunctions[i];
    }

    for(i=0; i<size; i++)
    {
        printf("\n%s\n" , temp[i].gates);
        report(temp[i].gatename,temp[i].gates);
    }


    return 0;
}
int myand (int a, int b)
{
    return a * b;
}

int myor (int a, int b)
{
    return a + b>0;
}

int mynand (int a, int b)
{
    return !(a * b);
}

int mynor (int a, int b)
{
    return !(a + b);
}

int myxor (int a, int b)
{
    return a*(!(a*b)) + b*(!(a*b));
}

int report(CallBack f , char * gates)
{
    int i , j;

    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
           printf("%d %d %d\n", i , j , f(i ,j));
        }
    }
        printf("\n");

    return 0;
}

This program it has as an output :

AND     
0 0 0      
0 1 0     
1 0 0                 
1 1 1


OR     
0 0 0       
0 1 1          
1 0 1           
1 1 1


XOR              
0 0 0               
0 1 1           
1 0 1               
1 1 0


NAND            
0 0 1                
0 1 1                
1 0 1              
1 1 0


NOR              
0 0 1                  
0 1 0                      
1 0 0              
1 1 0

2)Ι want to modify the functions myand , myor , etc. In order to take for an input simple linked list with type Data elements and prove that it works properly by writing the right code in main. Here the given statements:


typedef struct data {

int value;

struct data * next; }                    
Data;                                
typedef Data * DataList;                                     
int myandlst(DataList );                        
Data * createData( int value) {                    
Data * dataptr;                            
dataptr = malloc(sizeof (Data)); dataptr->value = value;                         
dataptr->next = NULL;                                   
return dataptr;
}

void appendData(DataList *lstptr, Data *newptr) {                             
if (*lstptr==NULL) {                                      
*lstptr = newptr;                              
return;               
}

appendData( &((*lstptr)->next), newptr);

return;
}

  • In `appendData`, it would be better to use iteration to find the end of the list, not tail recursion. Some compilers can optimize tail recursion to a simple iteration loop, but that is by no means guaranteed. It is pretty easy to do the iteration explicitly: `while ((*lstptr) {` `lstptr = &(*lstptr)->next;` `}` `*lstptr = newptr;`. – Ian Abbott May 24 '21 at 13:24
  • How is the result of the XOR operation to be defined for more than 2 inputs? Is it just the parity of the inputs as would result from chaining the operations together, e.g. XOR(a,b,c) = XOR(XOR(a,b),c)? – Ian Abbott May 24 '21 at 13:35
  • The second code I have attached is given from the exercise that I found on the Web. It is not my code. – C-Programmer May 28 '21 at 18:06

0 Answers0