📄 arithmetic.c
字号:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
FILE *fin;
char *filein;
struct alphabet /*定义list结构体,alph指定字节,time出现次数 */
{
char alph; /*记录字母*/
double time; /*记录字母出现次数*/
double freq; /*记录字母出现概率*/
double rangelow;
double rangehigh;
};
struct alphabet a[10];
void initiate()
{
int k;
a[1].alph=' ';
a[2].alph='a';
a[3].alph='b';
a[4].alph='e';
a[5].alph='g';
a[6].alph='i';
a[7].alph='l';
a[8].alph='s';
a[9].alph='t';
for(k=1;k<=9;k++)
{
a[k].time=0;
}
}
void input() /*输入文件名,对文件中的每个字节进行计数*/
{
/* FILE *fin,*fout; */
char ch;
int k;
double r=0; /*计算rangelow和rangehigh*/
double sum=0.0;
printf("enter the filename of from which the data is to be read::\n");
scanf("%s",filein);
fin=fopen(filein,"r");
while((ch=fgetc(fin))!=EOF)
{
switch(ch)
{
case ' ': a[1].time=a[1].time+1;break;
case 'a': a[2].time=a[2].time+1;break;
case 'b': a[3].time=a[3].time+1;break;
case 'e': a[4].time=a[4].time+1;break;
case 'g': a[5].time=a[5].time+1;break;
case 'i': a[6].time=a[6].time+1;break;
case 'l': a[7].time=a[7].time+1;break;
case 's': a[8].time=a[8].time+1;break;
case 't': a[9].time=a[9].time+1;break;
}
}
for(k=1;k<=9;k++)
{
sum=sum+a[k].time;
}
for(k=1;k<=9;k++)
{
a[k].freq=a[k].time/sum;
}
for(k=1;k<=9;k++)
{
a[k].rangelow=r;
a[k].rangehigh=r+a[k].freq;
r=r+a[k].freq;
}
fclose(fin); /*关闭文件*/
}
main()
{
int i;
double low,high,range,tlow,tag;
char ch;
initiate();
input();
low=0.0;
high=1.0;
fin=fopen(filein,"r");
while((ch=fgetc(fin))!=EOF)
{
range=high-low;
tlow=low;
switch(ch)
{
case ' ': low=tlow+range*a[1].rangelow;high=tlow+range*a[1].rangehigh;break;
case 'a': low=tlow+range*a[2].rangelow;high=tlow+range*a[2].rangehigh;break;
case 'b': low=tlow+range*a[3].rangelow;high=tlow+range*a[3].rangehigh;break;
case 'e': low=tlow+range*a[4].rangelow;high=tlow+range*a[4].rangehigh;break;
case 'g': low=tlow+range*a[5].rangelow;high=tlow+range*a[5].rangehigh;break;
case 'i': low=tlow+range*a[6].rangelow;high=tlow+range*a[6].rangehigh;break;
case 'l': low=tlow+range*a[7].rangelow;high=tlow+range*a[7].rangehigh;break;
case 's': low=tlow+range*a[8].rangelow;high=tlow+range*a[8].rangehigh;break;
case 't': low=tlow+range*a[9].rangelow;high=tlow+range*a[9].rangehigh;break;
}
}
/* while((ch=fgetc(fin))!=EOF)
printf("%c",ch); */
fclose(fin);
for(i=1;i<=9;i++)
{
printf("\nch=%c time=%g freq=%2.1f,rangelow=%2.1f,rangehigh=%2.1f",a[i].alph,a[i].time,a[i].freq,a[i].rangelow,a[i].rangehigh);
}
tag=(low+high)/2;
printf("\nlow=%12.10f high=%12.10f tag=%12.10f",low,high,tag);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -