阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】字符串在程序设计中扮演着重要角色。现需要设计字符串基类string,包含设置字 符串、返回字符串长度及内容等功能。另有一个具有编辑功能的串类edlt_string,派生于string,在其中设置一个光标,使其能支持在光标处的插入、删除操作。【程序】include <iostream.h>include <stdio.h>include <string.h>class string{int length;char *data;public:int get_length() {return length;}char *get_data() {return data;}~string() {delete data;}int set data(int in_length, char *in_data);int set_data(char *data);void print() {cout<<data<<endl;}};class edit_string: public string{int cursor;public:int get_cursor() {return cursor;}void move_cursor(int dis) {cursor=dis;}int add_data(string *new_data);void delete_data(int num);};int string::set_data(int in_length,char *in_data){length=in_length;if(!data)delete data;(1)strcpy(data,in_data);return length;}int string::set data(char *in_data){(2)if(!data)delete data;(1)strcpy(data,in_data);return length;}int edit_string::add_data(string *new_data){int n,k,m;char *cp,*pt;n=new_data->get_length();pt=new_data->get_data();cp=this->get_data();m=this->get_length();char *news=new char[n+m+1];for(int i=0; i<cursor; i++)news[i]=cp[i];k=i;for(int j=0; j<n; i++,j++)news[i]=pt[j];cursor=i;for(j=k; j<m; j++,i++)(3)news[i]='\0';(4)delete news;return cursor;}void edit string::delete_data( int num){int m;char *cp;cp=this->get_data();m=this->get_length();for(int i=cursor; i<m; i++)(5)cp[i]='\0';}
阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。
【说明】
字符串在程序设计中扮演着重要角色。现需要设计字符串基类string,包含设置字 符串、返回字符串长度及内容等功能。另有一个具有编辑功能的串类edlt_string,派生于string,在其中设置一个光标,使其能支持在光标处的插入、删除操作。
【程序】
include <iostream.h>
include <stdio.h>
include <string.h>
class string
{
int length;
char *data;
public:
int get_length() {return length;}
char *get_data() {return data;}
~string() {delete data;}
int set data(int in_length, char *in_data);
int set_data(char *data);
void print() {cout<<data<<endl;}
};
class edit_string: public string
{
int cursor;
public:
int get_cursor() {return cursor;}
void move_cursor(int dis) {cursor=dis;}
int add_data(string *new_data);
void delete_data(int num);
};
int string::set_data(int in_length,char *in_data)
{
length=in_length;
if(!data)
delete data;
(1)
strcpy(data,in_data);
return length;
}
int string::set data(char *in_data)
{
(2)
if(!data)
delete data;
(1)
strcpy(data,in_data);
return length;
}
int edit_string::add_data(string *new_data)
{
int n,k,m;
char *cp,*pt;
n=new_data->get_length();
pt=new_data->get_data();
cp=this->get_data();
m=this->get_length();
char *news=new char[n+m+1];
for(int i=0; i<cursor; i++)
news[i]=cp[i];
k=i;
for(int j=0; j<n; i++,j++)
news[i]=pt[j];
cursor=i;
for(j=k; j<m; j++,i++)
(3)
news[i]='\0';
(4)
delete news;
return cursor;
}
void edit string::delete_data( int num)
{
int m;
char *cp;
cp=this->get_data();
m=this->get_length();
for(int i=cursor; i<m; i++)
(5)
cp[i]='\0';
}