串的定长顺序存储

//========================================================================
// Name        : DataStruct_Practice.cpp
// Author      : hmy
// Version     :
// Copyright   : Your copyright notice
// Description : DataStruct_SString_sq, Ansi-style
//========================================================================

#include <iostream>
#include <stdlib.h>
#define MAX 250
typedef unsigned char SString[MAX + 1];
using std::cout;
using std::endl;
using std::cin;
void Concat(SString S, SString S1);
int SubSString(SString S, int pos, int len);
int StrLength(SString S);
void StrCopy(SString S);
int main()
{
    int n;
    SString S, S1;
    cout << "现在创建一个字符串,长度为多少请输入:" << endl;
    cin >> n;
    S[0] = n;
    cout << "请输入字符串:" << endl;
    for(int i = 1; i != S[0]; ++i){
        cin >> S[i];
    }

//    cout << "现在创建一个字符串,长度为多少请输入:" << endl;
//    cin >> n;
//    S1[0] = n;
//    cout << "请输入字符串:" << endl;
//    for(int i = 1; i != S1[0]; ++i){
//        cin >> S1[i];
//    }
//    Concat(S, S1);
//    SubSString(S,3,5);
//    StrLength(S);
    StrCopy(S);
    return 0;
}

void Concat(SString S, SString S1){
    //串连接
    if(S[0] + S1[0] <= MAX){
        for(int i = 1; i != S1[0]; ++i){
            S[S[0] - 1 + i] = S1[i];// S[5] --> 01234,5是空缺的。
        }
        S[0] = S[0] + S1[0] -1;
        cout << "S[0] + S1[0] <= MAX" << endl;
        for(int i = 1; i != S[0]; ++i){
            cout << S[i];
        }
    }else if(S[0] + S1[0] > MAX && S[0] < MAX){
        for(int i = 1; i != MAX-S[0]; ++i){
            S[S[0] + i - 1] = S1[i];
        }
        S[0] = MAX;
        cout << "S[0] + S1[0] > MAX && S[0] < MAX" << endl;
        for(int i = 1; i != S[0]; ++i){
            cout << S[i];
        }
    }else if(S[0] == MAX){
        cout << "S[0] == MAX" << endl;
        S[0] = MAX;
        for(int i = 1; i != S[0]; ++i){
            cout << S[i];
        }
    }
}

int SubSString(SString S, int pos, int len){
    //求子串
    SString Tem;
    if(pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1){return -2;}
    cout << len;
    Tem[0] = len;
    for(int i = 0; i != Tem[0]; ++i){
        Tem[i + 1] = S[pos + i];
    }
    cout << "SubSString:" << endl;
    for(int i = 1; i != Tem[0] + 1; ++i){
        cout << Tem[i];
    }
    return 0;
}

int StrLength(SString S){
    int len;
    len = S[0];
    cout << len << endl;
    return 0;
}

void StrCopy(SString S){
    SString cp;
    for(int i = 1; i != S[0]; ++i){
        cp[i] = S[i];
    }
    cp[0] = S[0];
    cout << "copy from S:" << endl;
    for(int i = 1; i != cp[0]; ++i){
        cout << cp[i];
    }
    cout << endl;
    StrLength(cp);
}

posted @ 2010-08-15 12:25  云端小飞象cg  阅读(289)  评论(0编辑  收藏  举报