用C统计单词

 http://blog.sina.com.cn/s/blog_5f5455450100csdn.html

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


typedef struct LNode{
 char          ch[20];       //放单词。
 int           count;        //次数。
 struct LNode  *next;
}LNode;


LNode *PL;
int   m=0;

void word();      //统计单词次数,及单词总数。  
void search();    //查找单词。
void sentence();  //句子数。
void sort();      //排序。
void encrypt();   //加密。
void decrypt();   //解密。
void image();     //图形显示。

void main()
 

 int a,b,c;
 char s[20];
 while(1){
 printf("        英文单词统计器             \n");
 printf("***********************************\n");
 printf("*  1.统计文件单词次数,及总次数.  *\n");
 printf("*  2.排序后写进文件.              *\n");
 printf("*  3.查找所需单词.                *\n");
 printf("*  4.统计句子数.                  *\n");
 printf("*  5.加密.                        *\n");
 printf("*  6.解密.                        *\n");
 printf("*  7.退出.                        *\n");
 printf("***********************************\n");
    scanf("%d",&a);
 switch(a){
     case 1:   word();       break;
  case 2:   sort();       break;
  case 3:    printf("input:");
          scanf("%s",&s);
          search( s);
          break;
  case 4:   sentence();   break;
        case 5:   printf("input:");
         scanf("%d",&b);
                  encrypt( b);
      break;
        case 6:   printf("input:");
         scanf("%d",&c);
      decrypt( c);
      break;
        case 7:   exit(0);
  default:  exit(0);
 }
 }
      
}
//

void sort()
{
    LNode *ptr1,*ptr2,*temp,*ptr3,*p2;
    FILE *fp2;
    if((fp2=fopen("out.txt","w"))==NULL){
  printf("file can't open");    exit(0);}
    ptr3=PL->next;
    while(ptr3!=NULL)
    {
        ptr1=PL->next;
        ptr2=PL->next->next;
        ptr3=ptr3->next;
        while(ptr2!=NULL)
        {
            if(strcmpi(ptr1->ch,ptr2->ch)>0)
             
                temp=(LNode *)malloc(sizeof(LNode));
                strcpy(temp->ch,ptr2->ch);
    temp->count=ptr2->count;
                strcpy(ptr2->ch,ptr1->ch);
    ptr2->count=ptr1->count;
                strcpy(ptr1->ch,temp->ch);
    ptr1->count=temp->count;
            }
        ptr1=ptr2;
        ptr2=ptr2->next;   
        }
       
    }
 p2=PL->next;
  while(p2!=NULL)
  {
   fprintf(fp2,"%s:%d\n",p2->ch,p2->count);
   printf("%s:%d次\n",p2->ch,p2->count);
   p2=p2->next;
  }
    fclose(fp2);
}
//

void sentence()   
{
    FILE *fp1;
 int  num=0;
 char ch;
    if((fp1=fopen("in.txt","r"))==NULL){
  printf("file can't open");    exit(0);}
    do
 {
     ch=fgetc(fp1);
  if(ch==','||ch=='.'||ch=='!'||ch=='?')
   num++;
 }while(!feof(fp1));
 printf("句子数为:%d\n",num);
}
//

void search(char s[20])  
{
 LNode *p1;
 p1=PL;
 do{
  if(strcmp(p1->ch,s)==0)   {printf("%s:%d次\n",p1->ch,p1->count);  break;}
  else p1=p1->next;}while(p1!=NULL);
}
//
void encrypt(int n)
{
    FILE *fp1,*fp2;
 char ch;
    if((fp1=fopen("in.txt","r"))==NULL){
  printf("file can't open");    exit(0);}
    if((fp2=fopen("jiami.txt","w"))==NULL){
  printf("file can't open");    exit(0);}
 do{
  ch=fgetc(fp1);
  if(ch>='A'&&ch<='Z')   {ch='A'+(ch-'A'+n)%26;  fputc(ch,fp2);}          //让26个字母循环;
  else if(ch>='a'&&ch<='z')  {ch='a'+(ch-'a'+n)%26;  fputc(ch,fp2);}
  else fputc(ch,fp2);
 }while(!feof(fp1));
    if((fp1=fopen("in.txt","w"))==NULL){
  printf("file can't open");    exit(0);}
 remove("in.txt");
 printf("加密成功!\n");
}
//
void decrypt(int n)
{
    FILE *fp1,*fp2;
 char ch;
    if((fp1=fopen("jiami.txt","r"))==NULL){
  printf("file can't open");    exit(0);}
    if((fp2=fopen("in.txt","w"))==NULL){
  printf("file can't open");    exit(0);}
 n=26-n;
 do{
  ch=fgetc(fp1);
  if(ch>='A'&&ch<='Z')   {ch='A'+(ch-'A'+n)%26;  fputc(ch,fp2);}
  else if(ch>='a'&&ch<='z')  {ch='a'+(ch-'a'+n)%26;  fputc(ch,fp2);}
  else fputc(ch,fp2);
 }while(!feof(fp1));
    if((fp1=fopen("jiami.txt","w"))==NULL){
  printf("file can't open");    exit(0);}
 remove("jiami.txt");
 printf("解密成功!\n");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值