数据结构--串

// string.cpp : Defines the entry point for the console application.
/*-----CODE FOR FUN---------------
-------CREATED BY Dream_Whui--
-------2015-1-27-------------------*/

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

#define   TRUE                    1
#define   FALSE                    0
#define   OK                        1
#define   ERROR                0
#define   OVERFLOW       -2
#define   INFEASIBLE           -1

typedef struct//定义串的结构
{
    char *ch;
    int length;
}HString;

int StrAssign(HString &T, char *chars)//生成一个值等于串常量chars的串T
{
    if(T.ch)
        free(T.ch);
    int num;
    num = strlen(chars);
    if(!num)
    {
        T.ch = NULL;
        T.ch = 0;
    }
    else
    {
        T.ch =(char*)malloc( num*sizeof(char) );
        if(!T.ch)
            return ERROR;
        T.length = num;
        int i;
        for(i=0; i<num; i++)
            T.ch[i] = chars[i];
        T.ch[i]='\0';
    }
    return OK;
}

int StrLength(HString S)//串的长度
{
    return S.length;
}

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 1;
        else if(S.ch[i] < T.ch[i])
            return -1;
    }
    return S.length - T.length;
}

int ClearString(HString &S)//清空串,释放串的空间
{
    if(S.ch)
    {
        free(S.ch);
        S.ch = NULL;
    }
    S.length = 0;
    return OK;
}

int Concat(HString &T, HString S1, HString S2)//T返回S1和S2连接的新串
{
    if(T.ch)
        free(T.ch);
    T.ch = (char*)malloc( (S1.length + S2.length) * sizeof(char) );
    if(!T.ch)
        return ERROR;
    int i;
    for(i=0; i<S1.length+S2.length; i++)
    {
        if(i<S1.length)
            T.ch[i] = S1.ch[i];
        else
            T.ch[i] = S2.ch[i-S1.length];
    }
    T.ch[i] = '\0';
    T.length = S1.length+S2.length;
    return OK;
}

int SubString(HString &sub, HString S, int pos, int len)//截取从串S第pos个位置开始的len个字符,由sub返回
{
    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));
        int i,j;
        for(i=pos-1, j=0; j<len; i++, j++)
            sub.ch[j] = S.ch[i];
        sub.ch[j]='\0';
        sub.length = len;
    }
    return OK;
}

int StrInsert(HString &S, int pos, HString T)//在串S的第pos个位置之前插入串T
{
    if(pos<0 || pos >S.length+1)
        return ERROR;
    S.ch = (char*)realloc(S.ch, (S.length+T.length) * sizeof(char));
    int i,j;
    for(i=S.length-1; i>=pos-1; i--)
        S.ch[i+T.length] = S.ch[i];
    for(i=pos-1, j=0; j<T.length; j++,i++)
        S.ch[i] = T.ch[j];
    S.length += T.length;
    S.ch[S.length] = '\0';
    return OK;
}

void InitString(HString &S)//初始化串
{
    S.ch = (char*)malloc(sizeof(char));
    S.ch[0] = '\0';
    S.length = 0;
}

int main(int argc, char* argv[])
{
    HString T;
    InitString(T);
    cout<<T.ch<<endl;
    char *c = "123456";
    cout<<c<<endl;
    StrAssign(T, c);
    cout<<T.ch<<endl;
    cout<<strlen(T.ch)<<endl;
    HString S;
    S.ch = "abcdef";
    S.length = 6;
    cout<<StrCompare(S,T)<<endl;
    HString P;
    InitString(P);
    Concat(P,S,T);
    cout<<"P: "<<P.ch<<" "<<P.length<<endl;
    HString sub;
    InitString(sub);
    SubString(sub,P,3,5);
    cout<<"sub: "<<sub.ch<<" "<<sub.length<<endl;
    StrInsert(P,5,sub);
    cout<<P.ch<<" "<<P.length<<endl;
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值