行編輯器的簡易C++實現

說到行編輯器的實現,其實還是主要運用棧的結構特點,具體理論參考《數據結構》(嚴蔚敏著)實現代碼如下:

#include <iostream>
using namespace std;
#define MAX_SIZE 100

void LineEditor(char* c1, int& length){
 char* stack = new char[MAX_SIZE];
 int top = 0;
 int i;
 for(i = 0; i < length; i++){
  if(c1[i] == '#'){//'#'退格符
   if(top > 0){
    top--;
   }
  }else if(c1[i] == '@'){//'@'刪除整行
   top = 0;
  }else if(c1[i] == '^'){//'^'回車換行
   stack[top] = '/n';
   top++;
   stack[top] = '/r';
   top++;
  }else {
   stack[top] = c1[i];
   top++;
  }
 }
 length = top;
 int k = length;
 while(top > 0){
  top--;
  k--;
  c1[k] = stack[top];
 }
 delete stack;
}

void main(){
 int length1 = 0;
 int i;
 char c1[MAX_SIZE];
 cout<<"chs:";
 for(i = 0; i < MAX_SIZE; i++){
  char c;
  cin>>c;
  if(c == '$')//'$'爲終結符
   break;
  c1[i] = c;
  length1++;
 }
 LineEditor(c1, length1);
 for(i = 0; i < length1; i++){
  cout<<c1[i];
 }
 cout<<endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章