hangbianji.txt

来自「具体实现以下4条基本编辑命令: (1) 行插入。格式:i《行号》《回车》《文本」· 文本 代码 · 共 214 行

TXT
214
字号
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

typedef struct {
   char word[20];
   int length;
}wordtype;
wordtype seekword;            /*seekword用来放要查找的词*/
wordtype save;                /*save用来临时成放从文件中读出的词*/

int check(char ch)            /*检查是否是合法的字符*/
{
  if(ch<='9'&&ch>='0'||ch<='z'&&ch>='a'||ch<='Z'&&ch>='A')
    return 1;
  else
    return 0;

}

void length()              /*计算seekword 和save中的word[]的长度*/
{
  int i;

  seekword.length=0;
  save.length=0;
  for(i=0;seekword.word[i]!='\0';i++)
        ;
  seekword.length=i;
  for(i=0;save.word[i]!='\0';i++)
        ;
  save.length=i; 
}

int compare()                  /*比较save和seekword中的word[]是否匹配,若匹配返回1,否则返回0 */
{
  int length1,length2,i,j,flag1,flag2,breakpoint;
  char segment1[20],segment2[20],ch;

  if (seekword.length<=save.length){      
      for(i=0;i<20;i++){
          if(seekword.word[i]=='?'||seekword.word[i]=='*'){              
               ch=seekword.word[i];
               breakpoint=i;
          }
      }
              
      if(ch=='?'||ch=='*'){
          
          for(i=0;i<breakpoint;i++)
               segment1[i]=seekword.word[i];
          length1=breakpoint;
          segment1[i]='\0';
          for(j=breakpoint+1,i=0;j<seekword.length;j++,i++)
               segment2[i]=seekword.word[j];
          segment2[i]='\0';
          length2=i;
          flag1=flag2=0;
          for(i=0;i<length1;i++)
             if(segment1[i]!=save.word[i]){
                   flag1=-1;
                   break;
             }
          if(flag1!=-1){
              if((breakpoint==seekword.length-1)&&(ch=='?')){
                   if(save.length==seekword.length)
                         return 1;
                   else
                         return 0;
              }
              for(i=0,j=save.length-length2;i<length2&&j<save.length;i++,j++)
                   if(segment2[i]!=save.word[j]){
                         flag2=-1;
                         break;
                   }
          }
          if(flag1==0&&flag2==0)
              return 1;
          else
              return 0;      
      }
      else{
          if(strcmp(save.word,seekword.word)==0)
              return 1;
          else
              return 0;  
      }   
    
  }         
      
  else 
       return 0;

}
void seek(char filename[10])      /*查找seekword在文件中的位置,并以(行,列)的形式输出*/
{
  FILE *fp;
  char ch;
  int i,j,k,num;
  typedef struct word_place{
       char word[20];
       int row;
       int line;       
       struct word_place *next;
  }seekplace;
  seekplace *header,*link;    
    
  fp=fopen(filename,"r");
  if(fp==NULL){
      printf("The file can not be opened!\nMaybe the file is not in current working directory\n");
      exit(0);
  }
  if((link=(seekplace *)malloc(sizeof(seekplace)))==NULL){ 
      printf("There is no free space in momory!");
      exit(0);     
  }
  header=link;
  num=0;
  
  if(feof(fp)){
     printf("The file is empty!");
     exit(0);   
  }
  j=0;k=1;
  
  while(!feof(fp)){
     i=0;
     while(check(ch=fgetc(fp))) { 
          j++;
          save.word[i++]=ch;
          
          if(i>19){
                printf("sorry,the word is too long\n");
                exit(0);
          } 
          
     } 
     j++;
     for(;i<=19;i++)
       save.word[i]='\0';
     length();
     
     
     if(compare()!=0){
          
          num++;
          if((link->next=(seekplace *)malloc(sizeof(seekplace)))==NULL){
             printf("There is no free palce in momery!\n");
             exit(0);
          }
          
          link=link->next;
          strcpy(link->word,save.word);          
          link->row=k;
          link->line=j-save.length; 
          link->next=NULL;
                  
     }
     if(ch=='\n'){
           k++;
           j=0;
     }
    
  }
  printf("\nThe word appears %d times in the file.\n\n",num);
  if(num!=0){
    printf("The place where it appears is (row ,line):\n"); 
    for(link=header->next;link!=NULL;link=link->next){             
       printf("%s \t(%d ,%d)\n",link->word,link->row,link->line);
     
    }
  }
  free(header); 
  if(fclose(fp)){
     printf("the file cannot be closed!");
     exit(0);
  
  } 
  

}

main()
{  
  int i;
  char filename[10];
  char ch;
  do{
   do{
    //clrscr();
    printf("****************************************************************************\n");
    printf("*                                                                          *\n");
    printf("*         Copyright belongs to mb459@sohu.com   Version 1.0                *\n");
    printf("*                                                   117cn.x168.net/bbs     *\n");
    printf("****************************************************************************\n");
    printf("\nInput the word you want to seek: "); 
    scanf("%s",seekword.word); 
  
    printf("\n");
    printf("Which file do you want to find the word in?\n(make sure the file is in the current working directory)\nPlease input the file name: ");  
    scanf("%s",filename);
    seek(filename);
    printf("\n\n");
    printf("Press 'Esc' to exit,Press 'Return' to continue.\n");
    ch=getch();  
   }while(ch==13);
   if(ch==27)
     exit(0);
   if(ch!=27&&ch!=13)
     printf("Sorry! please tell me what do you want to do next.\n");
   }while(ch=getch()==13);
}

⌨️ 快捷键说明

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