void Editstring::add(Tstring &s)
{
int n,k;
char *pt;
pt=s.getstr();
n=s.getlen();
char *newstr=new char[n+len+1];
for(int i=0;i<cursor;i++)
newstr[i]=str[i];
k=i;
for(int j=0;j<n;i++,j++)
newstr[i]=pt[j];
cursor=i;
for(j=k;j<=len;j++,i++)
newstr[i]=str[j];
delete []str;
str=new char[n+len+1];
strcpy(str,newstr);
delete []newstr;
}
void Editstring::add(Tstring &s) { int n,k,i,j; char *pt; pt=s.getstr(); n=s.getlen(); char *newstr=new char[n+len+1]; for(i=0;i<cursor;i++) newstr[i]=str[i]; k=i; for(j=0;j<n;i++,j++) newstr[i]=pt[j]; cursor=i; for(j=k;j<=len;j++,i++) newstr[i]=str[j]; delete []str; str=new char[n+len+1]; strcpy(str,newstr); delete []newstr; } //你的 i和 j 都是在循环里面声明的,属于局部变量。作用域在循环里面。出了循环就没效果了。
你把 i 和 j 在开头就定义好。
声明后运行,在strcpy(str,newstr)卡住了,运行不下去
@山顶水下: 你先看看你写的代码,有 for 语句的都用大括号 { }
包含起来,要不然你每个语句的作用域自己看的都不清楚。
@BUTTERAPPLE:
//estring.h #include #include class Tstring { protected: int len; char *str; public: Tstring(char *); ~Tstring(); void setstr(char *s); int getlen(); char *getstr(); void disp(); }; class Editstring:public Tstring { private: int cursor; public: Editstring(char *s,int); ~Editstring(); void movecursor(int off); void add(Tstring&s); void replace(Tstring&s); void erase(int pos); int getcursor(){return cursor;} }; //estring.cpp #include #include"estring.h" //#include //#include Tstring::Tstring(char *s=NULL) { len=strlen(s); str=new char[len+1]; strcpy(str,s); } Tstring::~Tstring() { delete []str; } void Tstring::setstr(char *s) { if(strlen(s)!=len) { delete []str; len=strlen(s)+1; str=new char[len+1]; } strcpy(str,s); } int Tstring::getlen() { return len; } char * Tstring::getstr() { return str; } void Tstring::disp() { std::cout<<str<<std::endl; } Editstring::Editstring(char *s,int off):Tstring(s) { cursor=off; } Editstring::~Editstring(){} void Editstring::movecursor(int off) { cursor=off>len?len:off; } void Editstring::add(Tstring &s) { int n,k,i,j; char *pt; pt=s.getstr(); n=s.getlen(); char *newstr=new char[n+len+1]; for(int i=0;i<cursor;i++) newstr[i]=str[i]; k=i; for(int j=0;j<n;i++,j++) newstr[i]=pt[j]; cursor=i; for(j=k;j<=len;j++,i++) newstr[i]=str[j]; delete []str; str=new char[n+len+1]; strcpy(str,newstr); delete []newstr; } void Editstring::replace(Tstring &s) { int n,t; char *pt; int i; pt=s.getstr(); n=s.getlen(); t=n+cursor<=len?n+cursor:len; for(int i=cursor,j=0;i<t;j++,i++) str[i]=pt[j]; cursor=i; } void Editstring::erase(int pos) { if(cursor+pos<len) { for(int i=cursor;i<=len;i++) str[i]=str[i+pos]; } else str[cursor]='\0'; } //main.cpp # include"estring.h" using namespace std; void main() { Tstring s1("visual"); Tstring s2("???"); Editstring es("Object-oriented programming!",0); std::cout<<"编辑字符串:"; es.disp(); std::cout<<"光标当前位置:"<<es.getcursor()<<std::endl; es.movecursor(16); std::cout<<"移动后光标位置:"<<es.getcursor()<<std::endl; std::cout<<"要插入的字符串:"; s1.disp(); es.add(s1); std::cout<<"插入后字符串:"; es.disp(); std::cout<<"当前光标位置:"<<es.getcursor()<<std::endl; es.movecursor(7); std::cout<<"移动后的光标位置:"<<es.getcursor()<<std::endl; std::cout<<"替换字符串:"; s2.disp(); es.replace(s2); std::cout<<"替换后字符串:"; es.disp(); es.erase(5); std::cout<<"删除后字符串:"; es.disp(); }
@BUTTERAPPLE:
加大括号..写代码好好加大括号.
for循环,不要括号吗????
for (int i = 0; i < 10; i++) {
}
不在定义的范围内