📄 word_ly0001.cpp
字号:
#include"word_ly0001.h"
Astring::Astring(int sz)
{maxSize=sz;
ch=new char[maxSize+1];
if(ch==NULL){cout<<"存储分配错误!"<<endl;exit(1);}
curLength=0;
ch[0]='\0';
for(int i=0;i<defaultsize/100;i++)ps[i]=0;
for(i=0;i<defaultsize/100;i++)ps_ws[i]=0;
for(i=0;i<defaultsize;i++)ws[i]=0;
}
Astring::Astring(const char *init)
{int len=strlen(init);
maxSize=(defaultsize>=len)?defaultsize:len;
ch=new char[maxSize+1];
if(ch==NULL){cout<<"存储分配错误!"<<endl;exit(1);}
curLength=len;
strcpy(ch,init);
for(int i=0;i<defaultsize/100;i++)ps[i]=0;
for(i=0;i<defaultsize/100;i++)ps_ws[i]=0;
for(i=0;i<defaultsize;i++)ws[i]=0;
}
Astring::Astring(const Astring &ob)
{maxSize=ob.maxSize;
ch=new char[maxSize];
if(ch==NULL){cout<<"存储分配错误!"<<endl;exit(1);}
strcpy(ch,ob.ch);
curLength=ob.curLength;
for(int i=0;i<defaultsize/100;i++)ps[i]=0;
for(i=0;i<defaultsize/100;i++)ps_ws[i]=0;
for(i=0;i<defaultsize;i++)ws[i]=0;
}
bool Astring::operator()(int pos,int len)
{Astring temp;
if(pos<0||pos+len>maxSize||len<0){temp.curLength=0;temp.ch[0]='\0';cout<<"参数不合理出错"<<endl;return false;}
else
{if(pos+len>curLength)len=curLength-pos;
for(int i=0,j=pos;i<len;i++,j++){temp.ch[i]=ch[j];temp.curLength=len;}
cout<<temp<<endl;
return true;
}
}
Astring& Astring::operator =(Astring &ob)
{if(&ob!=this)
{delete[]ch;
maxSize=ob.maxSize;
ch=new char[maxSize+1];
if(ch==NULL){cout<<"存储分配错误!"<<endl;exit(1);}
strcpy(ch,ob.ch);
curLength=ob.curLength;
}
else cout<<"字符串自身赋值出错!"<<endl;
return *this;
}
Astring& Astring::operator+=(Astring &ob)
{char *temp=ch;
int n=curLength+ob.curLength;
int m=(maxSize>=n)?maxSize:n;
ch=new char[m+1];
if(ch==NULL){cout<<"存储分配错误!"<<endl;exit(1);}
strcpy(ch,temp);
strcat(ch,ob.ch);
maxSize=m;
curLength=n;
delete[]temp;
return *this;
}
void Astring::operator [](int i)
{if(i<0||i>=curLength)cout<<"位置参数"<<i+1<<"超界!"<<endl;
else cout<<ch[i]<<endl;
}
int Astring::Find(Astring &pat,int k)const
{for(int i=k;i<=curLength-pat.curLength;i++)
{for(int j=0;j<pat.curLength;j++)
if(pat.ch[j]!=ch[i+j])break;
if(j==pat.curLength)return i; //i是从0开始的,表示第几个还得加1
}
return -1;
}
int Astring::count_p()
{int count=0,i=0;
//if(curLength>0)count=1; //文件末尾若换行则count=0;若没有换行则count=1
while(1)
{
while(i<curLength&&ch[i]!='\n')i++;
if(i<curLength)
{i++;
count++;
ps[count]=i;
}
else break;
}
return count;
}
int Astring::count_w()
{int count=0,i=0,j=0;
//if(curLength>0)count=1;
while(1)
{
while(i<curLength&&ch[i]==' ')i++;
ws[count]=i;
while(i<curLength&&ch[i]!=' '&&ch[i]!='\n')i++;
if(ch[i]==' '){count++;i++;ws[count]=i;}
else if(ch[i]=='\n'){count++;i++;ps_ws[j]=count;j++;}
else break;
}
return count;
}
void Astring::search_one(int k,Astring &pat) //逐个查找
{int i=k,j=ps[k-1],m,n=1,t,x;
while(j<=curLength-pat.curLength)
{m=Find(pat,j);
if(n==1&&m==-1){cout<<"共搜索到"<<n-1<<"个结果"<<endl;break;}
else if(n>1&&m==-1){cout<<"文章已结束,共搜索到"<<n-1<<"个结果"<<endl;break;}
else
{
while(1)
{if(m>=ps[i-1]&&m<ps[i])
{cout<<"第"<<n<<"个结果在第"<<i<<"段";break;}
else i++;
}
if(k==1)t=0;
else t=ps_ws[k-2];
for(;;t++)
{if(ws[t]==m)break;}
cout<<"的第"<<t+1-ps_ws[i-2]<<"个单词处"<<endl;
cout<<"1—查找下一处 2—停止查找"<<" "<<"请选择:";
cin>>x;
if(x==1){j=m+pat.curLength;n++;}
else break;
}
}
}
void Astring::search_all(Astring &pat) //全篇查找
{int i=1,j=ps[0],m,n=1;
while(j<=curLength-pat.curLength)
{m=Find(pat,j);
if(m==-1){cout<<"共搜索到"<<n-1<<"个结果"<<endl;break;}
else
{while(1)
{if(m>=ps[i-1]&&m<ps[i])
{cout<<"第"<<n<<"个结果在第"<<i<<"段";break;}
else i++;
}
for(int t=0;;t++)
{if(ws[t]==m)break;}
cout<<"的第"<<t+1-ps_ws[i-2]<<"个单词处"<<endl;
j=m+pat.curLength;
n++;
}
}
}
void Astring::replace_one(int k,Astring &pat,Astring &renew)
{int i=k,j=ps[k-1],m,n1=1,n2=0,t,x;char *p;
//n1记录找到几个结果,n2记录替换了几处
while(j<=curLength-pat.curLength)
{m=Find(pat,j);
if(n1==1&&m==-1){cout<<"很抱歉,文章中没有"<<pat<<endl;break;}
else if(n1>1&&m==-1){cout<<"文章已结束,共替换了"<<n2<<"处"<<endl;break;}
else
{while(1)
{if(m>=ps[i-1]&&m<ps[i])
{cout<<"第"<<n1<<"个结果在第"<<i<<"段";break;}
else i++;
}
if(k==1)t=0;
else t=ps_ws[k-2];
for(;;t++)
{if(ws[t]==m)break;}
cout<<"的第"<<t+1-ps_ws[i-2]<<"个单词处"<<endl;
cout<<"1—替换该处 2—取消操作"<<" "<<"请选择:";
cin>>x;
if(x==1)
{p=&ch[m];
strncpy(p,renew.ch,pat.curLength);
save_to_file(*this);
cout<<"替换完毕"<<endl;
n2++;
}
cout<<"1—替换下一处 2—停止替换"<<" "<<"请选择:";
cin>>x;
if(x==1){j=m+pat.curLength;n1++;}
else break;
}
}
}
void Astring::replace_all(Astring &pat,Astring &renew)
{int i=1,j=ps[0],m,n=1;char *p;
while(j<=curLength-pat.curLength)
{m=Find(pat,j);
if(n==1&&m==-1){cout<<"很抱歉,文章中没有"<<pat<<endl;break;}
else if(n>1&&m==-1){cout<<"共搜索到"<<n-1<<"个结果,已全部替换完毕"<<endl;break;}
else
{p=&ch[m];
strncpy(p,renew.ch,pat.curLength);
j=m+pat.curLength;
n++;
}
}
}
void Astring::cut_p(int k,Astring &cutbank)
{int i=ps[k-1],j=0;
for(;i<ps[k];i++,j++){cutbank.ch[j]=ch[i];}
cutbank.ch[j]='\0';
cutbank.curLength=ps[k]-ps[k-1];
for(j=ps[k];j<curLength;j++)ch[j-cutbank.curLength]=ch[j];
curLength=curLength-cutbank.curLength;
cout<<"已将第"<<k<<"段剪切到剪切板上"<<endl;
}
void Astring::copy_p(int k,Astring &cutbank)
{int i=ps[k-1],j=0;
for(;i<ps[k];i++,j++){cutbank.ch[j]=ch[i];}
cutbank.ch[j]='\0';
cutbank.curLength=ps[k]-ps[k-1];
cout<<"已将第"<<k<<"段复制到剪切板上"<<endl;
}
void Astring::paste(int k,Astring &cutbank)
{char *temp=ch;
int i,j,t;
int n=curLength+cutbank.curLength;
int m=(maxSize>=n)?maxSize:n;
ch=new char[m+1];
if(ch==NULL){cout<<"存储分配错误!"<<endl;exit(1);}
for(i=0;i<ps[k-1];i++)ch[i]=temp[i];
for(t=i,j=0;j<cutbank.curLength;t++,j++)ch[t]=cutbank.ch[j];
for(;i<curLength;i++,t++)ch[t]=temp[i];
//ch[t]='\0';
curLength=n;
maxSize=m;
delete[]temp;
/*strcpy(ch,temp);
strcat(ch,cutbank.ch);
curLength=n;
maxSize=m;*/
}
istream& operator>>(istream &in,Astring &str)
{int i=0;
char x=getchar();
while(x!='\n') //用'\n'控制输入结束,不能用别的字符,否则点确定时'\n'也算一个getchar()
{str.ch[i]=x;
x=getchar();
i++;
}
str.curLength=i;
str.ch[i]='\0'; //使strlen=curLength而非maxSize
return in;
}
ostream& operator<<(ostream &os,Astring &str)
{for(int i=0;i<str.curLength;i++)os<<str.ch[i];
os<<endl;
return os;
}
void save_to_file(Astring &Astr)
{ofstream outfile("word.txt"); //默认为以输出方式打开磁盘文件“ios::out”
if(!outfile)
{cerr<<"打开文件失败!"<<endl;
exit(1);
}
for(int i=0;i<Astr.curLength;i++)outfile<<Astr.ch[i];
outfile.close();
}
void get_from_file(Astring &Astr) //以输入方式打开磁盘文件"word.txt",并输出到显示屏上
{ifstream infile("word.txt",ios::in|ios::nocreate);
if(!infile)
{cerr<<"打开文件失败!"<<endl;
exit(1);
}
int i=0;
while(infile.get(Astr.ch[i]))
{cout.put(Astr.ch[i]);
i++;}
cout<<endl;
Astr.curLength=i;
Astr.ch[i]='\0';
infile.close();
}
int main()
{Astring Astr,cutbank; //cutbank为剪切板,记录剪切内容
int x;
get_from_file(Astr);
A:Astr.count_p();Astr.count_w();
cout<<endl<<"请选择菜单:"<<endl;
cout<<"1-统计段落数"<<" "<<"2-统计单词数"<<" "<<"3-查找"<<" "
<<"4-替换"<<" "<<"5-剪切"<<" "<<"6-复制"<<" "<<"7-粘贴"<<" "<<"8-读取文件"<<" "<<"0-退出"<<endl;
cout<<"请选择:";
cin>>x;
switch(x)
{case 1:cout<<"该文章的段落数为"<<Astr.count_p()<<endl;goto A;
case 2:cout<<"该文章的单词数为"<<Astr.count_w()<<endl;goto A;
case 3:{int k;
Astring pat;
cout<<"1—全篇范围查找 2—逐个查找"<<" "<<"请选择:";
cin>>x;
cout<<"请输入要查找的内容:"<<endl;
cin>>pat;
if(x==1)Astr.search_all(pat);
if(x==2)
{cout<<"请输入开始查找的段落:";cin>>k;
Astr.search_one(k,pat);}
goto A;
}
case 4:{int k;
Astring pat,renew;
cout<<"1—全篇范围替换 2—逐个替换"<<" "<<"请选择:";
cin>>x;
cout<<"请输入需要被替换的内容:"<<endl;
cin>>pat;
cout<<"请输入替换后的内容:"<<endl;
cin>>renew;
if(x==1)Astr.replace_all(pat,renew);
if(x==2)
{cout<<"请输入开始替换的段落:";cin>>k;
Astr.replace_one(k,pat,renew);}
save_to_file(Astr);
goto A;
}
case 5:{int k;
cout<<"请输入需要被剪切的段落:";
cin>>k;
Astr.cut_p(k,cutbank);
save_to_file(Astr);
goto A;
}
case 6:{int k;
cout<<"请输入需要被复制的段落:";
cin>>k;
Astr.copy_p(k,cutbank);
//cout<<cutbank;
goto A;
}
case 7:{int k;
cout<<"请输入粘贴的位置(段落):";
cin>>k;
Astr.paste(k,cutbank);
//Astr+=cutbank;
save_to_file(Astr);
cout<<"粘贴完毕"<<endl;
goto A;
}
case 8:{cout<<"该文本文档内容为:"<<endl;
get_from_file(Astr);
goto A;
}
case 0:{cout<<"退出程序?"<<endl<<"1—确定 2—取消"<<endl;
cin>>x;
if(x==2)goto A;
else break;}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -