⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edit.h

📁 这是本人精心搜集的关于常用图论算法的一套源码
💻 H
字号:
#include <fstream.h>
#include <ctype.h>
#include "str.h"
#include "DLlist.h"

class Editor : public List<String>
{ public:
   Editor(ifstream& file_in, ofstream& file_out);
   bool get_command( );
   void run_command( );
  private:
   ifstream infile;
   ofstream outfile;
   char user_command;
   // auxiliary functions
   Error_code next_line( );
   Error_code previous_line( );
   Error_code goto_line( );
   Error_code insert_line( );
   Error_code substitute_line( );
   Error_code change_line( );
   void read_file( );
   void write_file( );
   void find_string( );
};
Editor::Editor(ifstream& file_in, ofstream& file_out)
/* Post: Initialize theEditor membersinfile andoutfile with the parameters. */
{ infile=file_in;
  outfile=file_out;
}
bool Editor::get_command( ) //Receiving a Command
/* Post: Sets memberuser_command; returns true unless the user's command is q.
   Uses: C library function tolower. */
{ if(current!=NULL)
    cout<<current_position<<" : "<<current->entry.c_str( )<<"\n??" << flush;
  else
    cout<<"File is empty.\n??"<<flush;
  cin>>user_command;   // ignores white space and gets command
  user_command=tolower(user_command);
  while(cin.get() != '\n');    //ignore user's enter key
  if(user_command == 'q') return false;
   else return true;
}

void visit(String &s)
 { cout << s.c_str( ) << endl;  }   

void Editor::run_command( )
/* Post: The command in user_command has been performed.
   Uses: methods and auxiliary functions of theclass Editor , theclass String , and the
         String processing functions. */
{ String temp_string;
  switch (user_command) 
    {case 'b':
       if(empty()) cout<<" Warning: empty buffer \n" ;
        else while(previous_line() == success);
       break;
     case 'c':
       if(empty()) cout<<" Warning: empty buffer \n" ;
        else if( change_line() != success ) cout<<" Error: Substitution failed \n" ;
       break;
     case 'd':
       if(remove(current_position, temp_string) != success)
          cout << " Error: Deletion failed \n" ;
       break;
     case 'e':
       if(empty()) cout<<" Warning: empty buffer \n" ;
        else while(next_line( ) == success);
       break;
     case 'f':
       if(empty()) cout<<" Warning: empty file \n" ;
        else find_string();
       break;
     case 'g':
       if(goto_line() != success)
          cout<<" Warning: No such line \n" ;
       break;
     case '?': case 'h':
       cout<< " Valid commands are: b(egin) c(hange) d(el) e(nd) \n" ;
       cout<< "f(ind) g(o) h(elp) i(nsert) l(ength) n(ext) p(rior) \n" ;
       cout<< "q(uit) r(ead) s(ubstitute) v(iew) w(rite) \n" ;
     case 'i':
       if(insert_line() != success)
         cout << " Error: Insertion failed \n" ;
       break;
     case 'l':
       cout << "There are " << size( ) << "_lines in the file.\n" ;
       if(!empty())
         cout<< "Current_line length is "<<strlen(current->entry)<<endl;
       break;
     case 'n':
       if(next_line( ) != success)
          cout<<" Warning: at end of buffer \n" ;
       break;
     case 'p':
       if(previous_line() != success)
          cout<< " Warning: at start of buffer \n";
       break;
     case 'r':
       read_file( );
       break;
     case 's':
       if(substitute_line() != success)
          cout<< " Error: Substitution failed \n" ;
       break;
     case 'v':
       traverse(visit);
       break;
     case 'w':
       if (empty()) cout << " Warning: Empty file \n" ;
        else write_file();
       break;
     default :
       cout<< "Press h or ? for help or enter a valid command: ";
    }
}

void Editor::read_file( )
/* Pre: Either the Editor is empty or the user authorizes the command.
  Post: The contents of infile are read to the Editor. 
        Any prior contents of the Editor are overwritten.
  Uses: String andEditor methods and functions. */
{ if(!empty())
    { char answer;
      cout<< "Buffer is not empty; the read will destroy it. \n" ;
      cout<< " OK to proceed? (y/n) " ;     cin >> answer; 
      if(tolower(answer)=='n') return;
      clear();
    }
  char ch[80];
  int line_number=0;   String in_string;
  while(infile.getline(ch,80))
   { in_string = ch;  insert(line_number++, in_string);  }
}
 
void Editor::write_file( )
{ if( empty())
     cout<< "Buffer is  empty; the write will top. \n" ;
   else
    {  if(current_position)set_position(0); 
       Node<String> *p=current;
       while(p)	
         { outfile << p->entry.c_str( )<<endl;
           p = p->next; 
         }  
    }   
}

void Editor::find_string( )
/* Pre: The Editor is not empty.
  Post: The current line is advanced until either it contains a copy of a user-selected
        string or it reaches the end of the Editor . If the selected string is found, the
        corresponding line is printed with the string highlighted.
  Uses: String andEditor methods and functions. */
{ int index;  char ch[80];  String search_string;
  cout<< "Enter string to search for: " << endl;
  cin.getline(ch,80); search_string = ch;
  while((index = strstr(current->entry, search_string)) == -1)
     if(next_line() != success) break;
  if(index == -1) cout<< "String was not found.";
   else 
    { cout<< (current->entry).c_str( )<< endl;
      for(int i=0; i<index; i++) cout<< " ";
      for(int j=0; j<strlen(search_string); j++) cout << "^";
    }
  cout << endl;
}

Error_code Editor::insert_line( )
/* Post: A string entered by the user is inserted as a user-selected line_number.
   Uses: String andEditor methods and functions. */
{ int line_number;  char ch[80];   String to_insert; 
  cout<< " Insert what line number? " ;  cin >> line_number;
  while(cin.get() != '\n');
  cout<< " What is the new_line to insert? " ;
  cin.getline(ch,80);    to_insert = ch;
  return insert(line_number, to_insert);
}

Error_code Editor::change_line( )
/* Pre: The Editor is not empty.
  Post: If a user-specified string appears in the current line, it is replaced by a new
        user-selected string. Else: an Error code is returned.
  Uses: String andEditor methods and functions. */
{ String old_text,new_text;   char ch[80];
  Error_code result = success;
  cout << " What text segment do you want to replace? " << flush;
  cin.getline(ch,80);  old_text = ch; 
  cout << " What new_text segment do you want to add in? " << flush; 
  cin.getline(ch,80);   new_text = ch;  
  int index = strstr(current->entry, old_text);
  if(index == -1) result = fail;
   else 
    { String new_line;
      strncpy(new_line, current->entry, index);
      strcat(new_line, new_text);
      const char *old_line = (current->entry).c_str( );
      String rear=(String)(old_line+index+strlen(old_text));  
      strcat(new_line,rear );
      current->entry = new_line;
    }
  return result;
}

Error_code Editor::next_line( )
{ if(current_position == count)
    { cout<< "already to the last line \n ";
      return  fail;
    }
  current = current ->next;
  current_position++; 
  return  success;
}
Error_code Editor::previous_line( )
{ if(current_position == 0)
    { cout<< "already to the first line \n ";
      return  fail;
    }
  current = current -> back;
  current_position--; 
  return success;
}

Error_code Editor::goto_line()
{ int line_number;
  cout<< " goto what line number? " << flush;
  cin >> line_number;
  if(line_number<0 || line_number>=count)
    { cout<< "Error: line number range error! \n ";
      return  fail;
    }
  set_position(line_number);
  return success;  
}
Error_code Editor::substitute_line()
{ char ch[80];  String new_line,old_line;  int line_number;   
  cout << " Which line do you want to substitute? " << flush;
  cin >> line_number;   while(cin.get() != '\n');
  if(line_number<0 || line_number>=count)
    { cout<< "Error: line number range error! \n ";
      return fail;
    }
  else
    { cout<<" What new_text content do you want to add in? " << flush;
      cin.getline(ch,80);  new_line = ch;
      if(remove(line_number, old_line)!= success)
        { cout << " Error: can't delete old line! \n"; 
          return fail;
        }  
      return insert(line_number, new_line);
    }
  return success;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -