linux设备驱动第三篇:如何写一个简单的字符设
展开全部
#include<iostream>
#include<cstring>
using namespace std;
class string
{
private:
char *str;
public:
string(char *s);
~string(){delete str;};
int getlen(){return strlen(str)+1;}
char *get(){return str;}
void print();
};
string::string(char *s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
cout<<"constructing string"<<endl;
}
void string::print()
{
cout<<"原数组为:"<<str<<endl;
}
class editstring : public string
{
private:
char *str;
unsigned int cursor;
public:
editstring(char *s);
~editstring(){delete str;};
void setcurright()
{
if(cursor<strlen(str)+1)
cursor++;
cout<<"光标点右移为:"<<cursor<<endl;
};
void setcurleft()
{
if(cursor>0)
cursor--;
cout<<"光标点左移为:"<<cursor<<endl;
};
void insert(char c);
void deletes();
char *get(){return str;}
void replace(char c);
void print();
};
editstring::editstring(char *s):string(s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
cursor=0;
cout<<"constructing editstring"<<endl;
}
void editstring::insert(char c)
{
cout<<"插入字母:"<<c<<""<<endl<<"目前光标点为"<<cursor<<""<<endl;
int max=strlen(str)+1;
max++;
char *temp=new char[max];
strcpy(temp,str);
str=new char[max];
strcpy(str,temp);
unsigned int j;
for(j=cursor;j<max-1;j++)
{
str[j+1]=temp[j];
}
str[cursor]=c;
delete []temp; //delete temp.一样吗?
}
void editstring::deletes()
{
cout<<"删除函数被调用。"<<endl<<"目前光标点为"<<cursor<<""<<endl;
int max=strlen(str)+1;
int i=0;
for(i=cursor;i<max;i++)
{
str[i]=str[i+1];
}
i--;
str[i]='\0';
}
void editstring::replace(char c)
{
cout<<"替换函数被调用。"<<endl<<"目前光标点为"<<cursor<<""<<endl;
str[cursor]=c;
}
void editstring::print()
{
cout<<"编辑后的数组为:"<<str<<endl;
}
void main()
{
editstring p("china");
p.string::print();
p.setcurright();
p.setcurright();
p.setcurleft();
p.deletes();
p.editstring::print();
p.insert('a');
p.editstring::print();
p.deletes();
p.editstring::print();
p.replace('s');
p.editstring::print();
}
#include<cstring>
using namespace std;
class string
{
private:
char *str;
public:
string(char *s);
~string(){delete str;};
int getlen(){return strlen(str)+1;}
char *get(){return str;}
void print();
};
string::string(char *s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
cout<<"constructing string"<<endl;
}
void string::print()
{
cout<<"原数组为:"<<str<<endl;
}
class editstring : public string
{
private:
char *str;
unsigned int cursor;
public:
editstring(char *s);
~editstring(){delete str;};
void setcurright()
{
if(cursor<strlen(str)+1)
cursor++;
cout<<"光标点右移为:"<<cursor<<endl;
};
void setcurleft()
{
if(cursor>0)
cursor--;
cout<<"光标点左移为:"<<cursor<<endl;
};
void insert(char c);
void deletes();
char *get(){return str;}
void replace(char c);
void print();
};
editstring::editstring(char *s):string(s)
{
str=new char[strlen(s)+1];
strcpy(str,s);
cursor=0;
cout<<"constructing editstring"<<endl;
}
void editstring::insert(char c)
{
cout<<"插入字母:"<<c<<""<<endl<<"目前光标点为"<<cursor<<""<<endl;
int max=strlen(str)+1;
max++;
char *temp=new char[max];
strcpy(temp,str);
str=new char[max];
strcpy(str,temp);
unsigned int j;
for(j=cursor;j<max-1;j++)
{
str[j+1]=temp[j];
}
str[cursor]=c;
delete []temp; //delete temp.一样吗?
}
void editstring::deletes()
{
cout<<"删除函数被调用。"<<endl<<"目前光标点为"<<cursor<<""<<endl;
int max=strlen(str)+1;
int i=0;
for(i=cursor;i<max;i++)
{
str[i]=str[i+1];
}
i--;
str[i]='\0';
}
void editstring::replace(char c)
{
cout<<"替换函数被调用。"<<endl<<"目前光标点为"<<cursor<<""<<endl;
str[cursor]=c;
}
void editstring::print()
{
cout<<"编辑后的数组为:"<<str<<endl;
}
void main()
{
editstring p("china");
p.string::print();
p.setcurright();
p.setcurright();
p.setcurleft();
p.deletes();
p.editstring::print();
p.insert('a');
p.editstring::print();
p.deletes();
p.editstring::print();
p.replace('s');
p.editstring::print();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询