📄 wbbj.cpp
字号:
//
//*****************文本编辑器******************//
//********** 作者: happy_zh **********//
//
//*****************This is a word editor.*****************//
//You can input at most 100 chars.Every time after you input the letters,//
//be sure to input a '#' to show stop and over. //
//程序中s1是操作的对象,程序还用了一个s2,对s1操作的时候,把操作的结果暂时
//送入s2,操作完成,再把s2赋给s1。这样虽然有时候浪费了空间,但思想简单。
const int MaxSize = 100;
#include<iostream.h>
class Str{
public:
Str();
void input_str();
void copy_str(Str &s1,Str s2);
void print_str(Str &s);
void go_on_input(Str &s);
void delete_str(Str &s1,Str s);
void insert_str(Str &s,Str s2,Str ss);
void find_position(Str &s,Str &ss,int &n);
void instead_str(Str &s1,Str s2,Str ss1,Str ss2,int n);
char ch[MaxSize];
int len;
int position[100];
int y;
int n;
int b;
int p;
};
//
Str::Str(){ char *ch; ch=new char [MaxSize]; }
//下面的程序为:输入一个字符串,输入#表示结束。
void Str::input_str()
{
cout<<"now input a new str,please. # means over.\n";
int k;
k=0;
ch[k]='x';
while( cin>>ch[k],ch[k]!='#' ) k++;
ch[k]='#';
len=k;
}
//下面的程序为:显示整个字符串。
void Str::print_str(Str &s1)
{
cout<<"\n●The string is:";
for(int k=0;s1.ch[k]!='#';k++)
cout<<ch[k];
cout<<"\n\n";
}
//下面的程序为:拷贝现在的字符串,并连接在现在的字符串后面。
//复制s1,把连接的字符串赋给s2,再赋给s1。
void Str::copy_str(Str &s1,Str s2)
{
int p=0;
for( int k=0; k<s1.len ; k++)
{
s2.ch[p]=s1.ch[k];
p++;
}
for( k=0; k<s1.len; k++)
{
s2.ch[p]=s1.ch[k];
p++;
}
s2.len=p;
cout<<"the length of the string is : "<<s2.len<<'\n';
s2.ch[p]='#';
for(int q=0; s2.ch[q]!='#'; q++)
s1.ch[q]=s2.ch[q];
s1.ch[q]='#';
s1.len=s2.len;
s1.print_str(s1);
}
//在现在的字符串后面继续输入字符,输入#表示结束。
void Str::go_on_input(Str &s)
{
cout<<" # means over.\n";
for(int w=0;s.ch[w]!='#';w++)
cout<<ch[w];
int k;
k=s.len;
s.ch[k]='x';
while( cin>>ch[k],ch[k]!='#' ) {cout<<ch[k];k++;}
ch[k]='#';
len=k;
cout<<"the length of the string is : "<<len<<'\n';
s.print_str(s);
}
//下面的程序为:删除字符串的一部分。
//原来字符串为s1,输入想从字符串上开始删除的位置,再输入删除的字符串长度,
//程序把字符串处理后,传给s2,s2再传回s1。
void Str::delete_str(Str &s1,Str s)
{
cout<<"Input the position you want to start the deletion,please.\n";
int i;
cin>>i;
if(i>s1.len) cout<<"too big.\n";
else { cout<<"Input the length you want to delete,please.\n";
int j;
cin>>j;
if(i+j>s1.len) cout<<"too many.\n";
else {
int p=0;
for (int k=0;k<i;k++)
{s.ch[p]=s1.ch[k];p++;}
for (k=i+j;k<s1.len;k++)
{s.ch[p]=s1.ch[k];p++;}
s.len=p+1;
s.ch[p+1]='#';
for(int q=0; s.ch[q]!='#'; q++)
s1.ch[q]=s.ch[q];
s1.ch[q-1]='#';
s1.len=s.len-1;
cout<<"Delete succeed.\n";
s1.print_str(s1);
}
}
}
//下面的程序为:在字符串内插入一段字符串。
//先输入插入位置,再输入要插的字符串。
void Str::insert_str(Str &s,Str s2,Str ss)
{
cout<<"Input the position you want to insert , please. \n";
int i;
cin>>i;
i--;
if(i>s.len) cout<<"too large.\n";
else {
cout<<"input a string you want to insert ,please.'#' means over.\n";
int r; r=0; ss.ch[r]='x';
while( cin>>ss.ch[r],ss.ch[r]!='#' ) r++;
ss.ch[r]='#';
ss.len=r;
if(ss.len>MaxSize|| ( s.len +ss.len )>MaxSize)
cout<<"too long.\n";
else {
int p=0;
for (int k=0; k<=i; k++)
{
s2.ch[p]=s.ch[k];
p++;
}
for(k=0;k<ss.len;k++)
{
s2.ch[p]=ss.ch[k];
p++;
}
for(k=i+1;k<s.len;k++)
{
s2.ch[p]=s.ch[k];
p++;
}
s2.len=p; cout<<"the length is "<<s2.len;
s2.ch[p]='#';
for(int q=0; s2.ch[q]!='#'; q++)
s.ch[q]=s2.ch[q];
s.ch[q]='#';
s.len=s2.len ;
cout <<"insert succeed.\n";
print_str(s);
}
}
}
//下面的程序为:在字符串内寻找你想要的一段字符串,显示找到的位置。
void Str::find_position(Str &s,Str &ss,int &n)
{
cout<<"input a small string you want to find ,please.\n";
ss.input_str();
int y=0;
for(int i=0;i<s.len;i++)
{
for(int j=i,k=0; s.ch[j]==ss.ch[k]; j++,k++)
{
if(k==ss.len-1)
{ cout<<"the position is "<<i+1<< " .\n";
position[y]=i+1;
y++;
}
}
}
if(!y) {cout<<"none\n"; position[y]=-1;}
n=y; //n表示字符串内有n个你想要的小段字符串。
}
//下面的程序为:在字符串内,把某些小段字符串替换为你想要的字符串。
//先输入你要改变的小段字符串,程序先找到这些字符串,再输入想要替换成的
//字符串,程序完成替换功能。
void Str::instead_str(Str &s1,Str s2,Str ss1,Str ss2,int n)
{
s1.find_position(s1,ss1,n);
if(n!=0)
{
cout<<"input a string you want to take the place of the old one.\n";
ss2.input_str();
cout<<"ok";
cout<<"s1.len="<<s1.len;
cout<<" ss1.len="<<ss1.len;
cout<<" ss2.len="<<ss2.len;
cout<<" n="<<n;
cout<<(s1.len-(ss1.len-ss2.len)*n);
if( (s1.len-(ss1.len-ss2.len)*n)>MaxSize)
cout<<"too long.\n";
else
{
int p=0;
position[n]=s1.len+1;
for(int i=0;i<position[0]-1;i++)
{
s2.ch[p]=s1.ch[i];
p++;
}
for(int nn=0;nn<n;nn++)
{
for(int j=0;j<3;j++)
{
s2.ch[p]=ss2.ch[j];
p++;
}
for(int kk=(position[nn]-1+ss1.len); kk<position[nn+1]-1;kk++)
{
s2.ch[p]=s1.ch[kk];
p++;
}
}
s2.ch[p]='#';
s2.len=p;
for(int qq=0; s2.ch[qq]!='#'; qq++)
s1.ch[qq]=s2.ch[qq];
s1.ch[qq]='#';
s1.len=s2.len ;
}
}
}
//
void main()
{
cout<<"this is a word_editor.\n";
Str s1;
Str s2;
Str ss;
Str ss1;
Str ss2;
int n;
s1.input_str();
int choice=-1;
while(choice!=0)
{
cout<<"choicd for you:\n";
cout<<" 1: print the string.\n";
cout<<" 2: insert a small string.\n";
cout<<" 3: deletee a small string.\n";
cout<<" 4: copy the whole string.\n";
cout<<" 5: instead the string.\n";
cout<<" 6: find the position of a small string.\n";
cout<<" 7: go on inputting. \n";
cout<<" 0: over and exit.\n";
cout<<"your choice : ";
cin>>choice;
switch(choice)
{
case 1: cout<<"1: now print the string.\n";
s1.print_str(s1);
break;
case 2: cout<<"2: insert a small string.\n";
s1.insert_str(s1,s2,ss);
break;
case 3: cout<<"3: deletee a small string.\n";
s1.delete_str(s1,s2);
break;
case 4: cout<<"4: copy the whole string.\n";
s1.copy_str(s1,s2);
break;
case 5: cout<<"5: insert the string.\n";
s1.instead_str(s1,s2,ss1,ss2,n);
break;
case 6: cout<<"6: find the position of a small string.\n";
s1.find_position(s1,ss,n);
break;
case 7: cout<<"7: go on inputting.\n";
s1.go_on_input(s1);
break;
case 0: cout<<"0: over and exit.\n";
cout<<"Goodbye!\n\n";
break;
default:cout<<"error.\n";
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -