p230 继承与派生 代码较长 没有仔细看

//
// Created by 刘彪 on 2020/3/2.
//p230 继承与派生  代码较长 没有仔细看
#include <iostream>
#include <cstring>
using namespace std;
class mystring{
    int length;
    char *contents;
public:
    int get_length(){
        return length;
    }
    char *get_contents(){
        return contents;
    }
    ~mystring(){
        delete contents;
    }
    int set_contents(int in_length,char *in_contents);
    int set_contents(char *in_contents);
    void print(){
        cout<<contents<<endl;
    }
};

class edit_string:public mystring{
    int cursor;
public:
    int get_cursor_pos(){
        return cursor;
    }
    void move_cursor(int how_much){
        cursor = how_much;
    }
    int add_at_cursor(mystring *new_text);
    int repl_at_cursor(mystring *next_text);
    void dele_at_cursor(int how_much);
};

int mystring::set_contents(int in_length, char *in_contents) {
    length = in_length;
    if(!contents) delete contents;
    contents = new char[length+1];
    strcpy(contents,in_contents);
    return length;
}

int mystring::set_contents(char *in_contents) {
    length = strlen(in_contents);
    if(!in_contents) delete contents;
    contents = new char[length+1];
    strcpy(contents,in_contents);
    return length;
}

int edit_string::add_at_cursor(mystring *new_text) {
    int n,k,m;
    char *cp,*pt;
    n = new_text->get_length();
    pt = new_text->get_contents();
    cp = this->get_contents();
    m = this->get_length();
    char * news = new char[n+m+1];
    int i;int j;
    for(i=0;i<cursor;i++){
        news[i] = cp[i];
    }
    k = i;
    for(j=0;j<n;i++,j++){
        news[i] = pt[j];
    }
    cursor = i;
    for(j=k;j<m;j++,i++) news[i] = cp[j];
    news[i] = '\0';
    set_contents(news);
    delete news;
    return cursor;
}


int edit_string::repl_at_cursor(mystring *new_text) {
    int n,m;
    char *pt,*news;
    n = new_text->get_length();
    pt = new_text->get_contents();
    m = this->get_length();
    news = new char[m>n+cursor ?m+1:n+cursor+1];
    news = this->get_contents();
    int i;int j;
    for(i=cursor,j=0;i<n+cursor;j++,i++){
        news[i] = pt[j];
    }
    if(m<n+cursor) news[i] = '\0';
    cursor = i;
    set_contents(news);
    delete news;
    return cursor;
}

void edit_string::dele_at_cursor(int how_much) {
    int m;
    char *cp,*news;
    cp = this->get_contents();
    m=this->get_length();
    int i;
    for(i=cursor;i<m;i++) cp[i] = cp[i+how_much];
    cp[i] = '\0';
}

int main(){
    mystring s1;
    edit_string s2;
    char *cp;
    s1.set_contents("Object_Oriented Programming");
    cp = s1.get_contents();
    s2.set_contents(cp);
    s2.print();
    s2.move_cursor(15);
    s1.set_contents(" Windows");
    s2.add_at_cursor(&s1);
    s2.print();
    s2.move_cursor(6);
    s2.dele_at_cursor(9);
    s2.print();
    s1.set_contents(" TTT");
    s2.repl_at_cursor(&s1);
    s2.print();


    return 0;
}

全部评论

相关推荐

驼瑞驰_招募评论官版...:点击就挂,露头就秒
点赞 评论 收藏
分享
不服输的外卷侠很健谈:杭电来我公司还是很有认可度的,可以来试试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务