• 締切済み

ハッシュ法が得意な人お願いします。

辞書検索プログラムが実行できません。どこがおかしいか教えてください。 #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUCKET_SIZE 100 typedef struct pointer { struct cell *chain; }pointer; struct cell{ char eng[20]; char jp[40]; struct cell *next; } table[BUCKET_SIZE]; int n; void read_dic(); void init_table(); int hash(char *tango); void main(); struct cell *find(char *tango); struct pointer bucket[BUCKET_SIZE]; void init_table() { int i; for (i=0;i<BUCKET_SIZE;i++){ strcpy(table[i].eng,"0"); strcpy(table[i].jp,"0"); } } void main() { int m; struct cell *p; char tango[20]; read_dic(); while(1){ printf("\n単語を入力: "); scanf("%s", tango); if (strcmp(tango, "*") == 0){ printf("検索を終了\n"); exit(0); } if((p= find(tango)) == NULL) printf("単語は見つかりませんでした\n"); else printf("訳語: %s \n", table[p].jp); } } まだ続きます。。。

  • ya-co
  • お礼率16% (1/6)

みんなの回答

回答No.1

管理者より: 続きの質問があるのでそちらをご参照下さい

参考URL:
http://www.okweb.ne.jp/kotaeru.php3?q=109206

関連するQ&A

  • 辞書検索プログラムの続きです。

    void read_dic() { char eng2[20], jp2[40]; FILE *fp; int i=0; if((fp=fopen("dic.txt", "r"))==NULL) { printf("\n ファイルがありません\n"); exit(1); } else { printf("読み込み中...\n"); while (!feof(fp)) { fscanf(fp, "%s %s", eng2, jp2); strcpy(table[i].eng, eng2); strcpy(table[i].jp, jp2); i++; if(i>=100){ printf("辞書のテーブルがいっぱいです\n"); fclose(fp); exit(1); } } fclose(fp); printf("読み込み終了\n"); n = i; } } int hash(char *tango) { int h=0,p=256; while (*tango!='\0') { h = h*p + *tango; h = h%BUCKET_SIZE; tango++; } return(h); } struct cell *find(char *tango) { int a; struct cell *q; a=hash(tango); if(bucket[a].chain==NULL) return NULL; else{ q=bucket[a].chain; while ((strcmp(q->eng,tango))!=0){ if(q->next==NULL) return NULL; else q=q->next; } return q; } } 以前の投稿は http://oshiete1.goo.ne.jp/kotaeru.php3?qid=109202

  • ハッシュ法の質問

    #include <stdlib.h> #include <stdio.h> #include <string.h> #define M 257 int zoo(char *v){ int x; x=0; while(*v) x = 256*x + (*v++); if (x<0) x=(-x); return(x%M);} #define CHARMAX 10000 static int chartop=0; static int charbtm=CHARMAX; static char charheap[CHARMAX]; char *goo(char *s){ char *cp; int i,j,len,result; cp =s; len=0; while(*cp++) len++; len++; if(charbtm - chartop < len){ printf("errrrrrrrrrrrrrrrrrrrr"); exit(1);} result = chartop; j=chartop; chartop += len; cp=s; for (i=0;i<len;i++) charheap[j+i]=(*cp++); return(&charheap[result]);} struct item {char *id; int info;}; static struct item table[M]; void initialize(){ int i; for(i=0; i<M; i++){ table[i].id=goo(" ");}} void enter(char *id1, int info1){ int x; x=zoo(id1); while(strcmp(table[x].id, " ")) x=(x+1)%M; table[x].id =goo(id1); table[x].info=info1;} int search(char *id1){ int x; x=zoo(id1); while(strcmp(table[x].id,id1)) x=(x+1)%M; return(table[x].info);} main(){ int t; initialize(); enter("takahasi",1234); enter("kato",2345); enter("saito",4532); printf("%d\n",search("takahasi")); printf("%d\n",search("kato")); printf("%d\n",search("saito")); } これは学生の学籍番号を登録し、登録した名前から番号を検索するプログラムです。 1.このプログラムでは何人まで登録できますか? 2.その人数を超えた場合何が起こるか。 3.配列charheap、配列tableには何が格納されているか という問題があたのですが上の3つの問題がわかりません。誰か教えてください。1・は10000かなっておもいましたが違うようです。

  • 文字の並べ替え

    C言語で文字の並べ替えをしたいのですが条件があり ポインタを使って文字を並べ替える別の関数をを渡さなければなりません.(void reverse(char *str)をつくる) そこで作ったのが #include <stdio.h> #include <string.h> void reverse(char *str); int main(void){ char s[80]; gets(s); reverse(s); printf("%s\n",s); return 0; } void reverse(char *str){ char q[80]; int i,n; n=strlen(str)-1; str+=n; for(i=0;i<=n;i++)q[i]=*str--; str++; q[i]='\0'; for(i=0;i<=n;i++)*str++=q[i]; } なのですが ポインタを使っている意味が余りないので ポインタを使うよりよいプログラムを教えてください. (アドバイスでもかまいません.)

  • 構造体宣言したポインタ変数に値を代入するには?

    strcpy(p -> key ,name);と打ってp -> key に入力した名前を格納したいのですがうまくいきません。 ほかにもp -> key = nameなども試してみましたがコンパイルエラーが出現してダメでした。 うまく格納できるやり方があれば教えてください。よろしくお願いします。 #include<stdio.h> #include<string.h> #define WORD_LENGTH 50 /* 文字列の最大長 */ typedef struct cell{ char key[WORD_LENGTH]; struct cell *next; /* 次のセルへのポインタ */ } CELL; void main(void) { char name[WORD_LENGTH]; CELL *p; printf("名前入力\n"); scanf("%s\n", name); strcpy(p -> key ,name); printf("%s\n", p -> key); }

  • 構造体の構造体 引数

    構造体の中の構造体の関数の引き渡し方法がわかりません。 下記ソースで試したのですが、うまくいきませんでした。 助言お願いいたします。 //repo.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define NUM 20 #define MAX 15 struct seiseki{ float shu[3]; }; struct seito{ char name[NUM]; int age; struct seiseki kekka; }; void input(struct seito *p); void s_input(struct seiseki *p); void ss_input(struct seiseki *data); int main(){ int i; struct seito data[2]; for(i=0;i<2;i++){ printf("------------------------------\n"); printf("%d人目",i+1); input(&data[i]); } printf("%f\n",data[0].kekka.shu[0]); printf("%f\n",data[0].kekka.shu[1]); printf("%f\n",data[0].kekka.shu[2]); //data[1]に格納できない。 printf("%f\n",data[1].kekka.shu[0]); printf("%f\n",data[1].kekka.shu[1]); printf("%f\n",data[1].kekka.shu[2]); return 0; } void input(struct seito *p){ printf("名前->"); scanf("%s",p->name); printf("年齢->"); scanf("%d",p->age); s_input(&(p->kekka)); } void s_input(struct seiseki *data){ printf("国語->"); ss_input(data); printf("算数->"); ss_input(data); printf("英語->"); ss_input(data); } //下記関数で成績をchar型で受け取り、数値化したい。 void ss_input(struct seiseki *data){ char p[100]; int i=0; static int o=0; scanf("%s",p); while( p[i] != '\0'){ if(isdigit(p[i])==0){ printf("再入力してください"); scanf("%s",p); } i++; } data->shu[o]=atof(p); printf("%f\n",data->shu[o]); o++; }

  • プログラミング構造体について。

    include<stdio.h> #include<stdlib.h> #include<string.h> struct person{ char name[10]; int gender; int age; }; void printPersonList(struct person *person_p, int size); void outputPersonList(struct person *person_p, int size); double getAverageOfAge(struct person *person_p, int size); int countMales(struct person *person_p, int size); int countFemales(struct person *person_p, int size); int main(void){ struct person *person_p; int i, count, gender, age, maleCount, femaleCount; char name[20]; double average; printf("登録する人数を入力してください。\n"); scanf("%d", &count); person_p = (struct person*)malloc(sizeof(struct person)* count); for(i=0; i < count; i++){ printf("名前・性別(男性:0, 女性:1)・年齢をスペース区切りで入力してください。\n"); scanf("%s %d %d", name, &gender, &age); strcpy((person_p + i) -> name, name); (person_p + i) -> gender = gender; (person_p + i) -> age = age; } printPersonList(person_p, count); outputPersonList(person_p, count); average = getAverageOfAge(person_p, count); printf("平均年齢:%f\n", average); maleCount = countMales(person_p, count); femaleCount = countFemales(person_p, count); printf("男性:%d名, 女性:%d名\n", maleCount, femaleCount); free(person_p); return 1; } void printPersonList(struct person *person_p, int size){ int i; printf("登録リスト\n"); printf(" name | gender | age\n"); printf("----------+--------+-----\n"); for(i=0; i < size; i++){ printf("%10s | %1d | %2d\n", (person_p + i) -> name, (person_p + i) -> gender, (person_p + i) -> age); } } void outputPersonList(struct person *person_p, int size){ FILE *output; int i; if((output = fopen("meibo.c", "w")) == NULL){ printf("meibo.cを開けませんでした。\n"); return; } for(i=0; i < size; i++){ fprintf(output, "%s, %d, %d\n", (person_p + i) -> name, (person_p + i) -> gender, (person_p + i) -> age); } fclose(output); } 残り3つの関数をすべて定義する(それぞれ10行程度) getAverageOfAge, countMales, countFemales どう定義すればいいのか教えてください。お願いします。

  • 【至急】予想通りの結果が得られません。

    下記のプログラム(C++)を実行しようとすると、以下の2と3の間で止まります。 1、「_/_/_/_/...」が表示される。   ↓ 2、「Hideaki...」が表示される。   ↓ 3、ユーザに性別(sex)の入力をさせる。   =停止(反応なし)=   ↓ 4、継承クラスのWizardが「"Fire"を放つ」と表示される(はず・・・。 #include <stdio.h> #include <cstring> #include <cstdlib> #include <ctime> // if をたくさん使いたい // 所持金をつかったジョブシステムでゲーム // 所持金をメダルや経験値や武器・防具に交換できる struct Hajimete_no_struct { int i; char str; }; class BaseCharacter { protected: char name[200]; private: char sex[200]; //int HP; int EXP; char weapon[200]; char protector[200]; public: BaseCharacter(void){} void speak(const char speaker[200], const char speakThis[200]) { printf("%s:%s\n",speaker, speakThis); } }; BaseCharacter* p; class Wizard : public BaseCharacter { int MP; public: Wizard (void){} void magic(const char magicThis[200]) { printf("%s:%sを放つ", name, magicThis); } }; // ******************************************************************** // SimpleFunstion // ******************************************************************** void Start(void); void Start(void){ printf("\n\n\n_/_/_/_/_/_/_/_/_/_/_/_/_/_/\n = Fantasy =\n_/_/_/_/_/_/_/_/_/_/_/_/_/_/\n\n\n");} // sex void Ask01(void);int i; void Ask01(void){ printf("[ 1.woman 2.man ]\nChoose this sex:");scanf("%d", &i); printf("Your selection : %d\n",i); if (i == 1) { printf("1.woman\n"); }else if(i == 2){ printf("2.man\n"); }else { printf("Please select from 1 or 2.\n"); Ask01();} } // ******************************************************************** // intMain // ******************************************************************** int main(void) { Start(); BaseCharacter human01; human01.speak("Hideaki", "My name is Hideaki."); Ask01();//sex !This function will not use so destroy. Wizard human02; human02.magic("Fire"); }

  • 構造体へのポインタについて

    初心者です。 C入門書の著者のサポートページには正誤表とダウンロードしかないためこちらで質問させていただきます。 下記のコードの下から二行目の構造体へのポインタ (Car *) について、中学生に説明するように基本的な考え方、目的、書式、参考URLなどを教えて下さい。 ポインタについては、該当の章を読み直し基本事項については理解しておりますが、突然あるページから(Void *)や(Char *)など括弧で閉じるものが説明なしに出てきてちょっと混乱してます。(汗 どうぞ宜しくお願い致します。 #include<stdio.h> /* 構造体型struct Carの宣言 */ typedef struct Car{ int num; double gas; }Car; int main(void) { printf("int型のサイズは%dバイトです。¥n", sizeof(int)); printf("double型サイズは%dバイトです。¥n", sizeof(double)); printf("構造体structCar型のサイズは%dバイトです。¥n", sizeof(Car)); printf("構造体struct Car型へのポインタのサイズは%dバイトです。¥n", sizeof(Car *)); return 0; }

  • プログラミングの質問です

    #include<stdio.h> #include<stdlib.h> #include<string.h> struct person{ char name[10]; int gender; int age; }; void printPersonList(struct person *person_p, int size); void outputPersonList(struct person *person_p, int size); double getAverageOfAge(struct person *person_p, int size); int countMales(struct person *person_p, int size); int countFemales(struct person *person_p, int size); int main(void){ struct person *person_p; int i, count, gender, age, maleCount, femaleCount; char name[20]; double average; printf("登録する人数を入力してください。\n"); scanf("%d", &count); person_p = (struct person*)malloc(sizeof(struct person)* count); for(i=0; i < count; i++){ printf("名前・性別(男性:0, 女性:1)・年齢をスペース区切りで入力してください。\n"); scanf("%s %d %d", name, &gender, &age); strcpy((person_p + i) -> name, name); (person_p + i) -> gender = gender; (person_p + i) -> age = age; } printPersonList(person_p, count); outputPersonList(person_p, count); average = getAverageOfAge(person_p, count); printf("平均年齢:%f\n", average); maleCount = countMales(person_p, count); femaleCount = countFemales(person_p, count); printf("男性:%d名, 女性:%d名\n", maleCount, femaleCount); free(person_p); return 1; } void printPersonList(struct person *person_p, int size){ int i; printf("登録リスト\n"); printf(" name | gender | age\n"); printf("----------+--------+-----\n"); for(i=0; i < size; i++){ printf("%10s | %1d | %2d\n", (person_p + i) -> name, (person_p + i) -> gender, (person_p + i) -> age); } } void outputPersonList(struct person *person_p, int size){ FILE *output; int i; if((output = fopen("meibo.c", "w")) == NULL){ printf("meibo.cを開けませんでした。\n"); return; } for(i=0; i < size; i++){ fprintf(output, "%s, %d, %d\n", (person_p + i) -> name, (person_p + i) -> gender, (person_p + i) -> age); } fclose(output); } 上は、「キーボードで入力した個人データ(名前、性別、年齢)を構造体に入れて、一覧を画面に出し、かつ、ファイルに書き出す」ことをするプログラムです。これを名前が辞書順になるようにしたいです。どう書くのか教えてください。

  • 質問ですが

    教えていただきたいのですが、C言語で名前と点数を入力して合計と平均を求めるというプログラムを作ったのですが、 main関数の中で情報入力、合計と平均の計算をしているのでそれぞれ入力関数、合計を求める関数、平均を求める関数というものを作ってmain関数をすっきりとさせたいのですがどうすればいいですか? #include <stdio.h> typedef struct{ char name[15]; int jpn; int eng; int math; int sum; } stdent; typedef struct{ double sum; double jpn; double eng; double math; } Score; void maxmin(stdent *, int); main() { int i, n; n = 5; stdent person[n]; Score average; average.jpn = 0; average.eng = 0; average.math = 0; average.sum = 0; for(i = 0; i < n; i++) { printf("Student No.%d \n",i + 1); printf("Name? "); scanf("%s", person[i].name); printf("Japanese? "); scanf("%d", &(person[i].jpn)); printf("English? "); scanf("%d", &(person[i].eng)); printf("Math? "); scanf("%d", &(person[i].math)); printf("\n"); person[i].sum = person[i].jpn + person[i].eng + person[i].math; average.jpn += person[i].jpn; average.eng += person[i].eng; average.math += person[i].math; average.sum += person[i].sum; } average.jpn /= n; average.eng /= n; average.math /= n; average.sum /= n; printf(" 名前 国語 英語 数学 \n"); printf("------------------------------------\n"); for(i = 0; i < n; i++) { printf("%8s %3d %3d %3d %3d\n", person[i].name, person[i].jpn, person[i].eng, person[i].math, person[i].sum); } printf("------------------------------------\n"); printf("平均 %.1lf %.1lf %.1lf %.1lf\n\n", average.jpn,average.eng,average.math,average.sum); return 0; }

専門家に質問してみよう