简体   繁体   中英

In C, how do I find if there is a contradicting (negated) literal in a string?

//input: a -b -a. This should contradict as '-a' is negated version of a.
char*check;
for (int i = 0; i < this_element_of_arrayLength; i++) {
     for (int j = 0; j < this_element_of_arrayLength; j++) {

         if (this_element_of_array[i]) == 2) {   //checking if char (a or -a) is of 2 length   
              check = str_replace(this_element_of_array[i], "-", "");
         }

         if (check == this_element_of_array[j]) {
              printf("Contradiction");
              exit(0);
         }    
     }
}

I am essentially trying to check if there are any opposite (negations) of that literal and trying many ways can't seem to get it right. I am making all literals positive and checking each one if there is a match. I've moved things around a lot and have been on this for way too long but nothing seems to be working so my brain is dead. If I delete the first if statement and don't switch them to positive it works

The words in your post (and comments) describe your intentions pretty well, but I was not able to follow some of the code. ( This is where a mcve would have been beneficial. ) So working mostly off your problem descriptions...

Given the descriptions for the input string of potentially negated pairs of atoms (using the descriptions here ) These assumptions/steps are used in the following implementation:

Assumptions:
string literals can be in the form: const char array[] = {"a -b c -d -a"}
atom elements are at most two characters long (for illustration)
a negation occurs when base element type eg a has a corresponding -a

Steps:

  • determine count of elements in collection.
  • create containers for count elements.
  • parse elements from string literal into containers.
  • loop through each element to find corresponding element of different length.

Here is a rough implementation that marks, and keeps count of negation pairs in a collection:

typedef struct {
    char ele[3];
    int len;
    bool negated;
}Ele;

int main(void)
{
    //const char array[] = {"-a -b -c -d -e -a -f -g -f -h -i -J k"};
    //const char array[] = {"a -b c -d e -a f g -f h i J k"};
    //const char array[] = {"a b c d a f g f"};
    const char array[] = {"-b -b -c -c a a"};//per comments

    char *tok = NULL;
    int countNegated = 0;

    int len = strlen(array);

    int count = 0;

    //count elements
   for(int i=0;i<len;i++)
   {
       if(array[i] == ' ') count++;
   }
   count += 1;
   Ele e[count] = {0};//assuming form of either "-a" or "a" with single character 
   //separate array into searchable discrete strings
   int i=0;
   tok = strtok(array, " ");
   while(tok != NULL)
   {
       strcpy(e[i].ele, tok);
       e[i].len = strlen(tok);
       e[i].negated = FALSE;
       tok = strtok(NULL, " ");
       i++;
   }
   //With data encapsulated, walk through elements of collection
   //1-by-1, comparing each to those that are downstream to find 
   //same type but different length elements.  This is done using
   //a single loop, with a branche to accommodate the length of
   //each new element against all of its downstream elements:
   int j=0;
   for(i=0;i<count;i++)
   {
        if(e[i].len == 1)
        {
            for(j=i;j<count;j++)
            {
                if((e[j].len == 2) && strstr(e[j].ele, e[i].ele))
                {
                    countNegated++;
                    e[j].negated = TRUE;
                    e[i].negated = TRUE;
                }       
            }
        }
        else //len == 2)
        {
            for(j=i;j<count;j++)
            {
                if((e[j].len == 1) && strstr(e[i].ele, e[j].ele)) 
                {
                    countNegated++;
                    e[j].negated = TRUE;
                    e[i].negated = TRUE;
                }       

            }
        }
   }



    return 0;
} 

Please leave a comment if this does not address all of your questions.

Per Comments : 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM