字符串顺序表

main.cpp

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#define max 100
class SqString
{
public :
    char *base;
    int maxlen;
public:
    SqString();
    SqString(char *s);
    void CreatString(SqString &t);
    ~SqString();
    bool InsertString(int pos, SqString &t);
    bool DeleteString(int pos, int len, SqString &t);
    void OutputString();
};
SqString::SqString(){
    maxlen = max;
    base = new char[maxlen];
    base[0] = 0;

}
SqString::SqString(char *s){
    maxlen = max;
    base = new char[maxlen + 1];
    base[0] = strlen(s);
    for (int i = 1; i <= base[0]; i++){
        base[i] = s[i - 1];
    }
}
void SqString::CreatString(SqString &t){
    maxlen = max;
    base = new char[maxlen + 1];
    base[0] = t.base[0] + 1;
    for (int i = 1; i <= base[0]; i++){
        base[i] = t.base[i];
    }
    
}
SqString::~SqString(){
    delete[] base;
}

bool SqString::InsertString(int pos, SqString &t){
    if (pos<1 || pos>base[0] + 1 || base[0] + t.base[0] > maxlen){
        cout << "插入失败!" << endl;
        return false;
    }
    else
    {
        for (int i = base[0]; i >= pos; i--){
            base[i + t.base[0]] = base[i];
        }
        for (int j = 1; j <= t.base[0]; j++){
            base[pos - 1 + j] = t.base[j];
        }
        base[0] = base[0] + t.base[0];
        return true;
    }

}

void SqString::OutputString(){
    if (base[0] == 0)
        cout << "空字符串" << endl;
    else
    {
        cout << "输出字符串:";
        for (int i = 1; i <= base[0]; i++)
            cout << base[i];
        cout << endl;
    }
    
}

int main()
{
    SqString str1 = {"abcdef"};
    SqString str2 = { "ghi" };
    SqString S;
    S.CreatString(str1);
    S.OutputString();
    cout << "在字符串第三个位置插入字符串\n";
    S.InsertString(3,str2);
    cout << "插入字符串后为:";
    S.OutputString();
    system("pause");
    return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值