sinat_21595363 于 2016.03.02 18:07 提问

`````` /串的动态堆分配存储
//by zoe

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define MAXSIZE 100
using namespace std;

typedef struct {
char *ch;
int length;
int size;
}HString;

void InitString(HString &S){
S.ch=new char[MAXSIZE];
if(S.ch==NULL) {cerr<<"error!\n";exit(1);}
S.ch[0]='\0';
S.length=0;
S.size=MAXSIZE;
}

//字符串复制
void StrAssign(HString &S,char *chars){
S.length=strlen(chars);
strcpy(S.ch,chars);
}

int StrLength(HString S){
return S.length;
}

//若s>t,则返回值大于零；s=t，返回值等于0；s<t，返回小于零
int StrCompare(HString S,HString T){
for(int i=0;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}

//将s2连接在s1后面
HString Concat(HString &S1,HString &S2){
int i=0,j;
if((S1.length+S2.length)>MAXSIZE){
char *dest=new char[S1.length+S2.length+1];
if(dest==NULL) {cerr<<"error!\n";exit(1);}
char *temp=dest;
for(int i=0;i<S1.length;i++) dest[i]=S1.ch[i];
delete [] S1.ch;
S1.ch=temp;
S1.size=S1.length+S2.length;
}
for(int j=0;j<S2.length;j++,i++) S1.ch[i]=S2.ch[j];
S1.ch[i]='\0';
S1.length=S1.length+S2.length;
return S1;
}

HString SubString(HString S,int pos,int len ){
HString sub;
sub.ch=(char*)malloc(len*sizeof(char));
if(!sub.ch) {cerr<<"error!\n";exit(1);}
if(pos>=S.length||pos<0||len<0||len>S.length){
sub.ch[0]='\0';sub.length=0;
}
else{
if(pos+len-1>S.length) len=S.length-pos;
for(int i=0,j=pos;i<len;i++,j++) sub.ch[i]=S.ch[j];
sub.ch[len]='\0';
sub.length=len;
}
return sub;
}

//在串s中删除与所有与串t相同的子串，并返回删除的次数
int Delet_SubString(HString &S,HString T){
int n=0;
for(int i=1;i<=(StrLength(S)-StrLength(T)+1);i++){
if(!StrCompare(SubString(S,i,StrLength(T)),T)){
StrAssign(tail,SubString(S,i+StrLength(T),StrLength(S)-i-StrLength(T)+1));
n++;
}
}
return n;
}

void PrintString(HString S){
for(int i=0;i<S.length;i++){
printf("%c ",S.ch[i]);
}
}

int main(){
HString S,T;
InitString(S);
InitString(T);
char s1[]={"abcdefgabcimcseeokabsf"};
char s2[]={"ab"};
StrAssign(S,s1);
StrAssign(T,s2);
int n=Delet_SubString(S,T);
PrintString(S);
printf("%d\n",n);
return 0;
}

``````

`````` wangxiaoxiaodeMacBook-Air:Data-Structures-and-Algorithm wangxiaoxiao\$ g++ HString.cpp -std=c++11
HString.cpp:84:4: error: no matching function for call to 'StrAssign'
^~~~~~~~~
HString.cpp:27:6: note: candidate function not viable: no known conversion from
'HString' to 'char *' for 2nd argument
void StrAssign(HString &S,char *chars){
^
HString.cpp:85:4: error: no matching function for call to 'StrAssign'
StrAssign(tail,SubString(S,i+StrLength(T),StrLen...
^~~~~~~~~
HString.cpp:27:6: note: candidate function not viable: no known conversion from
'HString' to 'char *' for 2nd argument
void StrAssign(HString &S,char *chars){
^
HString.cpp:86:4: error: no matching function for call to 'StrAssign'
^~~~~~~~~
HString.cpp:27:6: note: candidate function not viable: no known conversion from
'HString' to 'char *' for 2nd argument
void StrAssign(HString &S,char *chars){
^
3 errors generated.

``````

5个回答

caozhy      2016.03.02 18:31

void StrAssign(HString &S,char *chars)

caozhy      2016.03.02 18:53

HString Concat(HString &S1,HString &S2)

caozhy      2016.03.02 18:54
``````        StrAssign(head,SubString(S,1,i-1));
StrAssign(tail,SubString(S,i+StrLength(T),StrLength(S)-i-StrLength(T)+1));
``````

sinat_21595363 你好！多谢指教，我修改了下代码，如下：

sinat_21595363 你好！多谢指教，我修改了下代码，如下：

sinat_21595363 你好！多谢指教，我修改了下代码，如下：

sinat_21595363   2016.03.02 20:33
`````` //串的动态堆分配存储
//by zoe

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define MAXSIZE 100
using namespace std;

typedef struct {
char *ch;
int length;
int size;
}HString;

void InitString(HString &S){
S.ch=new char[MAXSIZE];
if(S.ch==NULL) {cerr<<"error!\n";exit(1);}
S.ch[0]='\0';
S.length=0;
S.size=MAXSIZE;
}

int StrLength(HString S){
return S.length;
}

//字符串复制
void StrAssign(HString &S,HString T){
if(S.ch) free(S.ch);
if(!T.ch) {exit(1);}
else{
if(!(S.ch=(char*)malloc(StrLength(T)*sizeof(char)))) {exit(1);}
for(int i=1;i<=StrLength(T);i++){
S.ch[i]=T.ch[i];
}
S.length=T.length;
S.size=T.size;
}
}

//若s>t,则返回值大于零；s=t，返回值等于0；s<t，返回小于零
int StrCompare(HString S,HString T){
for(int i=0;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}

//将s2连接在s1后面
void Concat(HString &S1,HString &S2){
int i=0,j;
if((S1.length+S2.length)>MAXSIZE){
char *dest=new char[S1.length+S2.length+1];
if(dest==NULL) {cerr<<"error!\n";exit(1);}
char *temp=dest;
for(int i=0;i<S1.length;i++) dest[i]=S1.ch[i];
delete [] S1.ch;
S1.ch=temp;
S1.size=S1.length+S2.length;
}
for(int j=0;j<S2.length;j++,i++) S1.ch[i]=S2.ch[j];
S1.ch[i]='\0';
S1.length=S1.length+S2.length;
}

HString SubString(HString S,int pos,int len ){
HString sub;
sub.ch=(char*)malloc(len*sizeof(char));
if(!sub.ch) {cerr<<"error!\n";exit(1);}
if(pos>=S.length||pos<0||len<0||len>S.length){
sub.ch[0]='\0';sub.length=0;
}
else{
if(pos+len-1>S.length) len=S.length-pos;
for(int i=0,j=pos;i<len;i++,j++) sub.ch[i]=S.ch[j];
sub.ch[len]='\0';
sub.length=len;
}

return sub;
}

//在串s中删除与所有与串t相同的子串，并返回删除的次数
int Delet_SubString(HString &S,HString T){
HString sub;
InitString(sub);
int m=StrLength(S);
int n=StrLength(T);
int i=1,count=0;
while(i<=m){
StrAssign(sub,SubString(S,i,n));
if(StrCompare(sub,T)==0){//匹配相等
InitString(sub);
Concat(sub,SubString(S,1,i-1));
Concat(sub,SubString(S,i+n,m-i-n+1));
StrAssign(S,sub);
n=StrLength(S); //要注意，操作完一次后s的值改变了
count++;
}
else {
i++;
}
}
return count;
}

void PrintString(HString S){
for(int i=0;i<S.length;i++){
printf("%c ",S.ch[i]);
}
}

int main(){
HString S,T;
InitString(S);
InitString(T);
char s1[]={"abcdefgabcimcseeokabcsf"};
char s2[]={"bc"};
for(int i=1;i<=S.size;i++){S.ch[i]=s1[i];}
for(int i=1;i<=T.size;i++){T.ch[i]=s2[i];}
int n=Delet_SubString(S,T);
PrintString(S);
printf("%d\n",n);
return 0;
}

``````

`````` wangxiaoxiaodeMacBook-Air:Data-Structures-and-Algorithm wangxiaoxiao\$ g++ HString.cpp
HString.cpp:97:4: error: no matching function for call to 'Concat'
Concat(sub,SubString(S,1,i-1));
^~~~~~
HString.cpp:53:6: note: candidate function not viable: expects an l-value for
2nd argument
void Concat(HString &S1,HString &S2){
^
HString.cpp:98:4: error: no matching function for call to 'Concat'
Concat(sub,SubString(S,i+n,m-i-n+1));
^~~~~~
HString.cpp:53:6: note: candidate function not viable: expects an l-value for
2nd argument
void Concat(HString &S1,HString &S2){
^
2 errors generated.

``````

sinat_21595363   2016.03.02 20:39

`````` //串的动态堆分配存储
//by zoe

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

#define MAXSIZE 100
using namespace std;

typedef struct {
char *ch;
int length;
int size;
}HString;

void InitString(HString &S){
S.ch=new char[MAXSIZE];
if(S.ch==NULL) {cerr<<"error!\n";exit(1);}
S.ch[0]='\0';
S.length=0;
S.size=MAXSIZE;
}

int StrLength(HString S){
return S.length;
}

//字符串复制
void StrAssign(HString &S,HString T){
if(S.ch) free(S.ch);
if(!T.ch) {exit(1);}
else{
if(!(S.ch=(char*)malloc(StrLength(T)*sizeof(char)))) {exit(1);}
for(int i=1;i<=StrLength(T);i++){
S.ch[i]=T.ch[i];
}
S.length=T.length;
S.size=T.size;
}
}

//若s>t,则返回值大于零；s=t，返回值等于0；s<t，返回小于零
int StrCompare(HString S,HString T){
for(int i=0;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}

//将s2连接在s1后面
void Concat(HString &S1,HString &S2){
int i=0,j;
if((S1.length+S2.length)>MAXSIZE){
char *dest=new char[S1.length+S2.length+1];
if(dest==NULL) {cerr<<"error!\n";exit(1);}
char *temp=dest;
for(int i=0;i<S1.length;i++) dest[i]=S1.ch[i];
delete [] S1.ch;
S1.ch=temp;
S1.size=S1.length+S2.length;
}
for(int j=0;j<S2.length;j++,i++) S1.ch[i]=S2.ch[j];
S1.ch[i]='\0';
S1.length=S1.length+S2.length;
}

HString SubString(HString S,int pos,int len ){
HString sub;
sub.ch=(char*)malloc(len*sizeof(char));
if(!sub.ch) {cerr<<"error!\n";exit(1);}
if(pos>=S.length||pos<0||len<0||len>S.length){
sub.ch[0]='\0';sub.length=0;
}
else{
if(pos+len-1>S.length) len=S.length-pos;
for(int i=0,j=pos;i<len;i++,j++) sub.ch[i]=S.ch[j];
sub.ch[len]='\0';
sub.length=len;
}

return sub;
}

//在串s中删除与所有与串t相同的子串，并返回删除的次数
int Delet_SubString(HString &S,HString T){
HString sub;
HString lsub;
InitString(sub);
int m=StrLength(S);
int n=StrLength(T);
int i=1,count=0;
while(i<=m){
StrAssign(sub,SubString(S,i,n));
if(StrCompare(sub,T)==0){//匹配相等
InitString(sub);
InitString(lsub);
Concat(sub,lsub=SubString(S,1,i-1));
Concat(sub,lsub=SubString(S,i+n,m-i-n+1));
StrAssign(S,sub);
n=StrLength(S); //要注意，操作完一次后s的值改变了
count++;
}
else {
i++;
}
}
return count;
}

void PrintString(HString S){
for(int i=0;i<S.length;i++){
printf("%c ",S.ch[i]);
}
}

int main(){
HString S,T;
InitString(S);
InitString(T);
char s1[]={"abcdefgabcimcseeokabcsf"};
char s2[]={"bc"};
for(int i=1;i<=S.size;i++){S.ch[i]=s1[i];}
for(int i=1;i<=T.size;i++){T.ch[i]=s2[i];}
int n=Delet_SubString(S,T);
PrintString(S);
printf("%d\n",n);
return 0;
}

``````