论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

DOS下的屏幕编辑器

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:43:06


#include "stdio.h"
#include "windows.h"
#include "stdio.h"
#include "crtdbg.h"
#include "string.h"

#define DIR_UP -1
#define DIR_DOWN 1

#define FORE_BLACK 0
#define FORE_BLUE FOREGROUND_BLUE
#define FORE_GREEN FOREGROUND_GREEN
#define FORE_RED FOREGROUND_RED
#define FORE_WHITE (FORE_BLUE|FORE_GREEN|FORE_RED)
#define FORE_MASK (FORE_BLUE|FORE_GREEN|FORE_RED|FOREGROUND_INTENSITY)

#define BACK_BLACK 0
#define BACK_BLUE BACKGROUND_BLUE
#define BACK_GREEN BACKGROUND_GREEN
#define BACK_RED BACKGROUND_RED
#define BACK_WHITE (BACK_BLUE|BACK_GREEN|BACK_RED)
#define BACK_MASK (BACK_BLUE|BACK_GREEN|BACK_RED|BACKGROUND_INTENSITY)

static HANDLE stdinput=INVALID_HANDLE_VALUE , stdoutput=INVALID_HANDLE_VALUE;

void InitIoSystem();
void GetChar(char * pscancode , char * pasciicode);
void DisplayChar(char ch);
void SetCursorXY(int x , int y);

void GetCursorXY(int * px ,int * py);
unsigned short GetTextForeColor(void);
unsigned short GetTextBackColor(void);

void SetTextForeColor(unsigned short forecolor);
void SetTextBackColor(unsigned short backcolor);

void ScrollScreen(int dir , unsigned int linecount , int left , int top , int right , int bottom);

void DisplayCursor(int flag);
void DisplayString(char *str);

void InitIoSystem()
{
 COORD cursorinitpos={0 , 0};
 COORD buffersize={80 , 25};
 stdinput=GetStdHandle(STD_INPUT_HANDLE);
 _ASSERTE(stdinput!=INVALID_HANDLE_VALUE);
 stdoutput=GetStdHandle(STD_OUTPUT_HANDLE);
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(SetConsoleCursorPosition(stdoutput , cursorinitpos)!=0);
 SetTextForeColor(FORE_WHITE);
 SetTextBackColor(BACK_BLACK);
 _ASSERTE(SetConsoleScreenBufferSize(stdoutput , buffersize)!=0);
}

void GetChar(char * pscancode , char * pasciicode)
{
 INPUT_RECORD inputrecord;
 unsigned long inputcount;
 _ASSERTE(pscancode!=NULL);
 _ASSERTE(pasciicode!=NULL);
 while (1)
 {
  _ASSERTE(stdinput!=INVALID_HANDLE_VALUE);
  _ASSERTE(ReadConsoleInput(stdinput , &inputrecord , 1 , &inputcount)!=0);
  if (inputrecord.EventType==KEY_EVENT && inputrecord.Event.KeyEvent.bKeyDown)
  {
    *pscancode = (char)inputrecord.Event.KeyEvent.wVirtualScanCode,
    *pasciicode = inputrecord.Event.KeyEvent.uChar.AsciiChar;
    if (inputrecord.Event.KeyEvent.dwControlKeyState&(RIGHT_ALT_PRESSED|RIGHT_CTRL_PRESSED|LEFT_ALT_PRESSED|LEFT_CTRL_PRESSED))
     continue;
    else if (*pasciicode==0&&(inputrecord.Event.KeyEvent.dwControlKeyState&SHIFT_PRESSED))
     continue;
    else
     break;
  }
 }
}

void DisplayChar(char ch)
{
 unsigned long outputcount;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(WriteConsole(stdoutput , &ch , 1 , &outputcount , NULL)!=0);
}

void SetCursorXY(int x ,int y)
{
 COORD cursorpos={x , y};
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(x>=0&&x<80&&y>=0&&y<25);
 _ASSERTE(SetConsoleCursorPosition(stdoutput , cursorpos)!=0);
}

void GetCursorXY(int * px , int * py)
{
 CONSOLE_SCREEN_BUFFER_INFO screenbufferinfo;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(px!=NULL&&py!=NULL);
 _ASSERTE(GetConsoleScreenBufferInfo(stdoutput , &screenbufferinfo)!=0);
 *px=screenbufferinfo.dwCursorPosition.X;
 *py=screenbufferinfo.dwCursorPosition.Y;
}

unsigned short GetTextForeColor(void)
{
 CONSOLE_SCREEN_BUFFER_INFO screenbufferinfo;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(GetConsoleScreenBufferInfo(stdoutput , &screenbufferinfo)!=0);
 return screenbufferinfo.wAttributes & FORE_MASK;
}

unsigned short GetTextBackColor(void)
{
 CONSOLE_SCREEN_BUFFER_INFO screenbufferinfo;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(GetConsoleScreenBufferInfo(stdoutput , &screenbufferinfo)!=0);
 return screenbufferinfo.wAttributes & BACK_MASK;
}

void SetTextForeColor(unsigned short forecolor)
{
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(SetConsoleTextAttribute(stdoutput , (WORD)(forecolor|GetTextBackColor()))!=0);
}

void SetTextBackColor(unsigned short backcolor)
{
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(SetConsoleTextAttribute(stdoutput , (WORD)(GetTextForeColor()|backcolor))!=0);
}

void ScrollScreen(int dir , unsigned int linecount , int left ,int top ,int right , int bottom)
{
 SMALL_RECT rect={left , top , right , bottom};
 COORD destcoord={left , top+dir*linecount};
 CHAR_INFO fillinfo={' ' ,};
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(left>=0&&top>=0&&right>=0&&bottom>=0&&left<80&&right<80&&top<25&&bottom<25);
 fillinfo.Attributes=GetTextForeColor() | GetTextBackColor();
 _ASSERTE(ScrollConsoleScreenBuffer(stdoutput , &rect , NULL , destcoord , &fillinfo)!=0);
}

void DisplayCursor(int flag)
{
 CONSOLE_CURSOR_INFO curinfo;
 _ASSERT(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERT(GetConsoleCursorInfo(stdoutput , &curinfo)!=0);
 curinfo.bVisible=(flag?TRUE:FALSE);
 _ASSERT(SetConsoleCursorInfo(stdoutput , &curinfo)!=0);
}

void DisplayString(char *str)
{
 unsigned long outputcount;
 _ASSERT(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERT(str!=NULL);
 _ASSERT(WriteConsole(stdoutput , str , strlen(str) , &outputcount , NULL)!=0);
}


#define MaxSize 79
#define MaxLine 24
#define Insert 1
#define Overwrite (-1)

FILE *fp;
char filename[20]="newfile";
char filename1[20];

typedef struct SNode
{
 struct SNode *next,*pri or;
 char String[MaxSize+1];
}SNode;


void openfile();
void newfile();
void savefile();
void quit();
int nextrow();
int backspace();
int uprow();
int downrow();
int downpage();
int uppage();   
int deletechar();
int insertchar(char b);


SNode * HeadNode,*TailNode,*rowp;
int State=Insert;
int sign=0,flag=0,flag1=0,sign1=0,sign2=0,sign3=0;
int x0=0;

void main(void)
{
 int x=0,y=0,i;
 char a,b;
 InitIoSystem();
 if(!(HeadNode=TailNode=rowp=(SNode*)malloc(sizeof(SNode))))
  exit(0);
 HeadNode->String [0]=0;
 HeadNode->next=HeadNode->prior=NULL;
 
 SetCursorXY(0,0);
 
 SetTextBackColor(BACK_BLUE);
 for(i=0;i<MaxSize;i++)
  printf(" ");
 SetCursorXY(0,0);
 printf("%s",filename);
 SetCursorXY(0,MaxLine);
 for(i=0;i<MaxSize;i++)
  printf(" ");
 SetCursorXY(0,MaxLine);
 printf("insert       F1-OPEN F2-NEW F3-SAVE F4-CLOSE F5-QUIT");
 
 SetCursorXY(0,1);
 SetTextBackColor(BACK_BLACK);
 while(1)
 {
  
  sign=0;
  flag=0;
  sign1=0;
  sign2=0;
  sign3=0;
  GetChar(&a,&b);
  GetCursorXY(&x,&y);
  if(a==1)//ESC
   ;
  
  else if(a==28)//ENTER
   nextrow();
  else if(a==14)
   backspace();
  else if(a==59)//F1
   openfile();
  else if(a==60)//F2
   newfile();
  else if(a==61)//F3
   savefile();
  else if(a==62)//F4
   newfile();
  else if(a==63)//F5
   
  {
   quit();
   if(flag1==1)
    return;
  }
  else if(a==71)//HOME
   SetCursorXY(0,y);
  else if(a==79)//END
   if(rowp->String[0]<MaxSize)
    SetCursorXY(rowp->String[0],y );
   else
    SetCursorXY(MaxSize-1,y );
   else if(a==72)//UP
    uprow();
   else if(a==80)//DOWN
    downrow();
   else if(a==75)//LEFT
   {
    if(x==0)
    {
     int i;
     i=uprow();//if i=0,top in the screen and can scroll
     if(i!=-1)//if i=-1,can't scroll
      if(rowp->String[0]<MaxSize)
       SetCursorXY(rowp->String[0],y-i);
      else
       SetCursorXY(MaxSize-1,y-i);
    }
    else
     SetCursorXY(x-1,y);
   }   file://left
   
   else if(a==77)//RIGHT
   {      
    if(x==rowp->String[0]-1&&!rowp->next)
     SetCursorXY(x+1,y);
    else if(x>=rowp->String[0]-1)
    {
     int i;
     i=downrow();//if i=1 common;if  i=0 bottom on the screen
     if(i!=-1)//if i=-1,can't scroll
      SetCursorXY(0,y+i);
    }    
    else
     SetCursorXY(x+1,y);
   }
   else if(a==81)//PAGE DOWN
   {
    DisplayCursor(0);
    downpage(); 
    DisplayCursor(1);
   }
   else if(a==73)//PAGE UP
   { 
    DisplayCursor(0);
    uppage();
    DisplayCursor(1);   
   }
   else if(a==82)//INSERT
    
   {
    State*=-1;
    if(State!=Insert)
    {
     SetCursorXY(0,MaxLine);
     SetTextBackColor(BACK_BLUE);
     printf("overwrite");
     SetCursorXY(x,y);
     SetTextBackColor(BACK_BLACK);
    }
    else
    {
     SetCursorXY(0,MaxLine);
     SetTextBackColor(BACK_BLUE);
     printf("insert   ");
     SetCursorXY(x,y);
     SetTextBackColor(BACK_BLACK);
    }
   }
   else if(a==83)//DELETE
    deletechar();
   
   
   
   else if(b)
    
    insertchar(b); 
   
   
 }//while(1)
}//main()

int insertchar(char ch)
{
 int i;  
 int x=0,y=0; 
 char ch1;                                              
 GetCursorXY(&x,&y);
 if(sign3)
  x=x0;
 
 if(!rowp)
 {
  if(!(TailNode->next=rowp=(SNode *)malloc(sizeof(SNode))))
   exit(0);
  TailNode->next->prior=TailNode; 
  TailNode->next->next=NULL;
  TailNode->next->String[0]=0;
  TailNode=TailNode->next;
  
 }
 if(State==Overwrite&&ch!='\t')
 {
  
  
  
  rowp->String[x+1]=ch;
  DisplayChar(ch);
  if(x==rowp->String[0])
   rowp->String[0]++;
  if(x==MaxSize-1)
  {
   if(y<MaxLine-1)
    SetCursorXY(0,y+1);
   else
   {
    ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
    SetCursorXY(0,MaxLine-1);
   }
   rowp=rowp->next;
   
  }
  return 0 ;
 }
 
 if(ch=='\t')//处理“TAB”键,在该行还剩>=8个空间时插入8个空格
 {
  if(State==Overwrite)
   return 0;
  if(x<MaxSize-8)
   for(i=0;i<8;i++)
    insertchar(' ');
   else
    for(i=x;i<MaxSize-1;i++)
     insertchar(' ');
    return 0 ;
 }
 if(rowp->String[0]<MaxSize)
 {
  for(i=rowp->String[0];i>x;i--)//元素后移
   rowp->String[i+1]=rowp->String[i];
  rowp->String[x+1]=ch;
  rowp->String[0]++;
  if(sign3)
   return 1;
  
  DisplayChar(ch);
  for(i=x+2;i<=rowp->String[0];i++)
   DisplayChar(rowp->String[i]);
  
  if(x<MaxSize-1)
   SetCursorXY(x+1,y);
  else if(y<MaxLine-1)
  {
   SetCursorXY(0,y+1);
   rowp=rowp->next;
  } 
  else
  { 
   ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
   SetCursorXY(0,MaxLine-1);
   rowp=rowp->next;
  }
  return 1;
 }
 else if(rowp->String[0]==MaxSize)
 {char ch2;
 if(y!=MaxLine-1)//不在屏幕最后一行插入
 { 
  ch1=rowp->String[rowp->String[0>;
  SetCursorXY(0,y+1);
  rowp=rowp->next;
  insertchar(ch1);
  rowp=rowp->prior;
  rowp->String[0]--;
  if(flag&&sign1)
   y--;
  sign1=1;
  if(y==0)
   sign3=1;
  if(!sign3)
   SetCursorXY(x,y);
  else x0=x;
  
  insertchar(ch);
  if(sign3)
   rowp=rowp->next;
 }
 else file://在最后一行插入
 {
  if(flag)
  {
   
   SNode *p1=rowp;
   sign2=1;
   DisplayChar(ch);
   
   if(rowp->String[0]<MaxSize)
   {
    for(i=1;i<=rowp->String[0];i++)
     
     DisplayChar(rowp->String[i]);
    
    for(i=rowp->String[0];i>0;i--)
     rowp->String[i+1]=rowp->String[i];
    
    rowp->String[1]=ch;
    rowp->String[0]++;
   }
   else
   {
    for(i=1;i<MaxSize;i++)
     DisplayChar(rowp->String[i]);
    ch2=rowp->String[MaxSize];  
    for(i=MaxSize-1;i>0;i--)
     rowp->String[i+1]=rowp->String[i];
    rowp->String[1]=ch;
    
    rowp=rowp->next;
pp:;
  
   if(!rowp)
   {
    if(!(TailNode->next=rowp=(SNode *)malloc(sizeof(SNode))))
     exit(0);
    TailNode->next->prior=TailNode; 
    TailNode->next->next=NULL;
    TailNode->next->String[0]=0;
    TailNode=TailNode->next;
    rowp->String[0]=1;
    rowp->String[1]=ch2;
    rowp=p1;
    return 1;
   }
  
   if(rowp->String[0]<MaxSize)
   
   { for(i=rowp->String[0];i>0;i--)
   rowp->String[i+1]=rowp->String[i];
  
   
   rowp->String[1]=ch2;
   rowp->String[0]++;
   rowp=p1;
   return 1;
   } 
   else
   {
    char ch3=rowp->String[MaxSize];
    for(i=MaxSize-1;i>0;i--)
     rowp->String[i+1]=rowp->String[i];
    rowp->String[1]=ch2;
    ch2=ch3;
    rowp=rowp->next;
    goto pp;
   }
  
   }
  }
  ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
  flag=1;
  if(rowp->next)
  { 
   SetCursorXY(0,MaxLine-1);
   for(i=0;i<rowp->next->String[0];i++)
    DisplayChar(rowp->next->String[i+1]);
  }
  SetCursorXY(x,y-1);
  insertchar(ch);
  
  
 }
 
 
 return 1;
}
return 1;
}


int backspace(void)
{
 int x=0,y=0;
 int i;
 GetCursorXY (&x,&y);
 if(x!=0)
 {
  SetCursorXY(x-1,y);
  DisplayChar(' ');
  SetCursorXY(x-1,y);
  for(i=x;i<rowp->String[0];i++)
  {
   rowp->String[i]=rowp->String[i+1];
   DisplayChar(rowp->String[i+1]);
  }
  DisplayChar(' ');
  SetCursorXY(x-1,y);
  rowp->String[0]--;
 }//if
 
 else//x=0
 { if(!rowp&&TailNode)
 {
  if(TailNode->String[0]<MaxSize)
   
   SetCursorXY(TailNode->String[0],y-1);
  else
  {
   TailNode->String[0]--;
   SetCursorXY(TailNode->String[0],y-1);
   DisplayChar(' ');
   SetCursorXY(TailNode->String[0],y-1);
  }
  rowp=TailNode;
  return 1;
 }
 else if(!rowp->prior)
  return 1; 
 
 
 else if(rowp->String[0]>=0&&y>1)//长度不为零,前面有行,不要滚屏
 {
  int y0=y;
  SNode * pp=rowp,*rowp0=rowp->prior,*rowp1;
  if(rowp->String[0]+rowp->prior->String[0]<=MaxSize)
  {
   int length=rowp->prior->String[0];
   SetCursorXY(rowp->prior->String[0],y-1);
   for(i=rowp->prior->String[0]+1;i<=rowp->String[0]+rowp->prior->String[0];i++)
   {
    rowp->prior->String[i]=rowp->String[i-rowp->prior->String[0>;
    DisplayChar(rowp->String[i-rowp->prior->String[0>);
   }
   SetCursorXY(0,y);   
   rowp->prior->String[0]+=rowp->String[0];   
   for(i=0;i<rowp->String[0];i++)
    DisplayChar(' ');
   rowp->String[0]=0;
   SetCursorXY(0,y);
   
   if(!rowp->next)
   {     
    rowp->prior->next=NULL; 
    TailNode=rowp->prior;
    rowp=rowp->prior;
    if(length<MaxSize)
    {
     SetCursorXY(length,y-1);
    }
    else
    {
     rowp->String[0]--;
     SetCursorXY(length-1,y-1);
     DisplayChar(' ');
     SetCursorXY(length-1,y-1);
    }
    
   }
   else
   {
    rowp->prior->next=rowp->next;
    rowp->next->prior=rowp->prior;
    rowp=rowp->next;
    rowp1=rowp;
    ScrollScreen(DIR_UP,1,0,y+1,MaxSize-1,MaxLine-1);
    for(i=y;i<MaxLine-1;i++)
     if(rowp)
      rowp=rowp->next;
     if(!rowp)
      rowp=rowp1;
     else
     {
      SetCursorXY(0,MaxLine-1);
      for(i=1;i<=rowp->String[0];i++)
       DisplayChar(rowp->String[i]);
     }
     rowp=rowp1;
     
     if(length<MaxSize)
     {
      SetCursorXY(length,y-1);
      rowp=rowp->prior;
     }
     else
      SetCursorXY(0,y);
     
     
   }
  }//if
  else//移上去部分
  {   if(rowp->prior->String[0]<MaxSize)
  {
   int length=rowp->prior->String[0];
   SetCursorXY(rowp->prior->String[0],y-1);
   for(i=rowp->prior->String[0]+1;i<=MaxSize;i++)
   {
    rowp->prior->String[i]=rowp->String[i-rowp->prior->String[0>;
    DisplayChar(rowp->String[i-rowp->prior->String[0>);
   }//for
   SetCursorXY(0,y);
   for(i=1;i<=rowp->String[0]+rowp->prior->String[0]-MaxSize;i++)
    DisplayChar(rowp->String[i]=rowp->String[i+MaxSize-rowp->prior->String[0>);
   for(;i<=rowp->String[0];i++)
    DisplayChar(' ');   
   rowp->String[0]-=MaxSize-rowp->prior->String[0];
   rowp->prior->String[0]=MaxSize;
   SetCursorXY(length,y-1);
   rowp=rowp->prior;
  }
  else
  {
   rowp=rowp->prior;
   rowp->String[MaxSize]=rowp->next->String[1];
   
   
   
   SetCursorXY(0,y);
   for(i=1;i<rowp->next->String[0];i++)
   {
    rowp->next->String[i]=rowp->next->String[i+1];
    DisplayChar(rowp->next->String[i]);
    
   }
   DisplayChar(' ');
   SetCursorXY(MaxSize-1,y-1);
   DisplayChar(rowp->String[MaxSize]);
   SetCursorXY(MaxSize-1,y-1);
   rowp->next->String[0]--;
   return 1;
  }
  }//else
  
  }//else if
  else if(y==1)
  {
   ScrollScreen(DIR_DOWN,1,0,1,MaxSize-1,MaxLine-2);
   SetCursorXY(0,1);
   for(i=0;i<rowp->prior->String[0];i++)
    DisplayChar(rowp->prior->String[i+1]);
   SetCursorXY(0,2);
   backspace();
  }//else if
  
  
 }//else
 return 1;
}//backspace

int downrow(void)
{
 int x=0,y=0;
 int i;
 GetCursorXY(&x,&y);
 if(!rowp->next)
  return -1;
 rowp=rowp->next;
 if(y!=MaxLine-1&&x>rowp->String[0])
  SetCursorXY(rowp->String[0],y+1);
 else if(y!=MaxLine-1)
  SetCursorXY(x,y+1);
 else
 {
  ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
  SetCursorXY(0,MaxLine-1);
  for(i=1;i<=rowp->String[0];i++)
   DisplayChar(rowp->String[i]);
  if(x>rowp->String[0])
   SetCursorXY(rowp->String[0],MaxLine-1);
  else
   SetCursorXY(x,MaxLine-1);
  return 0;
 }
 return 1;
 
}//downrow


int uprow(void)
{
 int x=0,y=0;
 int i;
 GetCursorXY(&x,&y);
 if(!rowp)
 {
  i=0;
  if(!(TailNode->next=rowp=(SNode *)malloc(sizeof(SNode))))
   exit(0);
  TailNode->next->prior=TailNode; 
  TailNode->next->next=NULL;
  TailNode->next->String[0]=0;
  TailNode=TailNode->next;
  
 }
 if(!rowp->prior)
  return -1;
 rowp=rowp->prior;
 if(y!=1&&x>rowp->String[0])
  SetCursorXY(rowp->String[0],y-1);
 else if(y!=1)
  SetCursorXY(x,y-1);
 else
 {
  ScrollScreen(DIR_DOWN,1,0,1,MaxSize-1,MaxLine-2);
  SetCursorXY(0,1);
  for(i=0;i<rowp->String[0];i++)
   DisplayChar(rowp->String[i+1]);
  if(x>rowp->String[0])
   SetCursorXY(rowp->String[0],1);
  else
   SetCursorXY(x,1);
  return 0;
 }
 return 1; 
}//uprow


int downpage(void)
{
 int x=0,y=0,i,j,k;
 SNode *pp=rowp;
 GetCursorXY(&x,&y);
 for(i=y;i<=MaxLine-1;i++,rowp=rowp->next)
  if(!rowp->next)
  {
   rowp=pp;
   return 0;
  }
  pp=0;
  SetCursorXY(0,1);
  for(i=1;i<MaxLine&&rowp;rowp=rowp->next,i++)
  {
   for(j=0;j<rowp->String[0];j++)
    DisplayChar(rowp->String[j+1]);
   for(;j<MaxSize;j++)
    DisplayChar(' ');
   SetCursorXY(0,i+1);
   if(i==y)
    pp=rowp;
  }
  if(pp)
   rowp=pp;
  else
   rowp=TailNode;
  for(j=i;j<MaxLine;j++)
  {
   for(k=0;k<MaxSize;k++)
    DisplayChar(' ');
   SetCursorXY(0,j+1);
  }
  if(y<i)
   if(rowp->String[0]>x)
    SetCursorXY(x,y);
   else
    SetCursorXY(rowp->String[0],y);
   else
    SetCursorXY(rowp->String[0],i-1);
   return 1;
   
}//downpage
int uppage()
{
 int x=0,y=0,i;
 SNode *pp=rowp;
 GetCursorXY(&x,&y);
 for(i=1;i<=y;i++)
 {
  if(!rowp->prior)
  {
   rowp=pp;
   return 1;
  }
  rowp=rowp->prior;
 }
 for(i=1;i<MaxLine-1&&rowp->prior;i++)
  rowp=rowp->prior;
 
 
 pp=rowp;
 SetCursorXY(0,1);
 for(i=1;i<MaxLine;i++)
 { int j;
 for(j=1;j<=rowp->String[0];j++)
  DisplayChar(rowp->String[j]);
 while(j++<=MaxSize)
  DisplayChar(' ');
 rowp=rowp->next;
 SetCursorXY(0,i+1);
 if(i==y-1)
  pp=rowp;
 
 
 }
 rowp=pp;
 if(x<rowp->String[0])
  SetCursorXY(x,y);
 else
  SetCursorXY(rowp->String[0],y);
 return 1;
}


int deletechar()
{
 int x=0,y=0,i;
 GetCursorXY(&x,&y);
 if(!rowp)
  return 1;
 if(x==0&&rowp->String[0]==0&&rowp->next&&y<MaxLine-1)
 {
  SetCursorXY(0,y+1);
  rowp=rowp->next;
  b ackspace();
 }
 else if(x==rowp->String[0]&&!rowp->next)
  return 1;
 else if(x==rowp->String[0]&&y<MaxLine-1)
 {
  rowp=rowp->next;
  SetCursorXY(0,y+1);
  backspace();
 }
 else if(x==0&&rowp->String[0]==0&&!rowp->next)
  return 1;
 else if(x==0&&rowp->String[0]==0&&rowp->next&&y==MaxLine-1)
 {
  rowp=rowp->next;
  for(i=1;i<=rowp->String[0];i++)
   DisplayChar(rowp->String[i]);
  SetCursorXY(0,MaxLine-1);
 }
 else if(x<MaxSize)
 {
  SetCursorXY(x+1,y);
  backspace();
  return 1;
 }
 else if(y<MaxLine-1)
 {
  SetCursorXY(0,y+1);
  rowp=rowp->next;
  backspace();
  return 1;
 }
 else 
 {
  ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
  SetCursorXY(0,MaxLine-1);
  rowp=rowp->next;
  backspace();
  return 1;
 }
 return 1;
}

int nextrow()
{
 int x=0,y=0,i;
 SNode *pp;
 GetCursorXY(&x,&y);
 if(!rowp)
 {
  if(!(pp=(SNode *)malloc(sizeof(SNode))))
   exit(0);
  pp->String[0]=0;
  pp->prior=TailNode;
  pp->next=0;
  TailNode->next=pp;
  rowp=pp;
 }
 if(x==rowp->String[0])
 {
  if(rowp->next)
  {
   if(!(pp=(SNode *)malloc(sizeof(SNode))))
    exit(0);
   pp->String[0]=0;
   pp->prior=rowp;
   pp->next=rowp->next;
   rowp->next->prior=pp;
   rowp->next=pp;
   if(y==MaxLine-2)
   {
    SetCursorXY(0,MaxLine-1);
    for(i=1;i<=MaxSize;i++)
     DisplayChar(' ');
    SetCursorXY(0,MaxLine-1);
    rowp=rowp->next;
    return 1;
   }
   else if(y<MaxLine-2)
    
    ScrollScreen(DIR_DOWN,1,0,y+1,MaxSize-1,MaxLine-2);
   else if(y=MaxLine-1)
    ScrollScreen(DIR_UP,1,0,2,MaxSize-1,y-1);
   if(y<MaxLine-2)
   {
    SetCursorXY(0,y+1);
    rowp=rowp->next;
    return 1;
   }
   else  
   {
    
    SetCursorXY(0,MaxLine-1);
    rowp=rowp->next;
    return 1;
   }
  }//if
  else if(!rowp->next)
  {
   if(!(pp=TailNode=(SNode *)malloc(sizeof(SNode))))
    exit(0);
   pp->String[0]=0;
   pp->prior=rowp;
   pp->next=rowp->next;
   rowp->next=pp;
   if(y<MaxLine-1)
   {
    SetCursorXY(0,y+1);
    rowp=pp;
    return 1;
   }
   else
   {
    ScrollScreen(DIR_UP,1,0,2,MaxSize-1,y);
    SetCursorXY(0,y);
    rowp=pp;
    return 1;
   }
  }//else if
 }//if
 else
 {
  for(i=x;i<rowp->String[0];i++)
   DisplayChar(' ');
  if(rowp->next)
  {
   if(!(pp=(SNode *)malloc(sizeof(SNode))))
    exit(0);
   
   pp->String[0]=rowp->String[0]-x;
   rowp->String[0]=(int)(x);
   for(i=1;i<=pp->String[0];i++)
    pp->String[i]=rowp->String[i+x];
   pp->prior=rowp;
   
   pp->next=rowp->next;
   rowp->next->prior=pp;
   rowp->next=pp;
   rowp=pp;
   if(y==MaxLine-2)
   {
    SetCursorXY(0,MaxLine-1);
    for(i=1;i<=pp->String[0];i++)
     DisplayChar(pp->String[i]); 
    while(i++<MaxSize-1)
     DisplayChar(' ');
    SetCursorXY(0,MaxLine-1);
   }
   else if(y<MaxLine-1)
    
   {
    ScrollScreen(DIR_DOWN,1,0,y+1,MaxSize-1,MaxLine-2);
    SetCursorXY(0,y+1);
    for(i=1;i<=pp->String[0];i++)
     DisplayChar(pp->String[i]);
    while(i++<MaxSize-1)
     DisplayChar(' ');
    SetCursorXY(0,y+1);
   }
   else if(y=MaxLine-1)
   {
    ScrollScreen(DIR_UP,1,0,2,MaxSize-1,y-1);
    SetCursorXY(0,MaxLine-1);
    for(i=1;i<=pp->String[0];i++)
     DisplayChar(pp->String[i]);
    while(i++<MaxSize-1)
     DisplayChar(' ');
    SetCursorXY(0,MaxLine-1);
   }
   
  }//if
  else if(!rowp->next)
  {
   if(!(pp=TailNode=(SNode *)malloc(sizeof(SNode))))
    exit(0);
   
   pp->String[0]=rowp->String[0]-x;
   rowp->String[0]=(int)(x);
   for(i=1;i<=pp->String[0];i++)
    pp->String[i]=rowp->String[i+x];
   pp->prior=rowp;
   pp->next=rowp->next;
   rowp->next=pp;
   if(y<MaxLine-1)
   {
    SetCursorXY(0,y+1);
    rowp=pp;
    for(i=1;i<=pp->String[0];i++)
     DisplayChar(pp->String[i]);
    while(i++<MaxSize-1)
     DisplayChar(' ');
    SetCursorXY(0,y+1);
    return 1;
   }
   else
   {
    ScrollScreen(DIR_UP,1,0,2,MaxSize-1,y);
    SetCursorXY(0,y);
    for(i=1;i<=pp->String[0];i++)
     DisplayChar(pp->String[i]);
    while(i++<MaxSize-1)
     DisplayChar(' ');
    SetCursorXY(0,y);
    rowp=pp;
    return 1;
   }
  }//else if
  
 }//else
 return 1;
}

void openfile(void)
{
 
 int i=1,x=0,y=0,count=0,j,t;
 char ch,c;
 
 strcpy(filename1,filename);
 
 GetCursorXY(&x,&y);
 SetCursorXY(0,0);
 SetTextBackColor(BACK_BLUE);
 printf("Save this file?Y for yes,N for no.C for cancel.  ");
 scanf("%c",&c);
 getchar();
 SetTextBackColor(BACK_BLACK);
 if(c=='C'||c=='c')
 {
  SetTextBackColor(BACK_BLUE);
  SetCursorXY(0,0);
  for(i=0;i<MaxSize;i++)
   printf(" ");
  SetCursorXY(0,0);
  printf("%s",filename);
  SetTextBackColor(BACK_BLACK);
  SetCursorXY(x,y);
  return;
 }
 
 
 
 if(c=='Y'||c=='y')
  savefile();
 
 SetCursorXY(0,0);
 SetTextBackColor(BACK_BLUE);
 for(i=0;i<MaxSize;i++)
  printf(" ");
 SetCursorXY(0,0);
 SetTextBackColor(BACK_BLUE);
 printf("Open what? ");
 scanf("%s",filename);
 getchar();
 if((fp=fopen(filename,"r"))==0)
 {
  SetCursorXY(MaxSize-50,0);
  printf("No such file.Press enter to go on.");
  getchar();
  SetCursorXY(0,0);
  for(i=0;i<MaxSize;i++)
   printf(" ");
  SetCursorXY(0,0);
  printf("%s",filename1);
  SetCursorXY(x,y);
  SetTextBackColor(BACK_BLACK);
  return;
  
 }
 SetTextBackColor(BACK_BLACK);
 if(!(HeadNode=rowp=(SNode*)malloc(sizeof(SNode))))
  exit(0);
 HeadNode->String[0]=0;
 HeadNode->next=HeadNode->prior=NULL;
 SetCursorXY(0,1);
 for(i=1;i<MaxLine;i++)
 {  
  for(j=1;j<=MaxSize;j++)
   DisplayChar(' ');
  SetCursorXY(0,i+1);
 }
 SetCursorXY(0,1);
 i=1;
 while(!feof(fp))
 {
  fscanf(fp,"%c",&ch);
  if(ch=='\n')
  {
   rowp->String[0]=i-1;
   i=1;
   rowp->next=(SNode*)malloc(sizeof(SNode));
   rowp->next->prior=rowp;
   rowp->next->next=NULL;
   rowp=rowp->next;
   count++;
  }
  else
  {
   rowp->String[i]=ch;
   i++;
  }
 }
 rowp=rowp->prior->prior;
 TailNode=rowp;
 rowp->next=NULL;
 SetCursorXY(0,1);
 t=count;
 if(count<=MaxLine-1)
 {
  
  for(rowp=HeadNode,count=1;rowp;rowp=rowp->next,count++)
  { 
   for(i=1;i<=rowp->String[0];i++)
    DisplayChar(rowp->String[i]);
   SetCursorXY(0,count+1);
   
  }
 }
 else
 {
  for(rowp=TailNode,count=1;count<MaxLine-1;rowp=rowp->prior,count++);
  for(count=1;count<MaxLine;rowp=rowp->next,count++)
  {
   for(i=1;i<=rowp->String[0];i++)
    DisplayChar(rowp->String[i]);
   SetCursorXY(0,count+1);
  }
  t=MaxLine;
 }
 SetCursorXY(0,0);
 SetTextBackColor(BACK_BLUE);
 for(i=0;i<MaxSize;i++)
  pr intf(" ");
 SetCursorXY(0,0);
 printf("%s",filename);
 if(TailNode->String[0]<=MaxSize)
  SetCursorXY(TailNode->String[0],t-1);
 else
  SetCursorXY(MaxSize,t-1);
 rowp=TailNode;
 SetTextBackColor(BACK_BLACK);
 fclose(fp);
}

void savefile(void)
{
 int i,x,y;
 char ch;
 SNode *pp;
 GetCursorXY(&x,&y);
 
 SetTextBackColor(BACK_BLUE);
pp:;
   SetCursorXY(0,0);
   for(i=0;i<MaxSize;i++)
    printf(" ");
   SetCursorXY(0,0);
   printf("Save as :");
   scanf("%s",filename);
   getchar();
  
   strcpy(filename1,filename);
   while((fp=fopen(filename,"r"))!=0)
   {
    SetCursorXY(17+strlen(filename),0);
    printf("Overwrite?Y for yes,N for no.");
    scanf("%c",&ch);
    getchar();
    fclose(fp);
    if(ch=='Y'||ch=='y')
     break;
    goto pp;
   }
   if((fp=fopen(filename,"w"))==0)
    return;
   for(pp=HeadNode;pp;pp=pp->next)
   {
    for(i=1;i<=pp->String[0];i++)
     fprintf(fp,"%c",pp->String[i]);
    fprintf(fp,"\n");
   }
   fclose(fp);
   SetCursorXY(0,0);
   SetTextBackColor(BACK_BLUE);
   for(i=0;i<MaxSize;i++)
    printf(" ");
   SetCursorXY(0,0);
   printf("%s",filename);
   SetTextBackColor(BACK_BLACK);
   SetCursorXY(x,y);
}

void newfile(void)
{
 int x=0,y=0,i,j;
 char c;
 SNode *pp;
 GetCursorXY(&x,&y);
 SetCursorXY(0,0);
 SetTextBackColor(BACK_BLUE);
 printf("Save this file?Y for yes,N for no.C for cancel.  ");
 scanf("%c",&c);
 getchar();
 SetTextBackColor(BACK_BLACK);
 if(c=='C'||c=='c')
 {
  SetTextBackColor(BACK_BLUE);
  SetCursorXY(0,0);
  for(i=0;i<MaxSize;i++)
   printf(" ");
  SetCursorXY(0,0);
  printf("%s",filename);
  SetTextBackColor(BACK_BLACK);
  SetCursorXY(x,y);
  return;
 }
 if(c=='Y'||c=='y')
  savefile();
 SetCursorXY(0,1);
 for(i=1;i<MaxLine;i++)
 {  
  for(j=1;j<=MaxSize;j++)
   printf(" ");
  SetCursorXY(0,i+1);
 }
 SetCursorXY(0,1);
 for(rowp=TailNode;rowp;free(pp))
 {
  pp=rowp;
  rowp=rowp->prior;
 }
 if(!(HeadNode=rowp=(SNode*)malloc(sizeof(SNode))))
  exit(0);
 HeadNode->String[0]=0;
 HeadNode->next=HeadNode->prior=NULL;
 SetCursorXY(0,0);
 SetTextBackColor(BACK_BLUE);
 for(j=1;j<=MaxSize;j++)
  DisplayChar(' ');
 SetCursorXY(0,0);
 printf("newfile");
 SetCursorXY(0,1);
 SetTextBackColor(BACK_BLACK);
}

void quit()  
{
 char ch;
 int x,y,i;
 SetTextBackColor(BACK_BLUE);
 GetCursorXY(&x,&y);
 SetCursorXY(0,0);
 printf("Do you want to save it?'Y' for yes ,'N' for no,'C' for cancel. ");
 scanf("%c",&ch);
 getchar();
 if(ch=='C'||ch=='c')
 {
  SetCursorXY(0,0);
  for(i=0;i<MaxSize;i++)
   printf(" ");
  SetCursorXY(0,0);
  printf("%s",filename);
  SetCursorXY(x,y);
  SetTextBackColor(BACK_BLACK);
  return;
 }
 SetTextBackColor(BACK_BLACK);
 if(ch=='Y'||ch=='y')
  savefile();
 flag1=1;
 
}

视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058