Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的结构和数组挑战_C_Arrays_Struct - Fatal编程技术网

C中的结构和数组挑战

C中的结构和数组挑战,c,arrays,struct,C,Arrays,Struct,我正在努力解决这一挑战: 基本上,我得到了一个带有结构的锁定代码存根,我应该解析给定的文本。这是我的代码的节略版本: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #define MAX_CHARACTERS 1005 #define MAX_PARAGRAPHS 5 #include <ctype.h> struct

我正在努力解决这一挑战:

基本上,我得到了一个带有结构的锁定代码存根,我应该解析给定的文本。这是我的代码的节略版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5

#include <ctype.h>

struct word {
    char* data;
};

struct sentence {
    struct word* data;
    int word_count;//denotes number of words in a sentence
};

struct paragraph {
    struct sentence* data  ;
    int sentence_count;//denotes number of sentences in a paragraph
};

struct document {
    struct paragraph* data;
    int paragraph_count;//denotes number of paragraphs in a document
};

struct document get_document(char* text) {
    int spaces = 0, periods = 0, newlines = 0;
    for(int i = 0; i < strlen(text); i++) 
        if(text[i] == ' ')
            spaces++;
        else if(text[i] == '.')
            periods++;
        else if(text[i] == '\n')
            newlines++;


    struct document doc;
    doc.paragraph_count = newlines + 1;
    doc.data = malloc((newlines + 1) * sizeof(struct paragraph));

    struct paragraph para[doc.paragraph_count];
    for(int i = 0; i < doc.paragraph_count; i++) {
        para[i].sentence_count = periods + 1;
        para[i].data = malloc((periods + 1) * sizeof(struct sentence));
    }

    struct sentence sen[para[0].sentence_count];
    for(int i = 0; i < para[0].sentence_count; i++) {
        sen[i].word_count = spaces + 1;
        sen[i].data = malloc((spaces + 1) * sizeof(struct word));
    }

    struct word word[spaces + periods + 1];

    int start = 0, k = 0, wordsub = 0, sensub = 0, parasub = 0, docsub = 0, wordno = 0, parano = 0;
    for(int i = 0; i < strlen(text); i++) {
        if(text[i] == ' ' || text[i] == '.') {
            word[wordsub].data = malloc((i - start) * sizeof(char) + 1);
            for(int j = start; j < i; j++)
                word[wordsub].data[k++] = text[j];
            word[wordsub].data[k++] = '\0';

            k = 0;

            if(i < strlen(text) - 1 && text[i + 1] == '\n')
                start = i + 2;
            else 
                start = i + 1;

            if(text[i] == ' ') {
                sen[sensub].data[wordno++] = word[wordsub++]; //wordno can be 0 or 1
            }
            if(i != strlen(text) && isalpha(text[i + 1]) && text[i] == '.') {
                sen[sensub].data[wordno++] = word[wordsub++];
                wordno = 0;
                para[parasub].data[parano++] = sen[sensub++];


            }
            if((i != strlen(text) && text[i + 1] == '\n') || i + 1 == strlen(text)) {
                sen[sensub++].data[wordno++] = word[wordsub];
                wordno = 0;

                parano = 0;
                para[parasub].data[parano++] = sen[sensub];

                doc.data[docsub++] = para[parasub++];


            }

        }
    }
    printf("%s\n", para[0].data[0].data[0].data);// should print "hello"
    return doc;
}

