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;
}