小小虫977 小小虫977
关注数: 1 粉丝数: 1 发帖数: 110 关注贴吧数: 11
VC环境中运行,查找文件中特定单词数目,为什么显示0 #include <stdio.h> #include<stdlib.h> #include <iostream>//标示符各种可见范围 using namespace std;//std内定义的所有标示符有效 const int MAXM = 30,KIND = 26;//指针 int m; /* struct node { char* s; int prefix; bool isword; node* next[KIND]; node() { s = NULL; prefix = 0; isword = false; memset(next,0,sizeof(next)); } }*root; */ void insert(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; p->s = s+i; if (p->next[x] == NULL) p->next[x] = new node; p = p->next[x]; p->prefix++; } p->isword = true; } bool del(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; if (p->next[x] == NULL) return false; p = p->next[x]; } if (p->isword) p->isword = false; else return false; return true; } bool search(node *root,char* s) { node* p = root; for (int i = 0;s[i];i++) { int x = s[i]-'a'; if (p->next[x] == NULL) return false; p = p->next[x]; } return p->isword; } int count(node *root,char *s)//统计后缀 { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; if (p->next[x] == NULL) return 0; p = p->next[x]; } return p->prefix; } int main() { FILE *fp; printf("Please enter the file'name:\n"); char filename[20]; scanf("%s",filename);//输入文件位置和名 if((fp=fopen(filename,"rb"))==NULL)//打开文件 {printf("Cannot open this file\n"); exit(0); } printf("enter the word you want to find :\n"); m = 0; root = new node; char s[MAXM]; while (gets(s)) { if (strcmp(s,"") == 0) break; insert(root,s); } while (gets(s)) printf("%d\n",count(root,s)); fclose(fp); }
C语言要在指定文件中查找特定单词数目,为什么单词数目显示是0 #include <stdio.h> #include<stdlib.h> #include <iostream> using namespace std; const int MAXM = 30,KIND = 26; int m; struct node { char* s; int prefix; bool isword; node* next[KIND]; node() { s = NULL; prefix = 0; isword = false; memset(next,0,sizeof(next)); } }*root; void insert(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; p->s = s+i; if (p->next[x] == NULL) p->next[x] = new node; p = p->next[x]; p->prefix++; } p->isword = true; } bool del(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; if (p->next[x] == NULL) return false; p = p->next[x]; } if (p->isword) p->isword = false; else return false; return true; } bool search(node *root,char* s) { node* p = root; for (int i = 0;s[i];i++) { int x = s[i]-'a'; if (p->next[x] == NULL) return false; p = p->next[x]; } return p->isword; } int count(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; if (p->next[x] == NULL) return 0; p = p->next[x]; } return p->prefix; } int main() { FILE *fp; printf("Please enter the file'name:\n"); char filename[20]; scanf("%s",filename); if((fp=fopen(filename,"rb"))==NULL) {printf("Cannot open this file\n"); exit(0); } printf("enter the word you want to find :\n"); m = 0; root = new node; char s[MAXM]; while (gets(s)) { if (strcmp(s,"") == 0) break; insert(root,s); } while (gets(s)) printf("%d\n",count(root,s)); fclose(fp); }
C语言为什么导入文件之后,输入查找的单词,全部显示为0 #include <stdio.h> #include<stdlib.h> #include <iostream> using namespace std; const int MAXM = 30,KIND = 26; int m; struct node { char* s; int prefix; bool isword; node* next[KIND]; node() { s = NULL; prefix = 0; isword = false; memset(next,0,sizeof(next)); } }*root; void insert(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; p->s = s+i; if (p->next[x] == NULL) p->next[x] = new node; p = p->next[x]; p->prefix++; } p->isword = true; } bool del(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; if (p->next[x] == NULL) return false; p = p->next[x]; } if (p->isword) p->isword = false; else return false; return true; } bool search(node *root,char* s) { node* p = root; for (int i = 0;s[i];i++) { int x = s[i]-'a'; if (p->next[x] == NULL) return false; p = p->next[x]; } return p->isword; } int count(node *root,char *s) { node *p = root; for (int i = 0;s[i];i++) { int x = s[i] - 'a'; if (p->next[x] == NULL) return 0; p = p->next[x]; } return p->prefix; } int main() { FILE *fp; printf("Please enter the file'name:\n"); char filename[20]; scanf("%s",filename); if((fp=fopen(filename,"rb"))==NULL) {printf("Cannot open this file\n"); exit(0); } printf("enter the word you want to find :\n"); m = 0; root = new node; char s[MAXM]; while (gets(s)) { if (strcmp(s,"") == 0) break; insert(root,s); } while (gets(s)) printf("%d\n",count(root,s)); fclose(fp); }
1 下一页