int main() {
    struct document doc;
    char * text = "hello world.\nhi.bye.\nwow.";
    doc = get_document(text);
    printf("%s\n", doc.data[0].data[0].data[0].data);//should also print "hello"
}
#包括
#包括
#包括
#包括
#定义最大字符数1005
#定义第5段中的最大值
#包括
结构字{
字符*数据;
};
结构句{
结构字*数据;
int word_count;//表示句子中的字数
};
结构段落{
结构句子*数据;
int SEQUANCE_count;//表示段落中的句子数
};
结构文档{
结构段*数据;
int paragration_count;//表示文档中的段落数
};
结构文档获取文档(字符*文本){
int空格=0,句点=0,换行符=0;
for(int i=0;i
问题是print语句没有打印“hello”。此外,如果我更改打印语句中的索引,则会出现分段错误。

此处:

word[wordsub].data[k++] = text[j];

您正在使用分配的内存访问数据成员。

问题陈述指定一个字后永远不会有两个终止符。至少应该有一个词

那么,测试短语呢

"hello world.\nhi.bye.\nwow."
不合适,但是

"hello world\nhi.bye\nwow"
适合你,你会有“你好”打印

此外,您的算法非常复杂,而代码可能更简单。尝试很有趣,我做到了

首先,让我们使用一些
typedef
来写更少的文本

typedef struct word {
    char* data;
} W;

typedef struct sentence {
    W* data;
    int word_count;//denotes number of words in a sentence
} S;

typedef struct paragraph {
    S* data  ;
    int sentence_count;//denotes number of sentences in a paragraph
} P;

typedef struct document {
    P* data;
    int paragraph_count;//denotes number of paragraphs in a document
} DOC;
然后是函数本身。逻辑很简单,按顺序对
text
的每个字符执行以下操作

  • 如果我们有任何分隔符(
    '
    '.
    '\n'
    )请记录单词
  • 如果我们有一个分隔符(
    。\n'
    )记录句子
  • 如果我们有分隔符(
    '\n'
    ),请记录一个段落
字符串的结尾视为段落的结尾

代码

struct document get_document(字符*文本){
DOC DOC={NULL,0};//你就是这个DOC,DOC
P parr={NULL,0};
S sarr={NULL,0};
int wpos=0;

对于(inti=0,l=strlen(text);iSorry,编辑了它。现在它打印“hi”,但这仍然不是正确的输出。
struct document get_document(char* text) {
     DOC doc = { NULL, 0 }; // you're the doc, doc
     P parr  = { NULL, 0 };
     S sarr  = { NULL, 0 };

     int wpos=0;

     for(int i=0, l=strlen(text) ; i<=l ; i++) { // <= length! (to deal with \0)
          char c = text[i];
          if ( ! c) c = '\n'; // End of string simulates end of paragraph

          if (c == '\n' || c == '.' || c == ' ') {
                // End of word, add it to sentence
                W word;
                word.data = malloc(i - wpos + 1);          // +1 for '\0'
                strncpy(word.data, text + wpos, i - wpos); // Copy only the word
                word.data[i - wpos] = 0;                   // 0 terminate it
                sarr.data = realloc(sarr.data, sizeof(W) * (sarr.word_count+1));
                sarr.data[ sarr.word_count++ ] = word;
                wpos = i+1;

                if (c == '\n' || c == '.') {
                     // End of sentence, add it to paragraph
                     parr.data = realloc(parr.data, sizeof(S) * (parr.sentence_count+1));
                     parr.data[ parr.sentence_count++ ] = sarr;
                     sarr.data = NULL;    // clear sentences
                     sarr.word_count = 0;
                }

                if (c == '\n') {
                     // End of paragraph, add it to doc
                     doc.data = realloc(doc.data, sizeof(P) * (doc.paragraph_count+1));
                     doc.data[ doc.paragraph_count++ ] = parr;
                     parr.data = NULL;     // clear paragraphs
                     parr.sentence_count = 0;
                }
          }
     }

    return doc;
}
int main(int argc, char **argv) {
    DOC doc;
    char * text = "hello world\nhi.bye\nwow";

    doc = get_document(text);

    for(int i=0 ; i<doc.paragraph_count ; i++) {
          printf("Para %d / %d\n", i, doc.paragraph_count-1);
          P para = doc.data[i];
          for(int j=0 ; j<para.sentence_count ; j++) {
                printf("Sent %d / %d\n", j, para.sentence_count-1);
                S sent = para.data[j];
                for(int k=0 ; k<sent.word_count ; k++) {
                     W word = sent.data[k];
                     printf("Word %d / %d: %s\n", k, sent.word_count-1, word.data);
                }
          }
     }

     return 0;
}