程序没有错误,但是不能运行,请大家指点下~
我用的是VC++6.O#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef struct{
char *ch;
int length;
}*HString;
StrAssign(HString T,char *chars)
{
int i,j;char *c;
if (T->ch) free(T->ch);
for (i=0,c=chars;*c;++i,++c);
if (!i) {T->ch=NULL;T->length=0;}
else {
if(!(T->ch=(char*)malloc(i*sizeof(char))))
return ERROR;
for (j=0;j<i;++j)
T->ch[j]=chars[j];
T->length=i;
}
return OK;
}
int StrLength(HString S)
{
return S->length;
}
int StrCompare(HString S,HString T)
{
int i;
for(i=0;i<S->length&&i<T->length;++i)
if(S->ch[i]!=T->ch[i])
return S->ch[i]-T->ch[i];
}
ClearString(HString S)
{
if (S->ch) {free(S->ch);S->ch=NULL;}
S->length=0;
return OK;
}
Concat(HString T,HString S1,HString S2)
{
int i;
if(T->ch) free(T->ch);
if(!(T->ch=(char*)malloc((S1->length+S2->length)*sizeof(char))))
return ERROR;
for(i=0;i<S1->length;++i)
T->ch[i]=S1->ch[i];
T->length=S1->length+S2->length;
for(i=0;i<S2->length;++i)
T->ch[S1->length+i]=S2->ch[i];
return OK;
}
SubString(HString Sub,HString S,int pos,int len)
{
int i;
if(pos<1||pos>S->length||len<0||len>S->length-pos+1) return ERROR;
if(Sub->ch) free(Sub->ch);
if(!len)
{
Sub->ch=NULL;
Sub->length=0;
}
else
{
Sub->ch=(char*)malloc(len*sizeof(char));
for(i=0;i<len;++i)
Sub->ch[i]=S->ch[pos+i-1];
Sub->length=len;
}
return OK;
}
StrInsert(HString S,int pos,HString T)
{
int i;
if(pos<1||pos>S->length+1) return ERROR;
if(T->length) {
if(!(S->ch=(char *)realloc(S->ch,(S->length+T->length)*sizeof(char))))
return ERROR;
for(i=0;i<S->length-pos+1;++i)
S->ch[S->length+T->length-1-i]=S->ch[S->length-1-i];
for(i=0;i<T->length;++i)
S->ch[pos-1+i]=T->ch[i];
S->length+=T->length;
}
return OK;
}
int ShowString(HString S)
{
int i;
for(i=0;i<S->length;++i)
printf("%c",S->ch[i]);
return OK;}
main()
{
int i,pos,len;
HString S1,S2,P,T,Sub=NULL;
char s,*chars,*temp,*test;
printf("请输入串S1:\n");
scanf("%s",chars);
StrAssign(S1,chars);
ShowString(S1);
printf("\n串的长度是%d!",StrLength(S1));
printf("\n请输入串S2:");
scanf("%s",temp);
StrAssign(S2,temp);
ShowString(S2);
i=StrCompare(S1,S2);
if(i<0)
s='<';
else if(i==0)
s='=';
else
s='>';
printf("串S1 %c 串S2!\n",s);
Concat(T,S1,S2);
printf("经过比较后的串为 T is:");
ShowString(T);
printf("\n请输入你所需要的位置和长度:");
scanf("%d,%d",&pos,&len);
SubString(Sub,T,pos,len);
printf("子串是:");
ShowString(Sub);
printf("\n请插入模式串S2:\n");
scanf("%s",test);
StrAssign(P,test);
printf("请输入你所要查找的位置:");
scanf("%d",&pos);
StrInsert(S2,pos,P);
printf("T的操作结果为:");
ShowString(S2);
getch();
}