查找关键词程序代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<iostream.h>
#include<math.h>
#include<string.h>
#define N 5000
class A{
charpassage[N];
public:
A()
{
char pass[N]={" As I crossed the line my initial thought was how much harder the race had been than expected, bearing in mind how, only eight weeks before, I had set a new personal best of two minutes 12.2 seconds. Then my mind turned to the result. Had I done it? I thought I had. I was aware of where the other athletes were, and was sure that I'd just made it. But, until I saw it on the scoreboard, I wouldn't let myself believe it. As I stood there, staring up and waiting for confirmation, I tried hard to keep negative thoughts from my mind—but I couldn't help thinking, what if I have just missed out? What if I've been through all this, and missed out?"};
}
int address(int i) //单词所在位置函数
{
int s=1;
for(int j=0;j<i;j++)
{
if(passage[j]==' ')
{
s++;
}
}
return s;
}
void find(char b[]) //查找函数
{
int m,n=0,k=strlen(b); //K求出输入单源自文库长度
for(int i=0;i<N;i++)
passage[i]=pass[i];
}
int compare(char a,int i) //比较单字母是否相同函数
{
if(passage[i]==a||int(fabs(passage[i]-a))==32) return 1;
else return 0;
for(int i=0;i<N;i++)
{
int j=0,t1=0;
if((passage[i]==' '||passage[i]=='.'||passage[i]=='?'||passage[i]=='!'||passage[i]==',')&&(passage[i+k+1]==''||passage[i+k+1]=='.'||passage[i+k+1]=='?'||passage[i+k+1]=='!'||passage[i+k+1]==','))
{
int t2=i+1;
for(t2;t1<k;t2++)
{
if(compare(b[j++],t2)) //调用比较单字母是否相同函数
t1++;
else t1=k+1;
}
if(t1==k)
{
n++;
m=address(i); //调用单词位置函数
cout<<"第"<<n<<"个"<<b<<"为第"<<m<<"个单词"<<endl;
{
int t2=i+1;
for(t2;t1<k;t2++)
{
if(compare(b[j++],t2)) //调用比较单字母是否相同函数
t1++;
else t1=k+1;
}
if(t1==k)
{
for(int t3=i+1;t3<i+k+1;t3++)
passage[t3]='*';
m++;
i+=k;
}
}
}
}
void change(char b[]) //替换函数
{
int m=0,k=strlen(b); //K求出输入单词长度
for(int i=0;i<N;i++)
{
int j=0,t1=0;
if((passage[i]==' '||passage[i]=='.'||passage[i]=='?'||passage[i]=='!'||passage[i]==',')&&(passage[i+k+1]==' '||passage[i+k+1]=='.'||passage[i+k+1]=='?'||passage[i+k+1]=='!'||passage[i+k+1]==','))
}
}
}
if(m)
{
cout<<passage<<endl;
cout<<"共替换"<<m<<"次"<<endl;
}
else cout<<"未查到此单词"<<endl;
}
};
void main(void)
{
A a;
char b[50];
cout<<"输入要查找的单词:"<<endl;
cin.getline(b,50);
a.find(b);
a.change(b);
}
相关文档
最新文档