2 sinat 21595363 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)){
            HString head,tail;
            StrAssign(head,SubString(S,1,i-1));
            StrAssign(tail,SubString(S,i+StrLength(T),StrLength(S)-i-StrLength(T)+1));
            StrAssign(S,Concat(head,tail));
            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'
                        StrAssign(head,SubString(S,1,i-1));
                        ^~~~~~~~~
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'
                        StrAssign(S,Concat(head,tail));
                        ^~~~~~~~~
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.

函数StrAssign明明定义了啊,它的参数为什么不对呢?
多谢!

5个回答

caozhy
caozhy   Ds   Rxr 2016.03.02 18:31

StrAssign(head,SubString(S,1,i-1));
其中SubString(S,1,i-1)这里返回的是HString不是char *

可是你只有
void StrAssign(HString &S,char *chars)

caozhy
caozhy   Ds   Rxr 2016.03.02 18:53

程序错误太多了
HString head,tail;
这里没有初始化head,tail

HString Concat(HString &S1,HString &S2)
这里,你到底是修改S1还是返回一个新的HString

caozhy
caozhy   Ds   Rxr 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));
        StrAssign(S,Concat(head,tail));

这个逻辑也不对啊,此时S被修改了,i应该从头来。

sinat_21595363
sinat_21595363 你好!多谢指教,我修改了下代码,如下:
接近 2 年之前 回复
sinat_21595363
sinat_21595363 你好!多谢指教,我修改了下代码,如下:
接近 2 年之前 回复
sinat_21595363
sinat_21595363 你好!多谢指教,我修改了下代码,如下:
接近 2 年之前 回复
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
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;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!