关于串的堆存储基本操作
用了好长时间编个程序竟然有错误,花了好长时间也没找出来哪错了,请各位高手给予指点,先谢谢了//堆分配方式:串的基本操作
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct
{
char *ch;
int length;
} StackString;
int StrAssign(StackString *S, char *chars)
{
int len, i;
char *ch;
for(len = 0, ch = chars; *ch; ch ++, len ++);
if(!len)
{
S->ch = NULL;
S->length = 0;
}
else
{
if(!(S->ch = (char *)malloc(len * sizeof(char))))
{
printf("不能分配\n");
return 0;
}
for(i = 0; i < len; i ++)
S->ch[i] = chars[i];
S->length = len;
S->ch [len] = '\0';
}
return 0;
}
int StrEmpty(StackString S)
{
if(S.length == 0)
{
printf("空\n");
return 0;
}
else
{
printf("非空\n");
return 0;
}
}
int StrCompare(StackString S, StackString 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];
return S.length - T.length ;
}
int StrConcat(StackString *S, StackString s1, StackString s2)
{
int i;
if(!(S->ch = (char *)malloc((s1.length + s2.length ) * sizeof(char))))
{
printf("不能连接\n");
return 0;
}
for(i = 0; i < s1.length; i ++)
S->ch[i] = s1.ch [i];
for(i = 0; i < s2.length; i ++)
S->ch [s1.length + i] = s2.ch [i];
S->length = s1.length + s2.length ;
S->ch [S->length] = '\0';
return 0;
}
int SubString(StackString *T, StackString S, int p, int len)
{
int i;
if(p < 1 || p > S.length || len < 0 || len > S.length - p + 1)
{
printf("输入错误\n");
return 0;
}
if(!len)
{
T->ch = NULL;
T->length = 0;
}
else
{
if(!(T->ch = (char *)malloc(len * sizeof(char))))
{
printf("分配失败!\n");
return 0;
}
for(i = 0; i < len; i ++)
T->ch [i] = S.ch [p - 1 + i];
T->ch [len] = '\0';
T->length = len;
}
return 0;
}
int DisplayString(StackString S)
{
int i = 0;
while(S.ch[i] != '\0')
printf("%c", S.ch[i]);
return 0;
}
int main(void)
{
StackString s1, s2, s3;
char ch;
int i, length;
printf("***************************\n");
printf("1--------串分配\n");
printf("2--------串比较\n");
printf("3--------串连接\n");
printf("4--------求子串\n");
printf("0--------退出\n");
printf("***************************\n");
i = 5;
printf("请选择操作类型:");
while(i < 6)
{
scanf("%d", &i);
switch(i)
{
case 1:printf("请输入要分配的字符串:");
scanf("%s", &ch);
StrAssign(&s1, &ch);
printf("分配后的串为:");
DisplayString(s1);
printf("请选择操作类型:");
break;
case 2:printf("请输入比较的串s1:");
scanf("%s", &ch);
s1.ch = &ch;
for(s1.length = 0, s1.ch; *(s1.ch); s1.length ++, s1.ch ++ );
printf("输入的字符串长度为:%d\n", s1.length);
printf("请输入比较的串s2:");
scanf("%s", &ch);
s2.ch = &ch;
for(s2.length = 0, s2.ch; *(s2.ch); s2.length ++, s2.ch ++);
printf("输入的字符串的长度为:%d\n", s2.length);
printf("两串比较的结果为:%d\n", StrCompare(s1, s2));
printf("请选择操作类型:");
break;
case 3:printf("请输入前串:");
scanf("%s", &ch);
s1.ch = &ch;
for(s1.length = 0, s1.ch; *(s1.ch); s1.length ++, s1.ch ++ );
printf("请输入后串:");
scanf("%s", &ch);
s2.ch = &ch;
for(s2.length = 0, s2.ch; *(s2.ch); s2.length ++, s2.ch ++);
StrConcat(&s3, s1, s2);
printf("连接后的字符串为:");
DisplayString(s3);
printf("请选择操作类型:");
break;
case 4:printf("请输入源串:");
scanf("%s", &ch);
s1.ch = &ch;
for(s1.length = 0, s1.ch; *(s1.ch); s1.length ++, s1.ch ++ );
printf("从第几个字符后取:");
scanf("%d", &i);
printf("请确定子串的长度:");
scanf("%d", &length);
SubString(&s2, s2, i, length);
printf("子串为:");
DisplayString(s2);
printf("请选择操作类型:");
break;
case 0:printf("程序结束\n");
return 0;
}
}
return 0;
}
[ 本帖最后由 ltianc 于 2012-11-25 22:34 编辑 ]