| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 793 人关注过本帖
标题:数据结构 串的基本操作(顺序存储方式)
取消只看楼主 加入收藏
ltianc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:128
注 册:2012-10-16
结帖率:100%
收藏
已结贴  问题点数:6 回复次数:1 
数据结构 串的基本操作(顺序存储方式)
//采用定长顺序存储来表示串的基本操作
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 225

typedef struct
{
    char str[MAXSIZE];
    int length;
} SeqString;

int StrInsert(SeqString *S, int i, SeqString T)
{
    int k;
    if(i > S->length || S->length + T.length > MAXSIZE)
    {
        printf("不能插入\n");
        return 0;
    }
    else
    {
        for(k = S->length - 1; k >= i - 1; k--)
        {
            S->str [k + T.length ] = S->str [k];
        }
        for(k = 0; k < T.length ; k++)
            S->str [i + k - 1] = T.str [k];
        S->length += T.length ;
        S->str [S->length ] = '\0';
    }
    printf("插入之后的字符串:");
    printf("%s\n", S->str );

    return 0;
}

int StrDelete(SeqString *S, int i, int len)
{
    int k;
    if(i < 1 || i > S->length || i - 1 + len > S->length)
        return 0;
    else
    {
        for(k = i - 1 + len; k < S->length; k++)
            S->str[k - len] = S->str[k];
        S->length = S->length - len;
        S->str[S->length] = '\0';
    }
    printf("删除之后的字符串:");
    printf("%s\n", S->str);

    return 0;

}

int StrLength(SeqString S)
{
    int i = 0;
    while(S.str [i] != '\0')
        ++i;
    S.length = i;
    printf("字符串的长:%d\n", S.length );

    return 0;
}
int Concat(SeqString s1, SeqString s2)
{
    int i;
    SeqString S;
    if(s1.length + s2.length > MAXSIZE)
    {
        return 0;
    }
    else
    {
        for(i = 0; i < s1.length; i ++)
        S.str [i] = s1.str [i];
        for(i = 0; i < s2.length; i ++)
        S.str [s1.length + i] = s2.str [i];
        S.length = s1.length + s2.length ;
        S.str [S.length ] = '\0';
    }
    printf("连接后的字符串:%s\n", S.str);

    return 0;
}
int SubString(SeqString S, int p, int len)
{
    int i;
    SeqString T;
    if(S.length <= 0 || p > S.length || len < 0 || len > S.length - p + 1)
        return 0;

    else
    {
        for(i = 0; i < len; i ++)
            T.str [i] = S.str [p + i - 1];
        T.str [len] = '\0';
    }
    printf("取得的字串:");
    printf("%s\n",T.str );

    return 0;
}

int main(void)
{
    SeqString s1, s2, s3;
    int i, length;
    printf("字符串操作基本练习:\n");
    printf("* * * * * * * * * * * * * *\n");
    printf("1-------------求串长\n");
    printf("2-------------串插入\n");
    printf("3-------------串连接\n");
    printf("4-------------子串删除\n");
    printf("5-------------取子串\n");
    printf("0-------------退出\n");
    printf("* * * * * * * * * * * * * *\n");

   
    i = 5;
    while(i < 6)
    {
        printf("请选择操作类型:");
        scanf("%d", &i);
        switch(i)
    {
    case 1: printf("请输入串:");
        scanf("%s", s1.str );
        StrLength(s1);
        printf("请继续选择\n");
        break;
    case 2: printf("插入串是:");
        scanf("%s", s2.str );
        printf("字符串长度为:%d\n", s2.length = strlen(s2.str ));
        printf("要插入的字符串:");
        scanf("%s", s3.str  );
        printf("字符串的长度为:%d\n", s3.length = strlen(s3.str ));
        printf("输入要插入的位置:");
        scanf("%d", &i);
        StrInsert(&s2, i, s3);
        printf("请继续选择\n");
        break;
    case 3:printf("请输入前串:");
        scanf("%s", s1.str );
        printf("字符串长度为:%d", s1.length = strlen(s1.str ));
        printf("请输入后串:");
        scanf("%s", s2.str );
        printf("字符串长度为:%d", s2.length = strlen(s2.str ));
        Concat(s1, s2);
        printf("请继续选择\n");
        break;
    case 4:printf("请输入源子串:");
        scanf("%s", s1.str );
        printf("字符串长度为:%d", s1.length = strlen(s1.str ));
        printf("请输入要删除的位置:");
        scanf("%d", &i);
        printf("请输入删除的长度:");
        scanf("%d", &length);
        StrDelete(&s1, i, length);
        printf("请继续选择\n");
        break;
    case 5:printf("请输入源子串:");
        scanf("%s", s1.str );
        printf("字符串长度为:%d\n", s1.length = strlen(s1.str ));
        printf("请输入子串的位置:");
        scanf("%d", &i);
        printf("请输入子串的长度:");
        scanf("%d", &length);
        SubString(s1, i, length);
        printf("请继续选择\n");
        break;
    case 0: printf("程序结束\n");
        exit(0);
    }
    }
   

    return 0;
}
c语言实现,给大家分享下。
搜索更多相关主题的帖子: 存储 include return 
2012-11-23 22:15
ltianc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:128
注 册:2012-10-16
收藏
得分:0 
回复 2楼 wp231957
你的意思是没有注释?

世界等我去改变。
2012-11-24 16:35
快速回复:数据结构 串的基本操作(顺序存储方式)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021226 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved