主题:[讨论]大家请进来看看,这个程序那里错了啊!~!~请帮忙改一下
#include<stdio.h>
#include<string>
//基本思想:打开文件,将文件中的所有内容读到链表中, 再进行相关的操作
//操作完成后, 再把内容写入文件中
typedef StrNode{
char str[500];
StrNode *prev, *next;
int colum;
StrNode(){
strcpy( str, "" );
colum=-1;
prev=next=NULL;
}
};
typedef TextEditor{
char filename[256];
StrNode *head, *tail, *fence;
bool is_in( char *s0, char *s1, int pos);//检查s0中从pos开始的串是否能
//和s1匹配
int GetLineNumber() ;//得到改动后文件的行数
TextEditor();
~TextEditor();
void OnStart();//打开文件并把内容写入链表
void OnEnd();//把内容写入到文件中去保存
void OnSave();//保存一次, 避免数据丢失
void Append();//在文件的尾部添加内容
void DeleteLine();//删除行
void DeleteSubStr();//删除一个字串
void SearchSubStr();//寻找子串并显示出来
void ChangeSubStr();//替换子串成新的串
void InsertStr();//在指定的地点插入一个子串
void InsertLine(); //在指定地点插入一行
void DoAll();//接口函数
void display();//显示当前文件的内容
};
TextEditor::TextEditor(){
printf("请输入文件名(提示:string.txt):" );
//strcpy( filename, "string.txt" );
scanf("%c",&filename);
head=tail=fence=NULL;
return;
TextEditor::~TextEditor(){
while( head!=NULL ){ fence=head; head=head->next; delete fence; }
printf( "--->析构函数!\n" );
return;
bool TextEditor::is_in( char *s0, char *s1, int pos){
assert( pos <= (int)(strlen(s0)-strlen(s1)) );
int i, j=0;
for( j=0, i=pos; j<(int)strlen(s1); i++, j++){
if( s0[i]!=s1[j] ) return false;
}
return true;
int TextEditor::GetLineNumber(){
if( tail== NULL ) return 0;
else return tail->colum;
void TextEditor::OnStart(){
ifstream inf( filename );
if( !inf.is_open() ){
printf("无法打开文件: " );
printf("filename\n");
exit( 1 );
}
int linenumber; //文件的行数
char ch;
scanf("%d",&linenumber);
scanf("%c",&ch);
scanf.putback( ch );// 把多读的字符送回去
static int col=1;
while ( linenumber-- ){
StrNode *tmp=new StrNode;
inf.getline( tmp->str, 500 );
tmp->colum=-1; //仅作为初始化
tmp->next=tmp->prev=NULL;
if( head==NULL ){
head=tail=fence=tmp;
tmp->colum=col++;
}
else{
tmp->colum=col++;
tmp->prev=tail;
tail->next=tmp;
tail=tmp;
}
}
inf.close();
}
void TextEditor::OnSave(){
StrNode *ptmp=head;
ofstream outf;
outf.open( filename );
if( !outf.is_open() ){
printf("Warning! 无法保存文件!");
exit( 1 );
}
printf("GetLineNumber()");//写入行数
while( ptmp!= NULL ){//逐行写入字符串
printf("ptmp->str");
ptmp=ptmp->next;
}
outf.close();
printf( "--->保存完毕!" );
}
void TextEditor::OnEnd(){ OnSave(); }
void TextEditor::display(){//显示文件的内容, 即整个文档
int i;
for( i=0; i<6; i++ ) printf("*");
printf(" 文件名:");
printf("filename");
printf(" ");
for( i=0; i<6; i++ ) printf("*\n");
StrNode *ptmp=head;
while( ptmp ){
printf( "Line " );
printf("ptmp->colum ");
printf(" :" );
printf("ptmp->str\n");
ptmp = ptmp->next;
}
printf( "*****以上是文件的内容*****\n");
}
void TextEditor::Append(){
char s[500];
if( head==NULL ){
cin.getline( s, 500 );//clear the stream
printf("当前文件的内容为空, ");
printf("开始编辑(请单独一行输入'#'表示输入结束):");
}
else{
printf("请单独一行输入'#'表示输入结束");
printf("在最后一行添加请输入:0 否则另起新行 请选择: ");
char flag;//判断标志字符
scanf("%c",&flag);
scanf.getline( s, 500 );//clear the buffer stream
if( flag=='0' ){//先完成最后一行内容的输入, 即添加到最后一行
printf( "Line " );
printf("tail->colum ");
printf( " :");
printf("tail->str");
scanf.getline( s, 500 );
if( s[0]!='#' ) strcat( tail->str, s );
else goto END;
}
}
//以下是添加到新行的内容
while(printf("Line ");printf("((tail!=NULL)?(tail->colum+1):1)");printf(" :");)
scanf.getline(s, 500),s[0]!='#' ){
fence=new StrNode;
strcpy( fence->str, s );
if( tail != NULL ){
fence->colum=tail->colum+1;
fence->prev=tail;
tail->next=fence;
tail=fence;
}
else{//如果当前是空文本
head=tail=fence;
fence->colum=1;
fence->prev=fence->next=NULL;
}
}
END: return ;
}
void TextEditor::DeleteLine(){//删除某一行的操作
if( head == NULL ){
printf( "当前的文件为空, 请进行其它操作!\n" );
return ;
}
printf("当前文件共有:")
printf("tail->colum ");
printf(" 行, 输入删除行号: ");
int line;
scanf("%d",&line);
if( line > tail->colum || line < head->colum )
printf("第 ");
printf("line");
printf("" 行不存在! 请继续其它操作。\n");
else{
if(fence==NULL) fence=head;
while( fence->colum < line ) fence=fence->next;
while( fence->colum >line ) fence=fence->prev;
//fence points to the line
if( fence->prev == NULL ) {//如果删除的是第一行
head=fence->next;
if( head != NULL ) head->prev=NULL;
else tail=NULL;//已经被删完了, tail 指针必须置 NULL
delete fence;
fence=head;
}
else if( fence == tail ){//如果删除的是最后一行
tail=fence->prev;
if( tail!= NULL ) tail->next=NULL;
delete fence;
fence=NULL;
}
else{// 删除中间结点
StrNode *ptmp=fence->next;//备用, 不至于丢掉
fence->prev->next=fence->next;
fence->next->prev=fence->prev;
delete fence;
fence=ptmp;
}
//改变删除行之后各结点的行标
StrNode *ptmp1=fence;
while( ptmp1 ){ ptmp1->colum--; ptmp1=ptmp1->next; }
printf("---> 删除成功!\n");
}
return ;
}
void TextEditor::DeleteSubStr(){
printf("输入要删除的子串: ");
char s[500];
scanf.getline(s, 500);
scanf.getline(s, 500);
fence=head;
while( fence ){
int i;
for( i=0; i < (int)strlen(fence->str)-strlen(s); i++ ) {
if( is_in(fence->str, s, i )){
int j, k;
for( j=i, k=i+(int)strlen(s); fence->str[k]!='\0'; j++ , k++ )
fence->str[j]=fence->str[k];
fence->str[j]='\0';
}
}
fence=fence->next;
}
printf("--->删除成功!\n");
}
void TextEditor::SearchSubStr(){
printf("输入搜索内容: ");
char s[500];
scanf.getline( s, 500 );
scanf.getline( s, 500 );
if( !strcmp( s, "" ) ){
printf("搜索内容不能为空!\n");
SearchSubStr();
return ;
}
fence = head;
bool flag=false;
int len0 = 0, len1 = strlen( s );
while( fence ){
len0 = strlen(fence->str);
if( len0 < len1 ) fence = fence->next;
else{ // 在单个串中找所有的子串并显示出来
int i;
for( i = 0; i<len0-len1; i++ ){
if( is_in( fence->str, s, i ) ){//如果在这个串中有要求的子串, 则输出此行
printf("Line " );
printf("fence->colum, Colum");
printf("i");
printf(" :");
printf("fence->str\n");
flag=true;
}
}
fence = fence->next;
}
}
if( !flag ) printf("未找到字符串:");
printf("s \n)";
printf("--->文档搜索完毕!\n");
}
void TextEditor::ChangeSubStr(){
char s0[500], s1[500];
char ch;
printf("请输入源串(被替换串):");
scanf("%c",&ch); scanf.putback(ch);
scanf.getline( s0, 500 );
if( s0[0]=='\0' ) return;//空串是不需做什么的, 返回
printf("请输入目的串(替换串): ");
scanf.getline( s1, 500 );
//替换工作
fence = head;
while( fence ){
int len0 = strlen( fence->str );
int i;
for( i = 0; i < len0-(int)strlen( s0 ); ){
if( is_in( fence->str, s0, i ) ){//确定是不是有要被替换的串在这个串中
char stemp[500];
int j = i + (int)strlen( s0 ), k=0;
while( fence->str[j] != '\0' )//将被替换串的后面的串保存起来
stemp[k++] = fence->str[j++];
stemp[k]='\0';
fence->str[i] = '\0';
if( s1[0] != '\0' ) strcat( fence->str, s1 );//插入新的串
strcat( fence->str, stemp );//连接后半部分到原来的串中
i += strlen ( s1 );
}
else i++;
}
fence = fence->next;
}
printf( "--->替换完毕!\n" );
return ;
}
void TextEditor::InsertStr(){ // 插入子串的操作
if( head == NULL ){
printf("当前文件为空, 无法插入串, 请继续其它操作.\n");
return ;
}
int line, col;
char flag;
fence = head;
while( 1 ){ //确定行号
printf( "请输入插入点的行号( ");
printf("head->colum ");
printf("-" );
printf("tail->colum ");
printf(" ): ");
scanf("%d",&line);
if( line > tail->colum || line < head->colum )
printf( "行 ");
printf("line ");
printf("非法!\n");
else{
while( fence->colum != line )
if( fence->colum > line ) fence = fence->prev;
else fence = fence->next;
printf("Line ");
printf("line ");
printf(" :");
printf("fence->str\n");
}
do{
printf( "选命令:取消-q 换行-c 置列-s 输入:");
scanf("%c",&flag);
if( flag == 'q' ) return ;
if( flag != 'c' && flag != 's' && flag != 'q')
printf( "Error! 非法命令! 请重新选择。。。\n" );
}while( flag != 'c' && flag != 's' );
if( flag == 's' ) break;
}
//设置列号, 确定最终的插入位置
int len = strlen( fence->str ), k=0;
while( 1 ){
k = 0;
printf( "选择列号( 0-" << len << "): ");
scanf("%d",&col);
if( col < 0 ) printf( "列 " << col << "非法!\n");
else{
printf( "当前列前的内容为:");
if( col >= len ) printf("fence->str\n");
else{
for( k = 0; k < col; k++ ) printf(" fence->str[k]\n");
}
}
do{
printf("选择命令:取消-q 确定-y 重选-r 输入:");
cin >> flag;
if( flag == 'q' ) return ;
if( flag != 'y' && flag != 'r' && flag != 'q' )
printf("Error! 非法命令! 请重新选择。。。\n");
}while( flag != 'y' && flag != 'r' );
if( flag == 'y' ) break;
}
// 当前行列都已选定
int t, j=0;
char s0[500], s1[500];
for( t=col; t<(int)strlen(fence->str); t++ )//保存插入位置右边的内容
s0[j++]=fence->str[t];
s0[j]='\0';
fence->str[col]='\0';
for( t = 0; t < col; t++ ) printf(" fence->str[t]");
scanf.getline( s1, 500 );//clear the input stream
scanf.getline( s1, 500 );
strcat( fence->str, s1 );
strcat( fence->str, s0 );
printf("--->插入完成!\n");
return ;
}
#include<string>
//基本思想:打开文件,将文件中的所有内容读到链表中, 再进行相关的操作
//操作完成后, 再把内容写入文件中
typedef StrNode{
char str[500];
StrNode *prev, *next;
int colum;
StrNode(){
strcpy( str, "" );
colum=-1;
prev=next=NULL;
}
};
typedef TextEditor{
char filename[256];
StrNode *head, *tail, *fence;
bool is_in( char *s0, char *s1, int pos);//检查s0中从pos开始的串是否能
//和s1匹配
int GetLineNumber() ;//得到改动后文件的行数
TextEditor();
~TextEditor();
void OnStart();//打开文件并把内容写入链表
void OnEnd();//把内容写入到文件中去保存
void OnSave();//保存一次, 避免数据丢失
void Append();//在文件的尾部添加内容
void DeleteLine();//删除行
void DeleteSubStr();//删除一个字串
void SearchSubStr();//寻找子串并显示出来
void ChangeSubStr();//替换子串成新的串
void InsertStr();//在指定的地点插入一个子串
void InsertLine(); //在指定地点插入一行
void DoAll();//接口函数
void display();//显示当前文件的内容
};
TextEditor::TextEditor(){
printf("请输入文件名(提示:string.txt):" );
//strcpy( filename, "string.txt" );
scanf("%c",&filename);
head=tail=fence=NULL;
return;
TextEditor::~TextEditor(){
while( head!=NULL ){ fence=head; head=head->next; delete fence; }
printf( "--->析构函数!\n" );
return;
bool TextEditor::is_in( char *s0, char *s1, int pos){
assert( pos <= (int)(strlen(s0)-strlen(s1)) );
int i, j=0;
for( j=0, i=pos; j<(int)strlen(s1); i++, j++){
if( s0[i]!=s1[j] ) return false;
}
return true;
int TextEditor::GetLineNumber(){
if( tail== NULL ) return 0;
else return tail->colum;
void TextEditor::OnStart(){
ifstream inf( filename );
if( !inf.is_open() ){
printf("无法打开文件: " );
printf("filename\n");
exit( 1 );
}
int linenumber; //文件的行数
char ch;
scanf("%d",&linenumber);
scanf("%c",&ch);
scanf.putback( ch );// 把多读的字符送回去
static int col=1;
while ( linenumber-- ){
StrNode *tmp=new StrNode;
inf.getline( tmp->str, 500 );
tmp->colum=-1; //仅作为初始化
tmp->next=tmp->prev=NULL;
if( head==NULL ){
head=tail=fence=tmp;
tmp->colum=col++;
}
else{
tmp->colum=col++;
tmp->prev=tail;
tail->next=tmp;
tail=tmp;
}
}
inf.close();
}
void TextEditor::OnSave(){
StrNode *ptmp=head;
ofstream outf;
outf.open( filename );
if( !outf.is_open() ){
printf("Warning! 无法保存文件!");
exit( 1 );
}
printf("GetLineNumber()");//写入行数
while( ptmp!= NULL ){//逐行写入字符串
printf("ptmp->str");
ptmp=ptmp->next;
}
outf.close();
printf( "--->保存完毕!" );
}
void TextEditor::OnEnd(){ OnSave(); }
void TextEditor::display(){//显示文件的内容, 即整个文档
int i;
for( i=0; i<6; i++ ) printf("*");
printf(" 文件名:");
printf("filename");
printf(" ");
for( i=0; i<6; i++ ) printf("*\n");
StrNode *ptmp=head;
while( ptmp ){
printf( "Line " );
printf("ptmp->colum ");
printf(" :" );
printf("ptmp->str\n");
ptmp = ptmp->next;
}
printf( "*****以上是文件的内容*****\n");
}
void TextEditor::Append(){
char s[500];
if( head==NULL ){
cin.getline( s, 500 );//clear the stream
printf("当前文件的内容为空, ");
printf("开始编辑(请单独一行输入'#'表示输入结束):");
}
else{
printf("请单独一行输入'#'表示输入结束");
printf("在最后一行添加请输入:0 否则另起新行 请选择: ");
char flag;//判断标志字符
scanf("%c",&flag);
scanf.getline( s, 500 );//clear the buffer stream
if( flag=='0' ){//先完成最后一行内容的输入, 即添加到最后一行
printf( "Line " );
printf("tail->colum ");
printf( " :");
printf("tail->str");
scanf.getline( s, 500 );
if( s[0]!='#' ) strcat( tail->str, s );
else goto END;
}
}
//以下是添加到新行的内容
while(printf("Line ");printf("((tail!=NULL)?(tail->colum+1):1)");printf(" :");)
scanf.getline(s, 500),s[0]!='#' ){
fence=new StrNode;
strcpy( fence->str, s );
if( tail != NULL ){
fence->colum=tail->colum+1;
fence->prev=tail;
tail->next=fence;
tail=fence;
}
else{//如果当前是空文本
head=tail=fence;
fence->colum=1;
fence->prev=fence->next=NULL;
}
}
END: return ;
}
void TextEditor::DeleteLine(){//删除某一行的操作
if( head == NULL ){
printf( "当前的文件为空, 请进行其它操作!\n" );
return ;
}
printf("当前文件共有:")
printf("tail->colum ");
printf(" 行, 输入删除行号: ");
int line;
scanf("%d",&line);
if( line > tail->colum || line < head->colum )
printf("第 ");
printf("line");
printf("" 行不存在! 请继续其它操作。\n");
else{
if(fence==NULL) fence=head;
while( fence->colum < line ) fence=fence->next;
while( fence->colum >line ) fence=fence->prev;
//fence points to the line
if( fence->prev == NULL ) {//如果删除的是第一行
head=fence->next;
if( head != NULL ) head->prev=NULL;
else tail=NULL;//已经被删完了, tail 指针必须置 NULL
delete fence;
fence=head;
}
else if( fence == tail ){//如果删除的是最后一行
tail=fence->prev;
if( tail!= NULL ) tail->next=NULL;
delete fence;
fence=NULL;
}
else{// 删除中间结点
StrNode *ptmp=fence->next;//备用, 不至于丢掉
fence->prev->next=fence->next;
fence->next->prev=fence->prev;
delete fence;
fence=ptmp;
}
//改变删除行之后各结点的行标
StrNode *ptmp1=fence;
while( ptmp1 ){ ptmp1->colum--; ptmp1=ptmp1->next; }
printf("---> 删除成功!\n");
}
return ;
}
void TextEditor::DeleteSubStr(){
printf("输入要删除的子串: ");
char s[500];
scanf.getline(s, 500);
scanf.getline(s, 500);
fence=head;
while( fence ){
int i;
for( i=0; i < (int)strlen(fence->str)-strlen(s); i++ ) {
if( is_in(fence->str, s, i )){
int j, k;
for( j=i, k=i+(int)strlen(s); fence->str[k]!='\0'; j++ , k++ )
fence->str[j]=fence->str[k];
fence->str[j]='\0';
}
}
fence=fence->next;
}
printf("--->删除成功!\n");
}
void TextEditor::SearchSubStr(){
printf("输入搜索内容: ");
char s[500];
scanf.getline( s, 500 );
scanf.getline( s, 500 );
if( !strcmp( s, "" ) ){
printf("搜索内容不能为空!\n");
SearchSubStr();
return ;
}
fence = head;
bool flag=false;
int len0 = 0, len1 = strlen( s );
while( fence ){
len0 = strlen(fence->str);
if( len0 < len1 ) fence = fence->next;
else{ // 在单个串中找所有的子串并显示出来
int i;
for( i = 0; i<len0-len1; i++ ){
if( is_in( fence->str, s, i ) ){//如果在这个串中有要求的子串, 则输出此行
printf("Line " );
printf("fence->colum, Colum");
printf("i");
printf(" :");
printf("fence->str\n");
flag=true;
}
}
fence = fence->next;
}
}
if( !flag ) printf("未找到字符串:");
printf("s \n)";
printf("--->文档搜索完毕!\n");
}
void TextEditor::ChangeSubStr(){
char s0[500], s1[500];
char ch;
printf("请输入源串(被替换串):");
scanf("%c",&ch); scanf.putback(ch);
scanf.getline( s0, 500 );
if( s0[0]=='\0' ) return;//空串是不需做什么的, 返回
printf("请输入目的串(替换串): ");
scanf.getline( s1, 500 );
//替换工作
fence = head;
while( fence ){
int len0 = strlen( fence->str );
int i;
for( i = 0; i < len0-(int)strlen( s0 ); ){
if( is_in( fence->str, s0, i ) ){//确定是不是有要被替换的串在这个串中
char stemp[500];
int j = i + (int)strlen( s0 ), k=0;
while( fence->str[j] != '\0' )//将被替换串的后面的串保存起来
stemp[k++] = fence->str[j++];
stemp[k]='\0';
fence->str[i] = '\0';
if( s1[0] != '\0' ) strcat( fence->str, s1 );//插入新的串
strcat( fence->str, stemp );//连接后半部分到原来的串中
i += strlen ( s1 );
}
else i++;
}
fence = fence->next;
}
printf( "--->替换完毕!\n" );
return ;
}
void TextEditor::InsertStr(){ // 插入子串的操作
if( head == NULL ){
printf("当前文件为空, 无法插入串, 请继续其它操作.\n");
return ;
}
int line, col;
char flag;
fence = head;
while( 1 ){ //确定行号
printf( "请输入插入点的行号( ");
printf("head->colum ");
printf("-" );
printf("tail->colum ");
printf(" ): ");
scanf("%d",&line);
if( line > tail->colum || line < head->colum )
printf( "行 ");
printf("line ");
printf("非法!\n");
else{
while( fence->colum != line )
if( fence->colum > line ) fence = fence->prev;
else fence = fence->next;
printf("Line ");
printf("line ");
printf(" :");
printf("fence->str\n");
}
do{
printf( "选命令:取消-q 换行-c 置列-s 输入:");
scanf("%c",&flag);
if( flag == 'q' ) return ;
if( flag != 'c' && flag != 's' && flag != 'q')
printf( "Error! 非法命令! 请重新选择。。。\n" );
}while( flag != 'c' && flag != 's' );
if( flag == 's' ) break;
}
//设置列号, 确定最终的插入位置
int len = strlen( fence->str ), k=0;
while( 1 ){
k = 0;
printf( "选择列号( 0-" << len << "): ");
scanf("%d",&col);
if( col < 0 ) printf( "列 " << col << "非法!\n");
else{
printf( "当前列前的内容为:");
if( col >= len ) printf("fence->str\n");
else{
for( k = 0; k < col; k++ ) printf(" fence->str[k]\n");
}
}
do{
printf("选择命令:取消-q 确定-y 重选-r 输入:");
cin >> flag;
if( flag == 'q' ) return ;
if( flag != 'y' && flag != 'r' && flag != 'q' )
printf("Error! 非法命令! 请重新选择。。。\n");
}while( flag != 'y' && flag != 'r' );
if( flag == 'y' ) break;
}
// 当前行列都已选定
int t, j=0;
char s0[500], s1[500];
for( t=col; t<(int)strlen(fence->str); t++ )//保存插入位置右边的内容
s0[j++]=fence->str[t];
s0[j]='\0';
fence->str[col]='\0';
for( t = 0; t < col; t++ ) printf(" fence->str[t]");
scanf.getline( s1, 500 );//clear the input stream
scanf.getline( s1, 500 );
strcat( fence->str, s1 );
strcat( fence->str, s0 );
printf("--->插入完成!\n");
return ;
}