列表元素,而不是列C [英] List element instead of array C

查看:139
本文介绍了列表元素,而不是列C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完美的作品的功能。用户键入词和关键词。在关键字代替一些字母它可以用于'?'这意味着任何随机字母,'*'末和[X,Y,Z]这意味着信可能是X或Y或Z,如果那么功能检查的话比赛意味着任何字母随机量。下面是它的样子:

I have a function that works perfectly. The user types the word and the keyword. In the keyword instead some letters it can be used '?' meaning any random letter, '*' meaning any random amount of letters at the end and [x,y,z] meaning the letter could be x or y or z, then the functions checks if the words match. Here's how it looks like:

int MatchWord(char *Word, char *Sequence)

{

int i = 0;
int j = 0; 
int k = 0;
int LastChar = 0;
int CharMatch = 0;
char SpecifiedChars[20];
while(Word[i]!='\0' && Sequence[j]!='\0')
{if(isalpha(Sequence[j]))
    {
        if(Word[i]!=Sequence[j])
        {return 0;}
        i++;
        j++;
    }

    if(Sequence[j] == '?')
    {
        i++;
        j++;
    }

    if(Sequence[j] == '[')

    {

        j++;
        while(Sequence[j]!= ']')

        {
            if(isalpha(Sequence[j]))
            {
                SpecifiedChars[LastChar] = Sequence[j];
                LastChar++;
                j++;
            }
            else
            {j++;}
        }
        j++;
        for(k = 0 ; k <= LastChar ;k++)
        {
            if(SpecifiedChars[k]==Word[i])
            {CharMatch = 1;}
            SpecifiedChars[k] = ' ';
        }
        SpecifiedChars[0] = '\0';
        LastChar = 0;
        if(!CharMatch)
        {return 0;}
        i++;
    }
    if(Sequence[j] == '*')
    {
        j++;
        while(Word[i]!='\0')
        {i++;}
    }
}
return 1;
}


int main()
{
char word[30], keyword[30];
printf("Type the word: \n");
scanf("%s",word);
printf("Type the key: \n");
scanf("%s",keyword);
if(MatchWord(word,keyword))
{
    printf("\nWords match");
}
else
{
    printf("\nWords don't match");
}
return 0;
   }

不过,我必须改变它的,而不是让用户键入的第一个字,它会检查文件中的txt基地的话我有。他们在结构:

But I have to change it that instead of letting user type the first word, it checks the words in base I have in file txt. They're in structure:

typedef struct bazaslowek                                
    {
        char *word1;
        char *category;
        struct bazaslowek* next;
    } baza;

这就是我如何把他们的名单,这也完美的作品上:

And that's how I put them on the list, which also works perfectly:

char word1[30];
char category[20];
FILE *fp;
if ((fp = fopen("bazaslow.txt", "r"))==NULL)
    {printf("Error!");
    exit(EXIT_FAILURE);}
else
    {
    while(!feof(fp))
        {
        fscanf(fp,"%s %s \n", word1, category);
        baza *wsk = *head;
        baza *new = malloc (sizeof(baza));
        new -> next = NULL;
        new -> word1 = strdup(word1);
        new -> category = strdup(category);
        if(wsk == NULL)
            {
            new -> next = *head;
            *head = new;
            }
        else
            {
            while(wsk -> next != NULL)
            wsk = wsk -> next;
            wsk -> next = new;
            }
        }
    }
fclose(fp);

我试图把一切都在一个循环,使而 wsk-&GT;!下次= NULL 则检查关键字匹配字1 ,如果是的,它用printfs它并检查一个字,如果不是它只是给名单上的另一个词不printf的。可悲的是我惨遭失败,在这里,因为它通常不会在所有printf的任何东西,或用printfs所有的话,如果他们匹配与否并不重要。有谁告诉我应该怎么样子吗?

I tried putting everything in a loop so while wsk->next!=NULL it checks if the keyword matches the word1, if yes it printfs it and checks another word, if not it just goes to the another word on the list without printf. Sadly I fail miserably here, as it usually doesn't printf anything at all or printfs all the words, doesn't matter if they match or not. Could anybody tell me how should it look like please?

推荐答案

您需要展示如何利用新的,WSK和头部被定义。此外,这将有助于给变量WSK一个有意义的名字。我想你使用通过你的列表来遍历code可能有帮助。

You need to show how new, wsk and head are defined. Also, it would help to give the variable wsk a meaningful name. I think the code you use to iterate through your list could be helpful as well.

最后,这里是如何我通常去填补链表:

Finally, here's how I usually go about filling up linked list:

baza *head = NULL, *tail, *new;
while(whatever condition) {
    new = malloc(sizeof(baza);
    new->next = NULL;
    new->word1 = strdrup(word1);
    new->category = strdrup(category);

    if(head) {
      tail->next = new;
      tail = new;
    } else { //first element, init head, tail
      head = tail = new;
    }
}

在此您可以通过列表从走路开始头

After this you can walk through your list starting from head.

总之,我认为你需要在你的变量命名,压痕和排版工作。你也应该做一些错误检查。例如,你可以使用的fscanf的返回值,看看有多少任务作了:而(的fscanf(blabl)== 2){}

All together, I think you need to work on your variable naming, indentation and typography. You should also do some error checking. You could for example use the return value of fscanf to see how many assignments were made: while(fscanf(blabl) == 2) {}

这篇关于列表元素,而不是列C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