读书人

将C语言代码修改为C++面向对象风格解决

发布时间: 2012-04-16 16:20:04 作者: rapoo

将C语言代码修改为C++面向对象风格
/**自学数据结构...如何把以下代码修改为C++语言让其和执行呢,在main()函数只要有S1="aabcaa"和S2="abc"分别调用上三个函数即可...随便怎么都行,让我容易看懂就好了...*在此谢过了/
void String::Concat(SString &T, SString S1, SString S2)
{
// 用T返回由S1和S2联接而成的新串。若未截断,则返回TRUE,否则FALSE。
int i;
Status uncut;
if (S1[0]+S2[0] <= MAXSTRLEN)
{ // 未截断
for (i=1; i<=S1[0]; i++) T[i] = S1[i];
for (i=1; i<=S2[0]; i++) T[i+S1[0]] = S2[i];
T[0] = S1[0]+S2[0];
uncut = TRUE;
} else if (S1[0] < MAXSTRLEN)
{ // 截断
for (i=1; i<=S1[0]; i++)
T[i] = S1[i];
for (i=S1[0]+1; i<=MAXSTRLEN; i++) T[i] = S2[i-S1[0]];
T[0] = MAXSTRLEN;
uncut = FALSE;
} else
{ // 截断(仅取S1)
for (i=0; i<=MAXSTRLEN; i++) T[i] = S1[i];
uncut = FALSE;
}
return uncut;
}

void String::SubString(SString &Sub, SString S, int pos, int len)
{
// 用Sub返回串S的第pos个字符起长度为len的子串。
// 其中,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1。
int i;
if (pos < 1 || pos > S[0] || len < 0 || len > S[0]-pos+1)
return ERROR;
for(i=1; i<=len; i++)
Sub[i] = S[pos+i-1];
Sub[0] = len;
return OK;
}
Status StrInsert(HString &S, int pos, HString T) { // 算法4.4
// 1≤pos≤StrLength(S)+1。在串S的第pos个字符之前插入串T。
int i;
if (pos < 1 || pos > S.length+1) // pos不合法
return ERROR;
if (T.length) { // T非空,则重新分配空间,插入T
if (!(S.ch = (char *)realloc(S.ch,(S.length+T.length+1)
*sizeof(char))))
return ERROR;
for (i=S.length-1; i>=pos-1; --i) // 为插入T而腾出位置
S.ch[i+T.length] = S.ch[i];
for (i=0; i<T.length; i++) // 插入T
S.ch[pos-1+i] = T.ch[i];
S.length += T.length;
}
return OK;
} // StrInsert

[解决办法]
应该可以用,未做深入测试

C/C++ code
#include <iostream>#include <cstring>using namespace std;class SString{    friend ostream &operator<<(ostream &output, const SString &s);  // 重载输出public:    SString(const char *s = NULL);  // 构造    SString(const SString &s);  // 拷贝构造    ~SString() { }    SString &operator=(const SString &s);  // 重载赋值    char *GetStr() { return str; }        // 由s1和s2联接而成的新串。若未截断,则返回true,否则false    bool Concat(const SString &s1, const SString &s2);    // 用sub返回第pos个字符起长度为len的子串。    bool SubString(SString &sub, int pos, int len) const;    // 在第pos个字符之前插入串t    bool StrInsert(int pos, const SString &t);    protected:    char str[256];};ostream &operator<<(ostream &output, const SString &s){    output << s.str;    return output;}SString::SString(const char *s)  // 构造{    int i;    this->str[0] = '\0';    if (s != NULL)    {        for (i = 0; s[i] != '\0' && i < 255; ++i)            this->str[i] = s[i];        this->str[i] = '\0';    }}SString::SString(const SString &s)  // 拷贝构造{    int i;    for (i = 0; s.str[i] != '\0'; ++i)        this->str[i] = s.str[i];    this->str[i] = '\0';}SString &SString::operator=(const SString &s)  // 重载赋值{    int i;    for (i = 0; s.str[i] != '\0'; ++i)        this->str[i] = s.str[i];    this->str[i] = '\0';    return *this;}// 由s1和s2联接而成的新串。若未截断,则返回true,否则falsebool SString::Concat(const SString &s1, const SString &s2){    int len1 = strlen(s1.str), len2 = strlen(s2.str);    int i;        if (len1 + len2 < 255)  // 未截断    {        for (i = 0; i < len1; ++i)            this->str[i] = s1.str[i];        for (i = 0; i < len2; ++i)            this->str[i + len1] = s2.str[i];        this->str[i + len1] = '\0';        return true;    }    else if (len1 < 255)  // 截断    {        for (i = 0; i < len1; ++i)            this->str[i] = s1.str[i];        for (i = len1 + 1; i < 256; ++i)            this->str[i] = s2.str[i - len1];        this->str[i] = '\0';        return false;    }    else  // 截断(仅取s1)    {        for (i = 0; i < 255; ++i)            this->str[i] = s1.str[i];        this->str[i] = '\0';        return false;    }}// 用sub返回第pos个字符起长度为len的子串。bool SString::SubString(SString &sub, int pos, int len) const{    int thislen = strlen(this->str);    int i;    if (pos < 0 || pos > thislen || len < 0 || len > thislen - pos + 1)        return false;        for (i = 0; i < len; ++i)        sub.str[i] = this->str[pos + i - 1];    sub.str[i]  = '\0';    return true;}// 在第pos个字符之前插入串tbool SString::StrInsert(int pos, const SString &t){    int i;    int thislen = strlen(this->str);    int len = strlen(t.str);    if (pos < 0 || pos > thislen)        return false;    if (thislen + len >= 255)        return false;    for (i = thislen - 1; i >= pos; --i)        this->str[i + len] = this->str[i];        for (i = 0; i < len; ++i)        this->str[pos + i] = t.str[i];    this->str[thislen + len]  = '\0';    return true;}int main(){    SString a("abcdefgh");    SString b = "12345678";    SString c, s;    cout << a << endl;    cout << b << endl;    c.Concat(a, b);    cout << c << endl;    c.SubString(s, 8, 6);    cout << s << endl;    s.StrInsert(3, "ABCDEEGH");    cout << s << endl;    return 0;} 

读书人网 >C++

热点推荐