新人求助,代码出错了
#include<iostream>#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define SIZE 20
typedef FILE *PFILE;
typedef char String[SIZE];
using namespace std;
typedef struct //单词类型
{
String data;//单词串
int len; //单词的长度
}WordType;
typedef struct WordNode //单词结点类型
{
WordType data;
WordNode *next;
}WordNode, *PWordNode;
typedef struct RowLink //表示文本每一行的链表
{
WordNode *head, *tail;
}RowLink,*RLink;
typedef struct RowNumNode //行号结点类型
{
int elem; //行号
RowNumNode *next;
}RowNumNode,*RowNumLink;
typedef struct SearchWordNode //带搜索的单词结点类型
{
WordType data; //待搜索单词
int count; //待搜索单词出现的次数
RowNumLink RNhead, RNtail; //存放文本中出现待搜索单词行号的链表
SearchWordNode *next;
}SearchWordNode,*SWLink;
struct SWLinkList
{
SWLink head,tail;
};
void CopyWord(WordType &w, String ch) //把字符串ch复制到单词元素w
{
int j = strlen(ch);
for (int i = 0; i <= j; i++)
w.data[i] = ch[i];
w.len = j;
}
int MatchWord(WordType w1, WordType w2) //单词的匹配,若相等则返回1,否则返回非0
{
int i;
if (w1.len != w2.len)
return 0;
else
{
for ( i = 0; i < w1.len; i++)
{
if (w1.data[i] != w2.data[i])
break;
}
if ( i==w1.len)
return 1;
else
return 0;
}
}
void MakeWordNode(PWordNode &PN) //生成一个单词结点
{
if (!(PN = (PWordNode)malloc(sizeof(WordNode))))
{
cout << "为单词分配存储空间失败" << endl;
exit(0);
}
PN->next = NULL;
}
void InsertAfter(RowLink &L, WordType w) //用后插法把单词结点w插入链表L
{
MakeWordNode(L.tail->next);
L.tail->next->data = w;
L.tail = L.tail->next;
}
void DestroyWordLink(RowLink &L) //销毁链表L
{
while (L.head)
{
L.tail = L.head->next;
delete(L.head);
L.head = L.tail;
}
}
void CreateWordLink(RowLink &L, FILE *f) //创建存放f指向文本中每一行单词的链表
{
int i;
String ch;
char c = getc(f); //从文件中读取一个字符
WordType w;
MakeWordNode(L.head);
L.tail = L.head;
while (c != '\n'&&!feof(f))
{
while (!(c >= 'A'&&c <= 'Z' || c >= 'a'&&c <= 'z') && c != '\n'&&!feof(f)) //滤去非法字符
c = getc(f);
for ( i = 0; c >= 'A'&&c <= 'Z' || c >= 'a'&&c <= 'z'; i++) //取单词
{
ch[i] = c;
c = getc(f);
}
ch[i] = '\0';
CopyWord(w, ch);
InsertAfter(L, w);
}
}
void MakeRowNumNode(RowNumLink &p) //生成一个行结点
{
if (!(p = (RowNumLink)malloc(sizeof(RowNumNode))))
{
cout << "分配行号结点失败" << endl;
exit(0);
}
p->next = NULL;
}
void MakeSWNode(SWLink &p) //生成一个待搜索的单词结点
{
if (!(p = (SWLink)malloc(sizeof(SearchWordNode))))
{
cout << "分配待搜索的单词结点失败" << endl;
exit(0);
}
p->next = NULL;
p->count = 0;
p->RNhead = NULL;
p->RNtail = NULL;
}
void CreateSWLinkList(SWLinkList &S) //建立一个待搜索的单词链表
{
MakeSWNode(S.head);
S.tail = S.head;
String st = "#";
WordType w, label;
CopyWord(label, st);
cout << "请输入要搜索的英文单词,输入完毕后请输入'#'结束输入" << endl;
cin >> st;
CopyWord(w, st);
while (!MatchWord(w, label)) //w不是'#'
{
MakeSWNode(S.tail->next);
S.tail->next->data = w;
S.tail = S.tail->next;
cin >> st;
CopyWord(w, st);
}
}
void MatchSWLinkList(SWLinkList &S, FILE *f)//查找文本中出现待搜索的单词
{
RowLink RL; //用于保存文件中一行单词的链表
PWordNode pr = NULL; //指向文件单次链表中的每一个单词
SWLink ps = NULL; //指向被搜索的单词
int i = 0;
while (!(feof(f))) //读取文本中的每一行单词
{
i++; //行号
CreateWordLink(RL, f); //创建文本的一行单词的链表
ps = S.head->next; //ps指向此时被搜索的单词
while (ps) //遍历待搜索的单词链表的每个结点,及当ps所指单词不为空,在本行中查找其出现次数
{
pr = RL.head->next; //该指针指向文件该行的链表
int label = 1; //用于标志待搜索单词在本行中是否是第一次出现,若是须创建一行结点,若不是,直接count+1
while (pr) //文本中一行单词链表的每个结点依次与被搜索单词比较
{
if (MatchWord(pr->data, ps->data))
{
ps->count++;
if (label == 1) //是该正在搜索指针所指的
{
if (ps->RNhead == NULL) //判断是否是第一个结点
{
MakeRowNumNode(ps->RNhead); //创建统计被搜索单词出现次数及行号的链表
ps->RNhead->elem = i;
ps->RNtail = ps->RNhead;
}
else
{
MakeRowNumNode(ps->RNtail->next);
ps->RNtail->next->elem = i;
ps->RNtail = ps->RNtail->next;
}
label = 0;
}
}
pr = pr->next; //指向本行中下一个单词并进行比较
}
ps = ps->next; //对待搜索的下一个单词进行统计
}
DestroyWordLink(RL);//销毁已被搜索过的文件中该行单词的链表
}
}
void OutputSWLinkList(SWLinkList S) //输出待搜索的单词链表在文本中出现的次数和行号
{
SWLink p; //指向待输出的单词
RowNumLink pr; //指向待输出单词的某一行
cout << "搜索结果:" << endl;
p = S.head->next;
while (p)
{
printf("%-8s", p->data.data);
cout << "出现的次数" << p->count << " " << "所在行为:";
if (p->count)
{
pr = p->RNhead;
while (pr)
{
cout << pr->elem << ",";
pr = pr->next;
}
cout << endl;
p = p->next;
}
}
}
void OpenFile(PFILE &f, String ch) //打开文件,表示文件的路径及名称
{
if (!(f = fopen(ch, "r"))) //以只读方式打开文件
{
cout << "file not open" << endl;
}
else
cout << "file open" << endl;
}
int main()
{
while (1)
{
PFILE f;
String ch;
SWLinkList S;
cout << "*----------------------------------------------------*" << endl;
cout << "*----------------欢迎使用文学研究助手----------------*" << endl;
cout << "* *" << endl;
cout << "*----------请输入要搜索的文本的路径及文件名:--------*" << endl;
cin >> ch;
OpenFile(f, ch);
CreateSWLinkList(S);
MatchSWLinkList(S, f);
OutputSWLinkList(S);
fclose(f);
cout << "*----------------------谢谢使用!--------------------*" << endl;
system("pause");
}
return 0;
}