h
#pragma once
#include "pch.h"
namespace sstring {
class sstringlist {
public :
void init(sstringlist *);
void watch(sstringlist *);
int cup(sstringlist *);//截取函数 相当于strcat
int increase(sstringlist *);//插入函数
int remove(sstringlist *);//删除
int compre(sstringlist *, sstringlist*);
int kmp(sstringlist *,sstringlist*,int );
private:
char *data;
int index;
};
}
cpp
#include "pch.h"
#include "串.h"
using namespace sstring;
void sstringlist::init(sstringlist *l)
{
cout << "请输入串值初始化" << endl;
l->index = 0;
l->data = new char;
cin >> l->data;
char *s = l->data;
while (*(s++) != '\0')
{
l->index++;
}
watch(l);
};
void sstringlist::watch(sstringlist *l)
{
cout << l->data;
}
int sstringlist::cup(sstringlist *l)
{
if (l->data == NULL)
{
cout << "字符串为空!" << endl;
}
cout << "输入截取的位置" << endl;
int x, y; //截取的前后索引
cin >> x >> y;
x--;
if (x<0 || y>l->index - 1)
{
cout << "输入错误" << endl;
return 0;
}
*(l->data + y) = '\0';//后面的边空 必须从后序开始 先序的话下标会改变
while (x--)
{
*l->data++ = '\0';//前面的为空
}
l->index = l->index - (y - x);
cout<<"截取后是"<<endl;
watch(l);
}
int sstringlist::increase(sstringlist *l)
{
cout << "请输入你要插入的位置" << endl;
int x; cin >> x;
if (x > l->index||x<1)
{
cout<<"位置不对"<<endl;
return 0;
}
char font[max];//把l从x切断,font是前序
int cf = 0;//font的长度
for (int i = 0; i < x; i++)
{
font[i] = *l->data + i;
cf++;
}
int cr = l->index - cf; //rear的长度
char *rear= l->data + x;//后序
cout << "输入长度和字符串" << endl;
char s[max]; //接入的字符串
int cs; cin >> cs; //接入的长度
for (int i = 0; i < cs; i++)
{
cin >> s[i];
}
for (int i = 0; i < cs; i++)//前序+接入
{
font[i + cs] = s[i];
}
for (int i = 0; i < cr ; i++)//前序+接入+后序
{
font[i + cf+cs] = rear[i];
}
l->index = l->index + cs;
watch(l);
}
int sstringlist::remove(sstringlist *l)
{
if (l->index == 0)
{
cout<<"空串!"<<endl;
return 0;
}
cout << "请输入你要删除的位置" << endl;
int x; cin >> x;
if ((x > l->index)||(x<1))
{
cout << "位置不对" << endl;
return 0;
}
char font[max];//把l从x切断,font是前序
int cf = 0;//font的长度
for (int i = 0; i < x; i++)
{
font[i] = *l->data + i;
cf++;
}
int cr = l->index - cf; //rear的长度
char *rear = l->data + x;//后序
cout << "输入长度" << endl; //接入的字符串
int rs; cin >> rs; //接入的长度
for (int i = 0; i < rs-1; i++)
{
l->data[x + i] = l->data[x+rs+i];
}
watch(l);
}
int sstringlist::compre(sstringlist*l, sstringlist*l2)
{
if (l->index != l2->index)
{
cout << "不等于!" << endl;
return 0;
}
int count = 0;
for (int i = 0; i < l->index-1;i++)
{
if (l->data[i] == l2->data[i])
{
count++;
}
}
if (count ==
l->index)
{
cout << "等于!" << endl;
return 1;
}
else {
cout<<"不等于!"<<endl;
return 0;
}
};
int sstringlist::kmp(sstringlist *l, sstringlist*l2, int next)
{
next = 0; int j = 0;
for (int i = 0; i < l->index-1; i++)
{
if (l->data[i] == l2->data[j])//想等条件
{
j++;
}
else {
j = 0;//不相等重新计算
}
if (j == l2->index)
{
cout << i;
}
}
return 1;
}