ID非公開さん
2022/1/22 18:07
1回答
与えられた英文テキストファイルを読み込み,その英文に含まれている3連語を辞書順に並べ換えて出力するプログラムを作成せよ.ただし,並べ換えに当たっては,単語の頭3文字を考慮し,第4文字以降は順序決定に無
与えられた英文テキストファイルを読み込み,その英文に含まれている3連語を辞書順に並べ換えて出力するプログラムを作成せよ.ただし,並べ換えに当たっては,単語の頭3文字を考慮し,第4文字以降は順序決定に無 関係としてよい. <ヒント> 3連語を一つの構造体で表し,英文から得られた3連語のすべてを構造体の配列に格納する(ただし,文頭・文末の単語は,それぞれ左,右を空白にせよ.各種句切り記号( ,. ”;: - など)は,直前の単語の一部とみなす). ※例えば,与えられた英文が "This is a pen. That is an apple."であるとき,(課題 A_2)で必要とされる処理は具体的にはつぎのようになる. (1)最初の単語は This であり,その前が空白,後の単語が is であるから,第1の3連単語は(空白 This is),第2の3連単語は is を中心に(This is a),第3は(is a pen. )であり,最後は(an apple. 空白)となる. そこで,まず構造体 struct w_triple { char pre[30]; char wrd[30]; char ant[30]; } を定義し,この型 struct w_triple の配列 word[300] を定義する. 与えられた英文から3連語を抽出し,この配列の要素である word[i] に順次代入して行けばよい. (もちろん,構造体の作成法はこの通りにする必要はない.例えば,入力文から抽出した単語(文の切れ目を表す「空白」も含む)を構造体のメンバ word[i].wrd に順番に入れて,word[i].pre, word[i].ant は空のまま,まずは構造体の配列word[ ]を作る.この配列では,文中で前後にある単語が,配列の前後の要素のメンバ word[i-1].wrd, word[i+1].wrd に含まれているので,これらの要素から取り出して word[i].pre, word[i].ant を補充してもよい.) (2)3連語の中央の単語によって,3連語を辞書順に並べ換えるには,(1)で作成した配列 word[300] の要素 word[i] をその第2メンバ word[i].wrd の辞書順に並べ直せばよい. ただし,構造体の配列要素を並べ換える際には,その順序は「単語の頭3文字」のみを考慮するだけでよい」としたが,それよりも短い単語には’空白’を後に付け足して,’空白’はアルファベットより前にあると考えよ.また大文字,小文字の違いは無視せよ. 上記の例文の場合,最終結果は, ( pen. 空白 That ) ( is a pen. ) ( is an apple. ) ( an apple. 空白 ) ・・・・ ( 空白 This is ) となる. この問題のプログラミングを教えてください。 与えられた英文テキストファイルは、次の文字です。 Eight Schools-Looking Toward the 21st Century: Kwansei Gakuin University has eight schools: theology, humanities, sociology, law, economics, business administration, science &technology, and policy studies. One of the characteristics of each school is the freshmen seminar organized with a small group of students. This embodies the policy of the small group education at university. Another practice is the integrated courses, which involve students taking courses outside their own majors. The Freshmen seminar aims for face-to-face communication between students and professors and the integrated courses aim to give students a multidimensional set of viewpoints of things.
ベストアンサー
(ID非公開) よく解らんが...こんな感じでいいのかな... 単語の頭文字が大文字の場合は無条件で文頭とした 例: #include <stdio.h> #include <string.h> #include <ctype.h> typedef struct { char w[3][30]; } WORD; WORD w[300]; int ix=0, is=0; void opeOne(){ ix++; strcpy(w[ix].w[0],w[ix-1].w[1]); strcpy(w[ix].w[1],w[ix-1].w[2]); is=2; } void extract(char *s){ char *p,*q; int len; len=strlen(s); if(s[len-1]=='\n')s[len-1]='\0'; p=q=s; while(q!=NULL){ q=strtok(p," "); p=NULL; if(q != NULL){ if(isupper(*q)){ strcpy(w[ix].w[is],"' '"); is++; if(is>=3)opeOne(); } strcpy(w[ix].w[is],q); is++; if(is>=3)opeOne(); } } } void endOpe(){ if(is != 0){ strcpy(w[ix].w[2],"' '"); is=0; ix++; } } void sort(){ WORD t; int i,j; for(i=0;i<(ix-1);i++){ for(j=i+1;j<ix;j++){ if(strcmp(w[i].w[1],w[j].w[1])>0){ t=w[i]; w[i]=w[j]; w[j]=t; } } } } void dsp(){ int i; for(i=0;i<ix;i++){ printf("(%s %s %s)\n",w[i].w[0],w[i].w[1],w[i].w[2]); } } int main(void) { FILE *fp; char FN[64]; char buf[256]; int n, i; printf("file name >"); scanf("%s", FN); if((fp=fopen(FN,"r"))==NULL){ puts("file open error!!"); return 1; } while(fgets(buf,256,fp)!=NULL){ extract(buf); } endOpe(); fclose(fp); dsp(); sort(); puts("\n--- sorted ---"); dsp(); return 0; } ~~~~~~~~~~~~~~~~~~~~~ ●"This is a pen. That is an apple."の場合 file name >a1.txt (' ' This is) (This is a) (is a pen.) (a pen. ' ') (pen. ' ' That) (' ' That is) (That is an) (is an apple.) (an apple. ' ') --- sorted --- (pen. ' ' That) (' ' That is) (' ' This is) (is a pen.) (is an apple.) (an apple. ' ') (This is a) (That is an) (a pen. ' ') ●英文テキストファイルの場合 file name >abc.txt (' ' Eight ' ') (Eight ' ' Schools-Looking) (' ' Schools-Looking ' ') (Schools-Looking ' ' Toward) (' ' Toward the) . . . (of viewpoints of) (viewpoints of things.) (of things. ' ') --- sorted --- (science &technology, and) (Schools-Looking ' ' Toward) (21st ' ' Century:) (Century: ' ' Kwansei) (Kwansei ' ' Gakuin) (Gakuin ' ' University) (Eight ' ' Schools-Looking) (studies. ' ' One) (students. ' ' This) (university. ' ' Another) (majors. ' ' The) (The ' ' Freshmen) (the 21st ' ') (' ' Another practice) (' ' Century: ' ') (' ' Eight ' ') (' ' Freshmen seminar) (' ' Gakuin ' ') (' ' Kwansei ' ') (' ' One of) (' ' Schools-Looking ' ') (' ' The ' ') (' ' This embodies) (' ' Toward the) (' ' University has) (with a small) (students a multidimensional) (business administration, science) . . . . (of the small) (outside their own) (schools: theology, humanities,) (of things. ' ') (aim to give) (at university. ' ') (of viewpoints of) (courses, which involve) (organized with a)
質問者からのお礼コメント
丁寧に教えていただき、ありがとうございます!
お礼日時:1/28 19:16