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

📄 shit5.cpp

📁 本程序是用c++实现的多功能文本编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	Error_code substitute_line();
	Error_code change_line();
	Error_code copy_line();
	void save_file();
	void read_file();
	void write_file();
	void find_string();
};
Editor::Editor()
{
  mode=0;
}

bool Editor::get_command()

{  
   if (current != NULL)
   {
      cout << current_position+1 << " : "
           << current->entry.c_str();
   if(mode==0)cout<< "\n请输入你想进行的操作:" << flush;
   else       cout<< "\nInput the operate: "<<flush;
   }
   else
   {
	 if(mode==0) 
		 cout << "缓冲区是空的\n请输入你想进行的操作:" << flush;
	 else
		 cout << "File is empty.\nInput the operate:"<<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 Editor::run_command()
/*
Post: The command in user_command has been performed.
Uses: methods and auxiliary functions of the class Editor,
      the class String, and the String processing functions.
*/
{
   String temp_string;
   switch (user_command) {
   case 'a':
	   if(empty())
	   {
		     if(mode==0)cout<<"警告:缓冲区是空的!! "<< endl;
			 else       cout << " Warning: empty buffer " << endl;
	   }
	   else 
          save_file();
	   break;
   case 'b':
       if(empty())
	   {
		     if(mode==0)cout<<"警告:缓冲区是空的!! "<< endl;
			 else       cout << " Warning: empty buffer " << endl;
	   }
      else
         while (previous_line() == success)
            ;
      break;

   case 'c':
      if (empty())
	  {
		  if(mode==0)cout << " 警告:空文件!!" << endl;
		  else       cout << " Warning: Empty file" << endl;
	  }
      else if (change_line() != success)
	  {
         if(mode==0)cout << " 错误:更改失败!! " << endl;
		 else       cout << " Error: Substitution failed " << endl;
	  }
      break;

   case 'd':
      if (remove(current_position, temp_string) != success)
	  {
          if(mode==0) cout << "错误:删除失败!!  " << endl;
		  else        cout << " Error: Deletion failed " << endl;
	  }
      break;

   case 'e':
        if(empty())
	   {
		     if(mode==0)cout<<"警告:缓冲区是空的!! "<< endl;
			 else       cout << " Warning: empty buffer " << endl;
	   }
         while (next_line() == success)
            ;
      break;

   case 'f':
     if (empty())
	  {
		  if(mode==0)cout << " 警告:空文件!!" << endl;
		  else       cout << " Warning: Empty file" << endl;
	  }
      else
         find_string();
      break;

   case 'g':
      if (goto_line() !=success)
	  {
		  if(mode==0)cout << " 警告:不存在此行!!" << endl;
		  else       cout << " Warning: No such line" << endl;
	  }
      break;

   case '?':
   case 'h':
           view();
	       break;
   case 'i':
      if (insert_line() != success)
	  {
		  if(mode==0)cout << " 错误:插入失败!! " << endl;
	     else        cout << " Error: Insertion failed " << endl;
	  }
      break;
   case 'j':
	   if(copy_line()!=success)
	   {
		   if(mode==0)cout<<"错误:复制失败!!"<<endl;
		   else       cout<<"Error:Copy failed "<<endl;
	   }
	   break;
	   
   case 'l':
	   if(mode==0)cout << "此文档共有 " << size() <<"段" << endl;
	   else       cout << "There are " << size() << " lines in the file." << endl;
      if (!empty())
	  {
         if(mode==0)cout << "当前段长度为: "
              << strlen(current->entry.c_str()) << endl;
		 else 
			  cout << "Current line length is "
              << strlen(current->entry.c_str()) << endl;
	  }
	  set_position(0);
	  num1=num2=0;
		do
		{
           num1=num1+strlen(current->entry.c_str());
		   num2=num2+strlen_zimu(current->entry.c_str());
		}while(next_line()==success);
		if(mode==0) 
		{
			        cout<<"总长度:    "<<num1<<endl;
		            cout<<"字符个数:  "<<num2<<endl;
		            cout<<"非字符个数:"<<num1-num2<<endl;
		}
		else
		{
			cout<<"The total length is:"<<num1<<endl;
			cout<<"The number of char:"<<num2<<endl;
			cout<<"The number of analphabetic:"<<num1-num2<<endl;
		}
   
      break;
   case 'k':
	   set_position(0);
	   do
	   {
		   (current->entry).changetosmall();
	   }while(next_line()==success);
       traverse(write);
	   break;
    case 'm':
	   set_position(0);
	   do
	   {
		   (current->entry).changetobig();
	   }while(next_line()==success);
       traverse(write);
	   break;
   case 'n':
      if (next_line() !=success)
	  {
		  if(mode==0)cout << " 警告:已到文档尾!!" << endl;
		  else   cout << " Warning: at end of buffer" << endl;
	  }
      break;
   case 't':
	   if(mode==0)
	   {
		   cout<<"Chinese-->English"<<endl;
		   mode=1;
	   }
	   else
	   {
		   cout<<"英文-->中文"<<endl;
		   mode=0;
	   }
	   break;
   case 'p':
      if (previous_line() !=success)
	  {
		  if(mode==0)cout << " 警告:已到文档头!!" << endl;
		  else       cout << " Warning: at start of buffer" << endl;
	  }
      break;

   case 'r':
      read_file();
      break;

   case 's':
      if (substitute_line() !=success)
	  {
		  if(mode==0)cout << " 错误:更改失败!! " << endl;
		  else cout << " Error: Substitution failed " << endl;
	  }
      break;

   case 'v':
      traverse(write);
      break;

   case 'w':
     if (empty())
	  {
		  if(mode==0)cout << " 警告:空文件!!" << endl;
		  else       cout << " Warning: Empty file" << endl;
	  }
      else
         write_file();
      break;

   default :
      if(mode==0)cout << "按h查看帮助或输入一个有效命令! "<<endl;
	  else cout << "Press h or ? for help or enter a valid command: ";
   }
}

void Editor::read_file()
{
    char filename1[max_filename_length];
	if(mode==0)cout<<"输入想要读取的文档名:"<<endl;
	else       cout<<"Input the read file's name:"<<endl;
	cin>>filename1;
	int i;
	for(i=0;i<max_filename_length;i++)
		current_file[i]=filename1[i];
  ifstream infile(filename1);
  if(infile==0){
		if(mode==0)cout<<"无法打开文档:"<<filename1<<endl;
		else       cout<<"Can't open input file"<<filename1<<endl;
		exit(1);
	}
	bool proceed=true;
	if(!empty()){
		if(mode==0){
			        cout<<"缓冲区不为空,读操作会破坏缓冲区的内容"<<endl;
	            	cout<<"是否进行操作?"<<endl;
		}
		else {
				cout<<"Buffer is not empty;the read will destroy it."<<endl;
		        cout<<"OK to proceed?"<<endl;
		}
		if(proceed=user_says_yes()) clear();
	}
	int line_number=0,terminal_char;
	while(proceed){
		String in_string=read_in(infile,terminal_char);
		if(terminal_char==EOF){
			proceed=false;
			if(strlen(in_string.c_str())>0)insert(line_number,in_string);
		}
		else insert(line_number++,in_string);
	}
	set_position(0);
}

void Editor::write_file()
{ 
	char filename2[max_filename_length];
	if(mode==0)cout<<"输入想要写入的文档名:"<<endl;
	else cout<<"Input the write file's name:"<<endl;
	cin>>filename2;
  ofstream outfile(filename2);
  if(outfile==0){
		if(mode==0)cout<<"无法打开文档:"<<filename2<<endl;
		else cout<<"Can't open output file"<<filename2<<endl;
		exit(1);
	}
   set_position(0);
   do
   {
   outfile<<(current->entry).c_str()<<endl;
   }while(next_line()==success);
}
void Editor::save_file()
{ 
  ofstream outfile(current_file);
   set_position(0);
   do
   {
   outfile<<(current->entry).c_str()<<endl;
   }while(next_line()==success);
}
Error_code Editor::next_line()
{
	if(current->next==NULL)return fail;
	else 
	{
		current=current->next;
		current_position++;
	}
	
	return success;
}

Error_code Editor::goto_line()
{
	int line_number;
	if(mode==0)cout<<"想要跳至哪一行?"<<flush;
	  else     cout<<"Which line do you want to go to?"<<flush;   
	cin>>line_number;
	while(cin.get()!='\n');
	if(line_number<0||line_number>count)return rang_error;
    set_position(line_number-1);
	current_position=line_number-1;
	return success;
}


Error_code Editor::insert_line()
{
	int line_number;
	if(mode==0)cout<<"输入要插入的位置:"<<flush;
	else  	cout<<"Insert what line number?"<<flush;
	cin>>line_number;
	while(cin.get()!='\n');
	if(mode==0)cout<<"输入插入内容:"<<flush;
	else    cout<<"What is the new line to insert?"<<flush;
	String to_insert=read_in(cin);
	return insert(line_number-1,to_insert);
}

Error_code Editor::substitute_line()
{
	int line_number;
	if(mode==0)cout<<"输入要改变的行的行号:"<<flush;
	else cout<<"Which line do you want to substitute?"<<flush;
	cin>>line_number;
	while(cin.get()!='\n');
	if(line_number<=0||line_number>count)return rang_error;
	if(mode==0)cout<<"输入插入内容:"<<flush;
	else    cout<<"What is the new line to insert?"<<flush;
	String to_replace=read_in(cin);
	return replace(line_number-1,to_replace);
}
Error_code Editor::copy_line()
{
  int line_number;
  if(mode==0)cout<<"输入要复制的行号:"<<flush;
  else       cout<<"Which line do you want to copy?"<<flush; 
  cin>>line_number;
  if(line_number<=0||line_number>count)return rang_error;
  set_position(line_number-1);
  String copy=current->entry;
  if(mode==0)cout<<"复制的内容要插入的位置:"<<endl;
  else      cout<<"What is the new line to insert?"<<flush;
	cin>>line_number;
	return insert(line_number-1,copy);
}
  
Error_code Editor::previous_line()
{
	if(current_position==0)return fail;
	current=current->back;
	current_position--;
	return success;
}

void Editor::find_string()
{
	int index;
	cout<<"Enter string to search for:"<<endl;
	String search_string=read_in(cin);
	set_position(0);
	int number=0;
do{
	index=strstr(current->entry,search_string);

	if(index!=-1){
		cout<<current_position+1<<":";
		cout<<(current->entry).c_str()<<endl;
		if(current_position<9)
			cout<<"  ";
		else
			cout<<"   ";
		for(int i=0;i<index;i++)
			cout<<" ";
		for(int j=0;j<(strlen(search_string.c_str()));j++)
			cout<<"^";
	    cout<<endl;
		number++;
	}
	}while(next_line()==success);
     if(number==0)
	 {
		 if(mode==0)cout<<"找不到指定字符串!!"<<endl;
		 else      cout<<"String was not found."<<endl;
	 }
}

Error_code Editor::change_line()
{
	Error_code result=success;
	if(mode==0)cout<<"输入要改变的行的行号:"<<flush;
	else       cout<<"What text segment do you want to replace?"<<flush;
	String old_text=read_in(cin);
   if(mode==0)	cout<<"输入新内容:"<<flush;
   else 	    cout<<"What new text segment do you want to add in?"<<flush;
   String new_text=read_in(cin);
	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();
		strcat(new_line,(String)(old_line+index+strlen(old_text.c_str())));
		current->entry=new_line;
	}
	return result;
}




int main()
{
	cout<<endl
		<<endl
		<<endl
		<<"        ************************************************************"<<endl
		<<"        *                欢 迎 使 用 文 档 编 辑 器                *"<<endl
		<<"        ************************************************************"<<endl
		<<endl
		<<endl
		<<endl
        <<"                                      "<<endl
		<<endl
		<<endl
		<<endl
		<<"  ****************因缓冲区为空,请先进行读文档或插入操作**********************"
		<<endl
		<<endl
		<<endl
        <<endl
		<<endl;
  cout<<"按任意键继续:"<<endl;
   getch();
      	view();
    	Editor buffer;
	    while(buffer.get_command())
		buffer.run_command();
		cout<<endl<<endl;
		cout<<"    END END END END END END END END END END END END END END END "<<endl;
        cout<<endl<<endl;
		cout<<"    *********************THANK YOU*****************************"<<endl;
	return 0;
}

⌨️ 快捷键说明

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