#include<stdio.h>
#include<stdlib.h>
# include<ctype.h>
#define N 43
int main( )
{
FILE *fp1,*fp2;
long i,count,sum=0;
char ch,m;
int num[N];
float prob[N];
char string[N]={'a','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z',' ', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0','<','>',',','.''@','#'};
if((fp1=fopen("d:\\string.txt","w"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
fwrite(string,sizeof(char),N,fp1);
if((fp2=fopen("d:\\wenjian.txt","r"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
for(i=0;i<N;i++)
{
count=0;
ch=string[i];
while((m=fgetc(fp2))!=EOF)
{
if(isascii(m))
{
if(m==ch)
count++;
}
}
num[i]=count;
}
for(i=0;i<N;i++)
sum+=num[i];
for(i=0;i<N;i++)
prob[i]=num[i]/sum/1.00f;
fclose(fp1);
fp1=fopen("d:\\prob.txt","a+");
fwrite(prob,sizeof(char),N,fp1);
}
----------------解决方案--------------------------------------------------------
char string[N+1]="abcdefghijklmnopqretuvwsyz 1234567890<>,.@#";
prob[i]=(float)num[i]/(float)sum;
if(m==ch) //fgetc(fp2)怎么不能和CH比较的?
count++;
[此贴子已经被作者于2006-9-1 15:03:45编辑过]
----------------解决方案--------------------------------------------------------
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
# include<ctype.h>
#define N 43
int main()
{
FILE *fp1,*fp2;
long i,count,sum=0;
char ch,m;
int num[N]={0};
float prob[N];
char string[N+1]="abcdefghijklmnopqretuvwsyz 1234567890<>,.@#";
if((fp1=fopen("d:\\string.txt","w"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
fwrite(string,sizeof(char),N+1,fp1);
if((fp2=fopen("d:\\wenjian.txt","r"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
while((m=fgetc(fp2))!=EOF)
{
for(i=0;i<N;i++)
{
ch=string[i];
if(isascii(m))
{
if(m==ch)
num[i]++;
}
}
}
for(i=0;i<N;i++)
sum+=num[i];
for(i=0;i<N;i++)
{
prob[i]=num[i]/(float)sum;
}
fclose(fp1);
fp1=fopen("d:\\prob.txt","w");
for(i=0;i<N;i++)
fprintf(fp1,"%f\n",prob[i]);
getch();
}
以后把程序写清楚些,程序的问题主要出在黑体那块,您再看下,应该很快就明白了。
[此贴子已经被作者于2006-9-1 14:54:19编辑过]
----------------解决方案--------------------------------------------------------
for(i=0;i<N;i++)
{
count=0;
ch=string[i];
while((m=fgetc(fp2))!=EOF)
{
if(isascii(m))
{
if(m==ch) //这里应该可以比较一轮的啊,就是i=0这次,怎么不行的?
count++;
}
}
num[i]=count;
}
#include<stdio.h>
int main(){
FILE *fp2;
char m,ch='h';
fp2=fopen("d:\\wenjian.txt","r");
while((m=fgetc(fp2))!=EOF)
{ putchar(m);
if(m==ch) / /这里怎么不行的??
printf("%c\t",m);
}}
[此贴子已经被作者于2006-9-1 15:10:25编辑过]
----------------解决方案--------------------------------------------------------
行的。为什么不行?
即输出的概率p(a)=1.0.
----------------解决方案--------------------------------------------------------
#include<stdio.h>
int main(){
FILE *fp2;
char m,ch='h';
fp2=fopen("d:\\wenjian.txt","r");
while((m=fgetc(fp2))!=EOF)
{ putchar(m);
if(m==ch) / /这里怎么不行的??
printf("%c\t",m);//这个打印不出来的?
}}
----------------解决方案--------------------------------------------------------
真是谢谢上面的几位高手,我明白了,非常感谢!
----------------解决方案--------------------------------------------------------