/* * @(#) msu97_c.c - Problem 'C' ("The Sentence(TM) Text Processor") * solution of the ACM Programming Contest at the MSU in 1997. * (c) 1998 Ivan Maidanski http://ivmai.chat.ru * Freeware program source. All rights reserved. ** * Language: ANSI C * Tested with: Borland C++ v3.1 * Last modified: 1998-04-02 15:40:00 GMT+04:00 */ /* Input data file: c.dat */ #include /* FILE, SEEK_BEG, fopen(), ftell(), fseek() */ /* fscanf(), ungetc(), printf() */ #include /* isalnum() */ #include /* memmove(), strcpy(), strcat(), strcmp(), strlen() */ FILE *f; char curword[100],curstr[100]; int get_sym(void) { char c; if (fscanf(f,"%c",&c)<0) return -1; if (c>' ') return c; if (c!='\n') do if (fscanf(f,"%c",&c)<0) return -1; while (c<=' ' && c!='\n'); if (c=='\n') { do if (fscanf(f,"%c",&c)<0) return -1; while (c<=' ' && c!='\n'); } if (isalnum(c)) { ungetc(c,f); return ' '; } if (c>' ') return c; if (fscanf(f,"%*[\0- ]")<0) return -1; return '\n'; } int get_lexem(char *w) { int i=0; while ((w[i]=get_sym())=='-') i++; if (!i) { while (w[i]=='(') w[++i]=get_sym(); while (isalnum(w[i])) w[++i]=get_sym(); while (w[i]!=-1 && w[i]!='\n' && w[i]!=' ' && w[i]!='(' && !isalnum(w[i])) w[++i]=get_sym(); } if (w[i]==-1) { w[i]='\0'; return 1; } if (w[i]>' ') ungetc(w[i],f); else if (w[i]=='\n') i++; w[i]='\0'; return 0; } void out_str(char *s, int leftpos, int rightpos, int identpos, int *begpara, int lastline) { int i,cnt; if (!lastline) { cnt=rightpos-leftpos-(*begpara? identpos : 0)-strlen(s); while (cnt>0) for (i=0;s[i];i++) if (s[i]==' ') { memmove((void *)&s[i+1],(void *)&s[i],strlen(&s[i])+1); if (!--cnt) break; while (s[++i]==' '); } } printf("%*s%s\n",leftpos-1+(*begpara? identpos : 0),"",s); s[0]='\0'; if (lastline) printf("\n"); *begpara=lastline; } void add_word(char *s, char *w, int leftpos, int rightpos, int identpos, int *begpara) { if (strlen(s)+strlen(w)+1>rightpos-leftpos-(*begpara? identpos : 0)) out_str(s,leftpos,rightpos,identpos,begpara,0); else if (strlen(s)>0) strcat(s," "); strcat(s,w); } int main(void) { int leftpos,rightpos,identpos; int testcnt,paracnt,wordcnt,punctcnt; int newline,begpara; long pos; f=fopen("c.dat","rt"); fscanf(f,"%*[^0-9]%d",&testcnt); while (testcnt--) { fscanf(f,"%*[^0-9]%d%*[^0-9]%d%*[^0-9]%d%*[^A-Za-z0-9.,:;!?()-]", &leftpos,&rightpos,&identpos); pos=ftell(f); for (paracnt=1,wordcnt=punctcnt=0,newline=2;;) { fscanf(f,"%*[^\nA-Za-z0-9.,:;!?()-]%c",&curword[0]); if (isalnum(curword[0])) { fscanf(f,"%[A-Za-z0-9]",&curword[1]); if (strcmp(curword,"ENDOFTEXT")) wordcnt++; else break; newline=0; } else if (curword[0]!='\n') { punctcnt++; newline=0; } else if (newline++) { fscanf(f,"%*[^A-Za-z0-9.,:;!?()-]"); paracnt++; } } if (newline>1) paracnt--; printf("-----\n" "The text contains %d paragraphs," " %d words, %d punctuation marks.\n\n", paracnt,wordcnt,punctcnt); fseek(f,pos,SEEK_SET); for (begpara=1,curstr[0]='\0';;) { if (get_lexem(&curword[0]) || !strcmp(&curword[0],"ENDOFTEXT")) { if (curstr[0]!='\0') out_str(&curstr[0],leftpos,rightpos,identpos,&begpara,1); break; } if (curword[strlen(curword)-1]!='\n') add_word(&curstr[0],&curword[0],leftpos,rightpos,identpos,&begpara); else { curword[strlen(curword)-1]='\0'; add_word(&curstr[0],&curword[0],leftpos,rightpos,identpos,&begpara); out_str(&curstr[0],leftpos,rightpos,identpos,&begpara,1); } } } printf("\n"); return 0; }