文本匹配问题
说明:建立一个文本文件,其内容如下。对于任一给定的单词,在该文本中找寻匹配的字符串,输出结果
Information security means protecting information and information systems from unauthorized access, use, disclosure, disruption, modification, or destruction. The terms information security, computer security and information assurance are frequently used interchangeably.
实验内容:
1、实现文件的读取操作,而不是将文本以字符串形式预存于程序中
2、进行单词匹配时,不区分大小写
3、统计需要匹配的单词在该文本中出现的次数和位置(即该单词是文本的第几个单词),并输出
主要思想:
(1)单词查询:这个方面需要进行字符串匹配来查找到对应的单词并记录个数
(2)模糊查找:采用两个单词对比寻找最大公共子串来进行模糊查找找到文件中相近的单词
(3)文件读写操作:这些单词是要存在文件中进行读写的。
主要函数:
文件读取主要函数:
void savetxt()
{
FILE *fp;
if((fp=fopen("1.txt","w"))==NULL)
{
printf("无法打开此文件\n");
exit(0);
}
char ch;
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
}
模糊查找代码
void equalclose(char *s1,char *s2,int* a){
int i,l2;
l2=strlen(s2);
int j=0;
for(i=0;i<l2;i++)
{
if(s1[i]==s2[j])
{j++;}
}
if(abs(j-l2)<3){
*a=1;
printf("文件中并没有这个单词,是否要查找%s\n",s1);//TODO
}
}
单词个数统计函数:
void chaxun1(struct danci *head,char *str)
{
int flag1=0;
int total=0;
danci *p=head;
while(p!=NULL)
{
if(equal(p->s,str))
total+=1;
p=p->next;
}
if(total==0){
while(head!=NULL)
{
equalclose(head->s,str,&flag1);
head=head->next;
if(flag1){
break;
}
}
//xunzhaozj(head,str);
//TODO*/
}
else{
printf("该单词出现了%d次",total);
}
}
全部源码:
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <string>
struct danci
{
char s[20]; //单词
int colum;
int n; //第n个单词
struct danci *next;
};
void savetxt(); //键盘输入保存到文件
struct danci* read(struct danci *head); //建立一个单词链表
void chaxun1(struct danci *head,char *str); //求单词出现的总次数
void chaxun2(struct danci *head,char *str); //求单词第一次出现所在的行号及位置
int equal(char *s1,char *s2); //字符串匹配
int main()
{
printf("请输入文本(以#结束):");
savetxt();
struct danci *p=NULL;
p=read(p);
printf("请输入查询的单词:");
char str[30];
scanf("%s",str);
chaxun1(p,str);
chaxun2(p,str);
return 0;
}
void savetxt()
{
FILE *fp;
if((fp=fopen("1.txt","w"))==NULL)
{
printf("无法打开此文件\n");
exit(0);
}
char ch;
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
}
struct danci* read(struct danci *head)
{
FILE *fp;
if((fp=fopen("1.txt","r"))==NULL)
{
printf("无法打开此文件\n");
exit(0);
}
struct danci *p,*q;
p=(struct danci *)malloc(sizeof(struct danci));
head=q=p;
char ch;
int col=1;
int count=1;
int i=0;
ch=fgetc(fp);
while(ch!=EOF) //从文件读取字符
{
if(ch=='\n') //当遇到‘.’或‘,’的下一字符为换行符
{
ch=fgetc(fp);
count=1;
col+=1; //行数加一,个数重置一
continue;
}
while(ch!=' '&&ch!=','&&ch!='.'&&ch!='\n'&&ch!=EOF)
{
p->s[i]=ch;
i++;
ch=fgetc(fp);
}
p->s[i]='\0'; //加结束标志
i=0;
p->colum=col;
p->n=count;
if(ch==' '||ch==','||ch=='.')
{
count+=1; //个数加一
}
if(ch=='\n')
{
count=1;
col+=1; //行数加一,个数重置一
}
if(ch==EOF)
{
break;
}
q=p;
p=(struct danci *)malloc(sizeof(struct danci));
q->next=p;
ch=fgetc(fp);
}
q->next=NULL;
return head;
}
void equalclose(char *s1,char *s2,int* a){
int i,l2;
l2=strlen(s2);
int j=0;
for(i=0;i<l2;i++)
{
if(s1[i]==s2[j])
{j++;}
}
if(abs(j-l2)<3){
*a=1;
printf("文件中并没有这个单词,是否要查找%s\n",s1);//TODO
}
}
void chaxun1(struct danci *head,char *str)
{
int flag1=0;
int total=0;
danci *p=head;
while(p!=NULL)
{
if(equal(p->s,str))
total+=1;
p=p->next;
}
if(total==0){
while(head!=NULL)
{
equalclose(head->s,str,&flag1);
head=head->next;
if(flag1){
break;
}
}
//xunzhaozj(head,str);
//TODO*/
}
else{
printf("该单词出现了%d次",total);
}
}
void chaxun2(struct danci *head,char *str)
{
int flag=0;
while(head!=NULL)
{
if(equal(head->s,str))
{
flag=1;
break;
}
head=head->next;
} //遍历整个链表
if(flag)
printf("该单词:%s 首次出现的位置第%d个单词\n",str,head->n);
}
int equal(char *s1,char *s2)
{
int i,l1,l2;
l1=strlen(s1);
l2=strlen(s2);
if(l1!=l2)
return 0;
for(i=0;i<l1;i++)
{
if(s1[i]!=s2[i])
return 0;
}
return 1;
就到此结束拉,希望大家拿走代码后点点关注。