2020-10-17

#include"stdafx.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"test.h"


#define MAX_LINE 1024


bool check_keyword(char* str,int inlen)
{
     bool bRet = false;
     for(int i=0; i<32;i++)
     {
         char *ret = strstr(str, c_keyword[i]);
         if (ret != NULL)
         {
             int outlen = strlen(c_keyword[i]);
             if(inlen == outlen)
             {
                bRet = true;
             }
         }
     }
     return bRet;
}


void mgr_keyword(char* str,int length)
{
    char ch;
    char word[30];
    int j = 0;
    for(int i=0; i<length;i++)
    {
        ch=str[i];
        if(ch!=' '&&ch!=','&&ch!='.'&&ch!='\n'&&ch!='?'&&ch!='!')
        {                  
            word[j]=ch;
            j++;
        }
        else if(ch==' '||ch=='\n'||ch=='\0')
        {
           if(check_keyword(word,j))
           {
               word[j]='\0';
               if(j>2)
                   printf("  关键字: %s\n", word);
               memset(word,0,sizeof(word));
               j=0;
           }
           else
           {
               memset(word,0,sizeof(word));
               j=0;
           }
        }
        else if(ch==','||ch=='.'||ch=='?'||ch=='!')
        {
           if(check_keyword(word,j))
           {
               word[j]=ch;
               word[++j]='\0';
               if(j>2)
                   printf("  关键字带标点: %s\n", word);
               memset(word,0,sizeof(word));
               j=0;
           }
           else
           {
               memset(word,0,sizeof(word));
               j=0;
           }
        }
    }
}

int main()
{
     char buf[MAX_LINE] = {0};  
     char str[MAX_LINE] = {0};
     FILE *fp;          
     int i, j = 0;
     if((fp = fopen("C:\\gt.txt","r")) == NULL)
     {
         perror("fail to read");
         exit (1) ;
     }


     while(fgets(buf,MAX_LINE,fp) != NULL)
     {
        for (i = 0; i<sizeof(buf); i++)
        {
            if(buf[i]=='\n')
            {
                buf[i]=' ';
            }
        }


        for (i = 0; i<sizeof(buf); i++)
        {
            if(buf[i]=='\0')
            {//没有标点符号的一行,合并到下一行一起输出。
                break;
            }

            if ((buf[i] == ',')||(buf[i] == '.')||(buf[i] == '?')||(buf[i] == '!'))
            {
               if((buf[i] == '.'))
               {//assignment1.txt 
                   if(buf[i+1]=='t'&& buf[i+2]=='x'&& buf[i+3]=='t')
                   {
                     str[j] = buf[i];
                     j++;
                     continue;
                   }
               }


               str[j] = buf[i];
               int l=0;
               //句子前的空格不输出
               for(int k=0;i<sizeof(str); k++)
               {
                   if(str[k]==' ')
                   {
                       l++;
                   }
                   else
                   {
                       break;
                   }
               }

               printf("句子: %s\n", str+l);
               mgr_keyword((str+l),strlen(str+l));
               memset(str,0,sizeof(str));
               j = 0;
            }
            else
            {
                 str[j] = buf[i];
               j++;
            }
        }
          memset(buf,0,sizeof(buf));     
    }
     system("pause");
     return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值