📄 d.cpp
字号:
#include <iostream.h>
typedef struct node{
char data;
struct node *next;
}Lstring;
void Strassign(Lstring * s,char cstr[])
{
int i;
Lstring * r,* p;
r=s;
for(i=0;cstr[i]!='\0';i++)
{
p=new Lstring;
p->data=cstr[i];
p->next=NULL;
r->next=p;
r=p;
}
}
int Length(Lstring * s)
{
int i=0;
Lstring *p=s->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
Lstring * Replace(Lstring *s,int i,int j,Lstring *t)
{
int k;
Lstring *str,*p=s->next,*p1=t->next,*q,*r;
str=new Lstring;
str->next=NULL;
r=str;
if(i<=0||i>Length(s)||j<0||i+j-1>Length(s))
return str;
for(k=0;k<i-1;k++)
{
q=new Lstring;
q->data=p->data;
q->next=NULL;
r->next=q;
r=q;
p=p->next;
}
for(k=0;k<j;k++)
p=p->next;
while(p1!=NULL)
{
q=new Lstring;
q->data=p1->data;
q->next=NULL;
r->next=q;
r=q;
p1=p1->next;
}
while(p!=NULL)
{
q=new Lstring;
q->data=p->data;
q->next=NULL;
r->next=q;
r=q;
p=p->next;
}
return str;
}
void Dispstr(Lstring * s)
{
Lstring *p=s->next;
if(p=NULL)
cout<<"空串"<<endl;
else
{
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
}
}
void main()
{
char a[100]={'\0'};
char b[100]={'\0'};
Lstring *s,*t;
int i,j;
cout<<"input s:";
cin>>a;
s=new Lstring;
s->next=NULL;
Strassign(s,a);
cout<<"input i:";
cin>>i;
cout<<"input j:";
cin>>j;
int len = Length(s);
if(i+j-1> len)
{
cout<<"Input Error";
return ;
}
cout<<"input t:";
cin>>b;
t=new Lstring;
t->next=NULL;
Strassign(t,b);
Lstring *Result = Replace( s,i , j, t);
Result = Result->next;
cout<<"Result:";
while(Result)
{
cout<<Result->data;
Result = Result->next;
}
return ;
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -