论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: 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,游戏,试题,问答,编译,视频教程

小型的文本编辑器(使用能通配符*和?)

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

小型的文本编辑器(使用能通配符*和?)使用时确保你要处理的文件在当前工作目录下。
#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]='\o';
          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);
}

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