-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_doc_soln.c
More file actions
278 lines (228 loc) · 7.11 KB
/
Copy pathstructured_doc_soln.c
File metadata and controls
278 lines (228 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5
#define dprint(...) do { printf("[%d] ",__LINE__); printf(__VA_ARGS__); } while(0)
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
};
/* Beginning of my code */
int count_paragraphs ( char * text ) {
int k, pcnt=0, slen = strlen(text);
for ( k=0; k<slen; ++k ) {
if ( '\n'==text[k] ) {
pcnt++;
}
}
return pcnt+2;
}
int count_sentences ( char * text ) {
int k, scnt=0, slen=strlen(text);
if ( slen<1 ) {
return 0;
}
for ( k=0; k<slen; ++k ) {
if ( '.'==text[k] ) {
scnt++;
}
}
return scnt;
}
int count_words ( char * text ) {
int k;
int wcnt = 0, slen = strlen(text);
for ( k=0; k<slen; ++k ) {
if ( ' '==text[k] ) {
wcnt++;
}
}
return wcnt+1; /* There is no space after the last word */
}
void get_sentence ( struct sentence * sent, char * text ) {
int k;
int wcnt = count_words(text),
slen = strlen(text);
char word[1024];
char * new_word;
int widx, sidx=0;
/* Allocate memory for each word */
sent->data = calloc(wcnt,sizeof(struct word));
sent->word_count = wcnt;
/* Get each word in the sentence */
for ( k=0; k<wcnt; ++k ) {
memset(word,0,1024); /* Zero the word */
widx=0; /* Start at the beginning of a word */
while ( sidx<slen ) {
if ( ' '==text[sidx] || '\0'==text[sidx+1] ) {
if ( '\0'==text[sidx+1] ) {
word[widx++] = text[sidx];
}
/* Allocate each word */
sent->data[k].data= calloc(1,strlen(word)+1);
snprintf(sent->data[k].data,strlen(word)+1,"%s",word);
sidx++;
break;
}
word[widx++] = text[sidx];
sidx++;
}
}
}
void get_paragraph ( struct paragraph * para, char * text ) {
int k; /* Dummy */
int scnt = count_sentences(text); /* Get the number of sentences in paragraph */
char sent[1024]; /* The current sentence */
int sidx, tidx=0; /* The sentence and text index */
/* Allocate the sentence data structures */
para->data = calloc(scnt,sizeof(struct sentence));
para->sentence_count = scnt;
/* Get the sentences */
for ( k=0; k<scnt; ++k ) {
memset(sent,0,1024); /* Zero out the sentence */
sidx = 0; /* Start at the beginning of hte sentence */
while ( tidx<strlen(text) ) {
if ( '.'==text[tidx] ) {
get_sentence(¶->data[k],sent);
tidx++;
break;
}
sent[sidx++] = text[tidx];
tidx++;
}
}
return;
}
struct document get_document(char* text) {
int k, /* Dummy */
pidx,tidx=0, /* Paragraph and text index */
tlen=strlen(text);; /* Text length */
char para[1024]; /* Paragraph text */
struct document ret; /* Document */
/* Get the number of paragraphs in a document */
ret.paragraph_count = count_paragraphs(text);
/* Allocate the number of paragraphs for the document */
ret.data = (struct paragraph*)calloc(2,sizeof(struct paragraph));
/* Get the paragraphs */
for ( k=0; k<ret.paragraph_count && tidx<tlen; ++k ) {
memset(para,0,1024); /* Zero out the sentence */
pidx=0; /* Start at the beginning of the sentence */
while ( 1 ) {
/* A return or end of text indicates the end of a paragraph */
if ( '\n'==text[tidx] || tlen-1==tidx ) {
//dprint("para %d = %s\n",k,para);
if ( tlen-1==tidx ) {
para[pidx++] = text[tidx];
}
get_paragraph(&ret.data[k],para);
tidx++; /* Make sure to skip the return character */
break; /* Get the next sentence or processing finished */
}
para[pidx++] = text[tidx]; /* Capture the next character of the sentence */
tidx++;
}
}
return ret;
}
struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc, int k, int m, int n) {
struct word ret = Doc.data[n-1].data[m-1].data[k-1];
return ret;
}
struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) {
struct sentence ret = Doc.data[m-1].data[k-1];
return ret;
}
struct paragraph kth_paragraph(struct document Doc, int k) {
struct paragraph ret = Doc.data[k-1];
return ret;
}
/* End of my code */
void print_word(struct word w) {
printf("%s", w.data);
}
void print_sentence(struct sentence sen) {
for(int i = 0; i < sen.word_count; i++) {
print_word(sen.data[i]);
if (i != sen.word_count - 1) {
printf(" ");
}
}
}
void print_paragraph(struct paragraph para) {
for(int i = 0; i < para.sentence_count; i++){
print_sentence(para.data[i]);
printf(".");
}
}
void print_document(struct document doc) {
for(int i = 0; i < doc.paragraph_count; i++) {
print_paragraph(doc.data[i]);
if (i != doc.paragraph_count - 1)
printf("\n");
}
}
char* get_input_text() {
int paragraph_count;
scanf("%d", ¶graph_count);
char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
memset(doc, 0, sizeof(doc));
getchar();
for (int i = 0; i < paragraph_count; i++) {
scanf("%[^\n]%*c", p[i]);
strcat(doc, p[i]);
if (i != paragraph_count - 1)
strcat(doc, "\n");
}
char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
strcpy(returnDoc, doc);
return returnDoc;
}
int main()
{
char* text = get_input_text();
struct document Doc = get_document(text);
/*
printf("-------------------------\n");
print_document(Doc);
printf("\n");
printf("-------------------------\n");
*/
int q;
scanf("%d", &q);
while (q--) {
int type;
scanf("%d", &type);
if (type == 3){
int k, m, n;
scanf("%d %d %d", &k, &m, &n);
struct word w = kth_word_in_mth_sentence_of_nth_paragraph(Doc, k, m, n);
print_word(w);
}
else if (type == 2) {
int k, m;
scanf("%d %d", &k, &m);
struct sentence sen= kth_sentence_in_mth_paragraph(Doc, k, m);
print_sentence(sen);
}
else{
int k;
scanf("%d", &k);
struct paragraph para = kth_paragraph(Doc, k);
print_paragraph(para);
}
printf("\n");
}
}