在每一行找到每个字符的实例。

huangapple go评论72阅读模式
英文:

Find the instance of each character in every row

问题

以下是您提供的代码的翻译部分:

我们有一个6x6的字符矩阵

X A E I O U
U X A E I O
O U X A E I
I O U X A E
E I O U X A
X Y Z X Y X

我们被要求找出其中一个字符重复最多的行

在我们的例子中,是第6行有3个X。

考虑到我以前做过类似的事情,我的想法是使用```链表(Linked Lists)```

基本思路如下(至少是我心中的想法):

我们建立一个结构,包含字符、实例数和它所属的行。

然后,我们构建一个```while```循环,逐个字符地扫描并检查该行是否已存储字符。如果是,则增加计数,否则存储它并初始化计数为1。

这是我的代码:
```c
#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int row;
    int count;
    char c;
    struct Node *next;
} node;

node *createNode(char *name)
{
    FILE *file;
    file = fopen(name, "r");
    if (file == NULL)
    {
        printf("打开文件出错....");
        exit(1);
    }
    int n = 6;
    char str[6][6];
    char c;
    node *head = NULL;
    int i = 0;
    int j = 0;
    while (fscanf(file, "%c ", &str[i][j]) != EOF)
    {
        int found = 0;
        int r = 0;
        node *p = head;
        while (p)
        {
            if (p->c == str[i][j] && p->row == i)
            {
                found = 1;
                break;
            }
            p = p->next;
        }
        if (found)
        {
            p->count++;
        }
        else
        {
            node *tmp = malloc(sizeof(node));
            if (tmp == NULL)
            {
                printf("分配内存出错!");
                exit(1);
            }
            tmp->c = str[i][j];
            tmp->row = i;
            tmp->count = 1;

            tmp->next = head;
            head = tmp;
        }
        i++;
        if (i == 6)
        {
            i = 0;
            j++;
        }
    }
    fclose(file);
    return head;
}

void printList(node *head)
{
    node *tmp = head;
    int i = 1;
    while (tmp != NULL)
    {
        printf("节点 %d:\n字符->%c\n行->%d\n计数->%d\n\n", i, tmp->c, tmp->row, tmp->count);
        i++;
        tmp = tmp->next;
    }
}

int main()
{
    char name[] = "file";
    node *head = NULL;
    head = createNode(name);
    printList(head);

    return 0;
}

请注意,此处仅提供代码的翻译部分,不包括解释或其他内容。如果您有其他问题或需要进一步的帮助,请随时提出。

英文:

We are given a 6x6 matrix of characters:

X A E I O U
U X A E I O
O U X A E I
I O U X A E
E I O U X A
X Y Z X Y X

And we are asked the find the most row where a character repeats the most

In our case is row 6 with 3 X's.

My idea considering i've done something similar in the past was to use Linked Lists

The idea basically goes like this(at least what i had in mind):

We build a structure containing the character, the number of instances and the row it belongs to.

Then we build a while loop scanning every character one by one and then checking if in this row the character is stored or not. If yes then we increment else we store it and initialize the count to 1

This is my code :

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

typedef struct Node
{
    int row;
    int count;
    char c;
    struct Node *next;
}node;

node *createNode(char *name)
{
    FILE *file;
    file= fopen(name,&quot;r&quot;);
    if(file==NULL)
    {
        printf(&quot;Error opening file....&quot;);
        exit(1);
    }
    int n=6;
    char str[6][6];
    char c;
    node *head=NULL;
    int i=0;
    int j=0;
    while(fscanf(file,&quot;%c &quot;,&amp;str[i][j])!=EOF)
    {
        int found=0;
        int r=0;
        node *p=head;
        while (p)
        {
            if(p-&gt;c==str[i][j] &amp;&amp; p-&gt;row==i)
            {
                found=1;
                break;
            }
        }
        if(found)
        {
            p-&gt;count++;
        }
        else
        {
            node *tmp= malloc(sizeof (node));
            if(tmp==NULL)
            {
                printf(&quot;Error allocating memory!&quot;);
                exit(1);
            }
            tmp-&gt;c=str[i][j];
            tmp-&gt;row=i;
            tmp-&gt;count=1;

            tmp-&gt;next=head;
            head=tmp;
        }
        i++;
        if(i==6)
        {
            i=0;
            j++;
        }
    }
    fclose(file);
    return head;
}

void printList(node *head)
{
    node *tmp=head;
    int i=1;
    while(tmp != NULL)
    {
        printf(&quot;Node %d:\nchar-&gt;%c\nrow-&gt;%d\ncount-&gt;%d\n\n&quot;,i,tmp-&gt;c,tmp-&gt;row,tmp-&gt;count);
        i++;
        tmp=tmp-&gt;next;
    }
}

int main() {

    char name[]={&quot;file&quot;};
    node *head=NULL;
    head=createNode(name);
    printList(head);

    return 0;
}

It seems correct to me at least but it doesnt print anything. Im just printing for now to see if its correct, Ill add the other stuff later

答案1

得分: 1

你的程序之所以不打印任何内容,是因为它陷入了无限循环。

while (p)
{
    if (p->c == str[i][j] && p->row == i)
    {
        found = 1;
        break;
    }
}

你从未让p遍历整个列表。
你需要添加 p = p->next;

英文:

Your program doesn't print anything because it gets stuck in an infinite loop.

while (p)
{
    if(p-&gt;c==str[i][j] &amp;&amp; p-&gt;row==i)
    {
        found=1;
        break;
    }
}

You never actually make p loop through the list.
You need to add p = p-&gt;next;

huangapple
  • 本文由 发表于 2023年5月13日 09:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定