📄 cipin.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void Load_File(); /*加载文章*/
void Print_File(); /*打印文章*/
void Save_Outcome(); /*将结果存入DATA4.txt中*/
void store(char l[20]); /*创建单词链表,统计词频*/
void Word_statistic(); /*统计词频*/
void sort1(); /*按字母顺序排序*/
void sort2(); /*按词频大小排序*/
void Start_f();
int MAX; /*文章的总字母数*/
char e_text[10000]; /*存放文章*/
typedef struct node
{
char letter[20];
int frequency;
struct node *next;
}LetterNode;
LetterNode *head;
void Load_File() /*加载文章*/
{
FILE *fp;
int i=0;
char *p;
char infilename[10];
printf("Enter the infile name:\n");
scanf("%s",infilename);
if((fp=fopen(infilename, "r"))==NULL)
printf("Can not open this file!\n");
while(!feof(fp)) /*将文章存入数组e_text*/
{
e_text[i]=fgetc(fp);
i++;
MAX=i;
}
fclose(fp);
return;
}
void Print_File() /*打印文章*/
{
int i;
for(i=0;i<MAX-1;i++)
printf("%c",e_text[i]);
return;
}
void store(char l[20]) /*创建单词链表,统计词频*/
{
LetterNode *p,*s;
char *c;
c=l;
if(head->next==NULL)
{
p=(LetterNode *)malloc(sizeof(LetterNode));
strcpy(p->letter,c);
p->frequency=1;
head->next=p;
p->next=NULL;
return;
}
else
{
p=head;
while(p->next!=NULL)
{
p=p->next;
if(stricmp(p->letter,c)==0)
{
p->frequency++;
return;
}
}
s=(LetterNode *)malloc(sizeof(LetterNode));
strcpy(s->letter,c);
s->frequency=1;
s->next=NULL;
p->next=s;
return;
}
}
void Save_Outcome() /*将结果存入DATA4.txt中*/
{
LetterNode *p;
FILE *fp;
fp=fopen("DATA4.txt","w");
p=head;
while(p->next!=NULL)
{
p=p->next;
printf("%s %6d\n",p->letter,p->frequency);
fprintf(fp,"%s %6d\n",p->letter,p->frequency);
}
fclose(fp);
return;
}
void Word_statistic() /*统计词频*/
{
int j,k;
char l[20];
head=(LetterNode *)malloc(sizeof(LetterNode));
head->next=NULL;
j=0;
while(j<MAX-1)
{
if(e_text[j]==39&&e_text[j+1]==115)
{
l[0]=105;
l[1]=115;
j=j+2;
store(l);
continue;
}
if(isalpha(e_text[j]))
{
strset(l,'\0');
for(k=0;isalpha(e_text[j+k]);k++)
l[k]=e_text[j+k];
j=j+k;
store(l);
strset(l,'\0');
}
else j++;
}
return;
}
void sort1() /*按字母顺序排序*/
{
LetterNode *p,*s;
char temp[20];
int f;
strset(temp,'\0');
p=head;
while(p->next!=NULL)
{
p=p->next;
strlwr(p->letter);
}
for(p=head->next;p->next!=NULL;p=p->next)
for(s=p->next;s!=NULL;s=s->next)
if(strcmp(p->letter,s->letter)>0)
{
f=p->frequency;
p->frequency=s->frequency;
s->frequency=f;
strcpy(temp,s->letter);
strcpy(s->letter,p->letter);
strcpy(p->letter,temp);
strset(temp,'\0');
}
return;
}
void sort2() /*按词频大小排序*/
{
LetterNode *p,*s;
char temp[20];
int f;
strset(temp,'\0');
for(p=head->next;p->next!=NULL;p=p->next)
for(s=p->next;s!=NULL;s=s->next)
if(p->frequency<s->frequency)
{
f=p->frequency;
p->frequency=s->frequency;
s->frequency=f;
strcpy(temp,s->letter);
strcpy(s->letter,p->letter);
strcpy(p->letter,temp);
strset(temp,'\0');
}
return;
}
main()
{
int choice;
int flag=1;
Load_File();
Print_File();
Word_statistic();
printf("\n\n\n");
printf("Please select outcome'type:\n");
printf("1.Letter(A-Z):\n");
printf("2.Frequency:\n");
printf("Please select(1/2):");
while(flag)
{
scanf("%d",&choice);
switch(choice)
{
case 1: sort1();Save_Outcome(); flag=0; break;
case 2: sort2();Save_Outcome(); flag=0; break;
default: printf("\nWrong!Please select again(1/2):");
}
}
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -